« | October 2025 | » | 日 | 一 | 二 | 三 | 四 | 五 | 六 | | | | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | | |
| 公告 |
戒除浮躁,读好书,交益友 |
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。
这就是说添加了一个,再继续添加,会出现重复提交的问题。 |
|
|