Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/asp.net-mvc-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 从SpringMVC返回警告和错误?_Java_Spring_Jsp_Spring Mvc - Fatal编程技术网

Java 从SpringMVC返回警告和错误?

Java 从SpringMVC返回警告和错误?,java,spring,jsp,spring-mvc,Java,Spring,Jsp,Spring Mvc,我正在寻找一种返回非致命验证“警告”消息以显示在JSP表单上的方法(除了通常的验证“错误”消息)。这些消息将允许处理继续进行,而不是阻止任务的完成 我想使用现有的Spring4MVC管道:BindingResult对象、SpringValidator接口和Spring标签。但是到目前为止,我还没有能够计算出返回第二个BindingResult对象并显示消息的细节 以下是到目前为止我得到的信息: myFormValidator.validate(myForm, bindingResult); my

我正在寻找一种返回非致命验证“警告”消息以显示在JSP表单上的方法(除了通常的验证“错误”消息)。这些消息将允许处理继续进行,而不是阻止任务的完成

我想使用现有的Spring4MVC管道:BindingResult对象、SpringValidator接口和Spring标签。但是到目前为止,我还没有能够计算出返回第二个BindingResult对象并显示消息的细节

以下是到目前为止我得到的信息:

myFormValidator.validate(myForm, bindingResult);
myFormValidator.validateWarnings(myForm, warnings);
model.addAttribute("warnings",warnings);
return new ModelAndView(FORM_VIEW, "myForm", myForm);

但是我不知道如何将自由浮动BindingResult对象
warnings
绑定到JSP端的
标记上。有人能帮上忙吗?

使用现有的
标记似乎没有一种简单的方法可以做到这一点。最后我做的是基于ErrorsTag.java及其超类创建一个自定义标记

以下是标记的外观:

这将在一个样式良好的块中列出
myForm.myFieldName
的所有警告

以下是内脏(样板可从以下网站获得):

protectedboolean shouldRender()抛出JspException{
试一试{
如果(this.getPath()!=null&&
此.getPath()!=“”){
//字段错误
return(this.bind.hasfielders(this.getPath());
}否则{
//全局误差
返回此.bind.hasGlobalErrors();
}
}捕获(非法状态例外){
//BindingResult和目标对象均不可用。
返回false;
}
}
受保护的int-writeTagContent(TagWriter-TagWriter)抛出JSPEException{
if(shouldRender()){
返回doWrite(tagWriter);
}
返回BodyTag.SKIP_BODY;
}
受保护的int doWrite(TagWriter TagWriter)抛出JSPEException{
试一试{
tagWriter.startTag(getElement());
writeDefaultAttributes(标记编写器);
字符串分隔符=ObjectUtils.getDisplayString(求值(“分隔符”,getDelimiter());
List errorMessages=null;
if(this.getPath()!=null){
errorMessages=getFieldErrorMessages(bind.getFieldErrors(this.getPath());
}否则{
errorMessages=getGlobalErrorMessages(bind.getGlobalErrors());
}
tagWriter.appendValue(“
    ”); 对于(int i=0;i); tagWriter.appendValue(getDisplayString(errorMessage)); tagWriter.appendValue(“”); } tagWriter.appendValue(“
”); tagWriter.forceBlock(); tagWriter.endTag(); }捕获(NullPointerException ex){ 例如printStackTrace(); } 返回BodyTag.EVAL_BODY_INCLUDE; }
作为补充,我需要一些快速的东西-我相信这是可以改进的。
protected boolean shouldRender() throws JspException {
    try {
        if (this.getPath() != null &&
                this.getPath() != "") {
            // Field errors
            return (this.bind.hasFieldErrors(this.getPath()));
        } else {
            // Global errors
            return this.bind.hasGlobalErrors();
        }
    } catch (IllegalStateException ex) {
        // Neither BindingResult nor target object available.
        return false;
    }
}

protected int writeTagContent(TagWriter tagWriter) throws JspException{
    if (shouldRender()) {
        return doWrite(tagWriter);
    }
    return BodyTag.SKIP_BODY;
}

protected int doWrite(TagWriter tagWriter) throws JspException {
    try {
        tagWriter.startTag(getElement());
        writeDefaultAttributes(tagWriter);
        String delimiter = ObjectUtils.getDisplayString(evaluate("delimiter", getDelimiter()));
        List<String> errorMessages = null;
        if (this.getPath() != null) {
            errorMessages = getFieldErrorMessages(bind.getFieldErrors(this.getPath()));
        } else {
            errorMessages = getGlobalErrorMessages(bind.getGlobalErrors());
        }
        tagWriter.appendValue("<ul>");
        for (int i = 0; i < errorMessages.size(); i++) {
            String errorMessage = errorMessages.get(i);
            tagWriter.appendValue("<li>");
            tagWriter.appendValue(getDisplayString(errorMessage));
            tagWriter.appendValue("</li>");
        }
        tagWriter.appendValue("</ul>");
        tagWriter.forceBlock();
        tagWriter.endTag();
    } catch (NullPointerException ex) {
        ex.printStackTrace();
    }

    return BodyTag.EVAL_BODY_INCLUDE;
}