Spring的BindException设置错误信息

来源:互联网 发布:java中方法的重写 编辑:程序博客网 时间:2024/05/15 23:48

      <spring:bind path="command.userName">

            名称<input type="text" name="${status.expression}" value="${status.value}"/>

            <font color="red"><c:out value="${status.errorMessage}"/></font><br/>

     </spring:bind>

     <spring:bind path="command.password">

            密码<input type="password" name="${status.expression}" value="${status.value}"/>

            <font color="red"><c:out value="${status.errorMessage}"/></font><br/>

     </spring:bind>

  • errors.reject("ccc","用户名或密码错误!");
  • errors.rejectValue("userName","nameErr",null,"用户名错误!“);
  • errors.rejectValue("password","passErr",null,"密码错误!”);

——》调用BindException的reject方法设置错误信息,之后调用BindException的getModel()方法就可以把错误连同表单等信息一并返回到表单页面显示。

(1)reject方法:

           参数一:是错误码,如果设定了国际化资源,则显示资源文件中该错误码对应的错误条目;如果没有设定国际化资源,则显示参数二。

           参数二:错误信息。

           ——》不足:在表现层不能区分错误信息属于哪个字段。

(2)rejectValue方法:

           rejectValue(String field, String errorCode, Object[] errorArgs, String defaultMessage)

           参数一:指定表单的域,即username或password。

           参数二:指定错误码。

           参数三:指定资源文件中的占位符。

           参数四:指定错误信息。

(3)rejectValue简化方法:

           rejectValue(String field,String errorCode,String defaultMessage)

0 0