本站首页    管理页面    写新日志    退出


«October 2025»
1234
567891011
12131415161718
19202122232425
262728293031


公告

戒除浮躁,读好书,交益友


我的分类(专题)

日志更新

最新评论

留言板

链接

Blog信息
blog名称:邢红瑞的blog
日志总数:523
评论数量:1142
留言数量:0
访问次数:9719704
建立时间:2004年12月20日




[j2ee]spring:bind注意的问题
原创空间,  文章收藏,  软件技术,  电脑与网络

邢红瑞 发表于 2006/8/27 20:47:52

校验用户的输入 <spring:bind path="command.*">                                  <c:if test="${not empty status.errorMessages}">                                      <div class="error">                                           <c:forEach var="error" items="${status.errorMessages}">                                                 <c:out value="${error}" escapeXml="false"/><br/>                                          </c:forEach>                                      </div>                                  </c:if>                           </spring:bind> spring:bind是个脆弱的标签,建议spring 2.0后不要使用,出现javax.servlet.ServletException: Neither Errors instance nor plain target object for bean name 'command' available as request attribute查看xml中那个SimpleFormController的配置,是否配置了commandName。<property name="commandName">   <value>command</value></property>当然也必须设置Command类或者构造中使用setCommandClass(Member.class);setCommandName("member");setValidator(new MemberValidator());setSessionForm(true);setFormView("registerMember");setSuccessView("redirect:successRegisterMember");}不可以直接访问那个bind的jsp,因为解析时 bind 标签从request中的得不到command 对象,所以必须经过这个SimpleFormController才不会报错,如果经过别的FormController或者Controller,request要加入request.setAttribute("command", member);否则 解析时 bind 标签从request中的得不到command 对象。如里bind的信息没有显示出来的controller是否return时有new ModelAndView("xxx",errors.getModel());必须有errors.getModel();因为errors是传过来的BindException对象或者return 前request.setAttribute("command", member);   protected ModelAndView onSubmit(Object command) throws Exception {    FormData form = (FormData) command;    if (form.getUsername().equals("good")) {      return new ModelAndView(this.getSuccessView());    } else {      return new ModelAndView(this.getFormView(), this.getCommandName(),form);    }  }肯定不行的,应该用onSubmit(Object command, BindException errors)或doSubmitAction(Object command),onSubmit(Object command, BindException errors)時,要用errors.getModel()取得model,定义onSubmit(Object command),要自己设置model。源码 * <p>Call <code>errors.getModel()</code> to populate the ModelAndView model   * with the command and the Errors instance, under the specified command name,   * as expected by the "spring:bind" tag.   protected ModelAndView onSubmit(      HttpServletRequest request,  HttpServletResponse response, Object command,  BindException errors)      throws Exception {     return onSubmit(command, errors);  }    * <p>Call <code>errors.getModel()</code> to populate the ModelAndView model   * with the command and the Errors instance, under the specified command name,   * as expected by the "spring:bind" tag.   protected ModelAndView onSubmit(Object command, BindException errors) throws Exception {    ModelAndView mv = onSubmit(command);    if (mv != null) {      // simplest onSubmit version implemented in custom subclass      return mv;    }    else {      // default behavior: render success view      if (getSuccessView() == null) {        throw new ServletException("successView isn't set");      }      return new ModelAndView(getSuccessView(), errors.getModel());    }  } ...It's preferable to use either   * <code>onSubmit(command, errors)</code> or <code>doSubmitAction(command)</code>,   * though: Use the former when you want to build your own ModelAndView; use the   * latter when you want to perform an action and forward to the successView.   protected ModelAndView onSubmit(Object command) throws Exception {    doSubmitAction(command);    return null;  }...  protected void doSubmitAction(Object command) throws Exception {  }   在onSubmit(Object command, BindException errors)中new ModelAndView(getSuccessView(), errors.getModel()),取得model。 这就是说添加了一个,再继续添加,会出现重复提交的问题。


阅读全文(5246) | 回复(0) | 编辑 | 精华
 



发表评论:
昵称:
密码:
主页:
标题:
验证码:  (不区分大小写,请仔细填写,输错需重写评论内容!)



站点首页 | 联系我们 | 博客注册 | 博客登陆

Sponsored By W3CHINA
W3CHINA Blog 0.8 Processed in 0.852 second(s), page refreshed 144812789 times.
《全国人大常委会关于维护互联网安全的决定》  《计算机信息网络国际联网安全保护管理办法》
苏ICP备05006046号