基于Spring MVC的Web应用开发(12) - Form

本节介绍SpringMVC中的表单,demo演示访问一个表单提交页面,填写表单的内容后使用jQuery的Ajax提交表单,将返回的文本信息显示出来。

记得在Struts1中有个FormBean的东西封装表单内容,在SpringMVC中也有,只不过SpringMVC更松耦合,只要写一个POJO就可以了,而不需要继承框架关联的类,看一下这个FormBean(只列出了主要属性):

 

Java代码   收藏代码
  1. public class FormBean {  
  2.       
  3.     @NotEmpty  
  4.     private String name;  
  5.       
  6.     @Min(21)  
  7.     private int age;  
  8.   
  9.     @DateTimeFormat(iso=ISO.DATE)  
  10.     @Past  
  11.     private Date birthDate;  
  12.   
  13.     @MaskFormat("(###) ###-####")  
  14.     private String phone;  
  15.   
  16.     @NumberFormat(pattern="$###,###.00")  
  17.     private BigDecimal currency;  
  18.   
  19.     @NumberFormat(style=Style.PERCENT)  
  20.     private BigDecimal percent;  
  21.       
  22.     private InquiryType inquiry;  
  23.       
  24.     private String inquiryDetails;  
  25.       
  26.     private boolean subscribeNewsletter;  
  27.       
  28.     private Map<String, String> additionalInfo;  
  29. ...  
  30. }  

 

 需要一个Controller,FormController:

 

Java代码   收藏代码
  1. package org.springframework.samples.mvc.form;  
  2.   
  3. import javax.validation.Valid;  
  4.   
  5. import org.springframework.mvc.extensions.ajax.AjaxUtils;  
  6. import org.springframework.stereotype.Controller;  
  7. import org.springframework.ui.Model;  
  8. import org.springframework.validation.BindingResult;  
  9. import org.springframework.web.bind.annotation.ModelAttribute;  
  10. import org.springframework.web.bind.annotation.RequestMapping;  
  11. import org.springframework.web.bind.annotation.RequestMethod;  
  12. import org.springframework.web.bind.annotation.SessionAttributes;  
  13. import org.springframework.web.context.request.WebRequest;  
  14. import org.springframework.web.servlet.mvc.support.RedirectAttributes;  
  15.   
  16. @Controller  
  17. @RequestMapping("/form")  
  18. @SessionAttributes("formBean")  
  19. public class FormController {  
  20.   
  21.     // Invoked on every request  
  22.   
  23.     @ModelAttribute  
  24.     public void ajaxAttribute(WebRequest request, Model model) {  
  25.         model.addAttribute("ajaxRequest", AjaxUtils.isAjaxRequest(request));  
  26.     }  
  27.   
  28.     // Invoked initially to create the "form" attribute  
  29.     // Once created the "form" attribute comes from the HTTP session (see @SessionAttributes)  
  30.   
  31.     @ModelAttribute("formBean")  
  32.     public FormBean createFormBean() {  
  33.         return new FormBean();  
  34.     }  
  35.       
  36.     @RequestMapping(method=RequestMethod.GET)  
  37.     public void form() {  
  38.     }  
  39.   
  40.     @RequestMapping(method=RequestMethod.POST)  
  41.     public String processSubmit(@Valid FormBean formBean, BindingResult result,   
  42.                                 @ModelAttribute("ajaxRequest"boolean ajaxRequest,   
  43.                                 Model model, RedirectAttributes redirectAttrs) {  
  44.         if (result.hasErrors()) {  
  45.             return null;  
  46.         }  
  47.         // Typically you would save to a db and clear the "form" attribute from the session   
  48.         // via SessionStatus.setCompleted(). For the demo we leave it in the session.  
  49.         String message = "Form submitted successfully.  Bound " + formBean;  
  50.         // Success response handling  
  51.         if (ajaxRequest) {  
  52.             // prepare model for rendering success message in this request  
  53.             model.addAttribute("message", message);  
  54.             return null;  
  55.         } else {  
  56.             // store a success message for rendering on the next request after redirect  
  57.             // redirect back to the form to render the success message along with newly bound values  
  58.             redirectAttrs.addFlashAttribute("message", message);  
  59.             return "redirect:/form";              
  60.         }  
  61.     }  
  62.       
  63. }  

 

 FormController只有一个@RequestMapping,通过GET,POST来区分是访问表单页面(GET),还是提交表单(POST)。在这个类中有:

 

Java代码   收藏代码
  1. @ModelAttribute("formBean")  
  2. public FormBean createFormBean() {  
  3.     return new FormBean();  
  4. }  

 

 它表示访问"http://localhost:8080/web/form/"时,就初始化一个叫formBean名字的属性放在Model中。在类的开头有:

 

Java代码   收藏代码
  1. @SessionAttributes("formBean")  

 

 它表示Model中的这个formBean在session的范围内有效,想一下,session是跟浏览器窗口关联的,窗口关闭,session就失效,所以每次打开一个新的表单页面都会生成一个新的formBean,另外填写完表单内容以POST方式提交后,formBean会根据提交的参数自动设置FormBean的值。

 

Java代码   收藏代码
  1. @ModelAttribute  
  2. public void ajaxAttribute(WebRequest request, Model model) {  
  3.     model.addAttribute("ajaxRequest", AjaxUtils.isAjaxRequest(request));  
  4. }  

 

 该配置会导致每次访问"http://localhost:8080/web/form/",都会设置一个ajaxRequest值(因为没有象formBean一样设置ajaxRequest的范围,默认的范围为Request级,每次请求都执行)。之前说过填好表单后通过jQuery的Ajax提交表单,通过这个配置,就知道请求是否是Ajax请求。

 

Java代码   收藏代码
  1. @RequestMapping(method=RequestMethod.GET)  
  2. public void form() {  
  3. }  

访问"http://localhost:8080/web/form/",返回到"webapp/WEB-INF/views/form.jsp"。

 

Java代码   收藏代码
  1. @RequestMapping(method=RequestMethod.POST)  
  2. public String processSubmit(@Valid FormBean formBean, BindingResult result,   
  3.                             @ModelAttribute("ajaxRequest"boolean ajaxRequest,   
  4.                             Model model, RedirectAttributes redirectAttrs) {  
  5.     if (result.hasErrors()) {  
  6.         return null;  
  7.     }  
  8.     // Typically you would save to a db and clear the "form" attribute from the session   
  9.     // via SessionStatus.setCompleted(). For the demo we leave it in the session.  
  10.     String message = "Form submitted successfully.  Bound " + formBean;  
  11.     // Success response handling  
  12.     if (ajaxRequest) {  
  13.         // prepare model for rendering success message in this request  
  14.         model.addAttribute("message", message);  
  15.         return null;  
  16.     } else {  
  17.         // store a success message for rendering on the next request after redirect  
  18.         // redirect back to the form to render the success message along with newly bound values  
  19.         redirectAttrs.addFlashAttribute("message", message);  
  20.         return "redirect:/form";              
  21.     }  
  22. }  

processSubmit第一个参数formBean封装了页面提交的表单参数,注意到它前面有一个@Valid,因此有第二个参数BindingResult result, 该参数可以知道表单是否验证通过。第三个参数获取Model中的属性值"ajaxRequest",在方法体中,判断ajaxRequest,如果是,将返回"webapp/WEB-INF/views/form.jsp",如果不是,将它重定向到一个jsp,当然,这个条件在本例子中没有使用到。

 

最后看看非常复杂的form.jsp,该jsp使用了大量的SpringMVC的标签:

 

Html代码   收藏代码
  1. <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>  
  2. <%@ taglib uri="http://www.springframework.org/tags" prefix="s" %>  
  3. <%@ taglib uri="http://www.springframework.org/tags/form" prefix="form" %>  
  4. <%@ page session="false" %>  
  5. <c:if test="${!ajaxRequest}">  
  6. <html>  
  7. <head>  
  8.     <title>forms | mvc-showcase</title>  
  9.     <link href="<c:url value="/resources/form.css" />rel="stylesheet"  type="text/css" />       
  10.     <script type="text/javascript" src="<c:url value="/resources/jquery/1.6/jquery.js" />"></script>  
  11. </head>  
  12. <body>  
  13. </c:if>  
  14.     <div id="formsContent">  
  15.         <h2>Forms</h2>  
  16.         <p>  
  17.             See the <code>org.springframework.samples.mvc.form</code> package for the @Controller code    
  18.         </p>  
  19.         <form:form id="form" method="post" modelAttribute="formBean" cssClass="cleanform">  
  20.             <div class="header">  
  21.                 <h2>Form</h2>  
  22.                 <c:if test="${not empty message}">  
  23.                     <div id="message" class="success">${message}</div>    
  24.                 </c:if>  
  25.                 <s:bind path="*">  
  26.                     <c:if test="${status.error}">  
  27.                         <div id="message" class="error">Form has errors</div>  
  28.                     </c:if>  
  29.                 </s:bind>  
  30.             </div>  
  31.             <fieldset>  
  32.                 <legend>Personal Info</legend>  
  33.                 <form:label path="name">  
  34.                     Name <form:errors path="name" cssClass="error" />  
  35.                 </form:label>  
  36.                 <form:input path="name" />  
  37.       
  38.                 <form:label path="age">  
  39.                     Age <form:errors path="age" cssClass="error" />  
  40.                 </form:label>  
  41.                 <form:input path="age" />  
  42.                   
  43.                 <form:label path="birthDate">  
  44.                     Birth Date (in form yyyy-mm-dd) <form:errors path="birthDate" cssClass="error" />  
  45.                 </form:label>  
  46.                 <form:input path="birthDate" />  
  47.                    
  48.                 <form:label path="phone">  
  49.                     Phone (in form (###) ###-####) <form:errors path="phone" cssClass="error" />  
  50.                 </form:label>  
  51.                 <form:input path="phone" />  
  52.       
  53.                 <form:label path="currency">  
  54.                     Currency (in form $#.##) <form:errors path="currency" cssClass="error" />  
  55.                 </form:label>  
  56.                 <form:input path="currency" />  
  57.       
  58.                 <form:label path="percent">  
  59.                     Percentage (in form ##%) <form:errors path="percent" cssClass="error" />  
  60.                 </form:label>  
  61.                 <form:input path="percent" />  
  62.       
  63.             </fieldset>  
  64.       
  65.             <fieldset>  
  66.                 <legend>Inquiry</legend>  
  67.                 <form:label path="inquiry">  
  68.                     Type (select one)  
  69.                 </form:label>  
  70.                 <form:select path="inquiry">  
  71.                     <form:option value="comment">Comment</form:option>  
  72.                     <form:option value="feedback">Feedback</form:option>  
  73.                     <form:option value="suggestion">Suggestion</form:option>  
  74.                 </form:select>  
  75.                   
  76.                 <form:label path="inquiryDetails">  
  77.                     Details  
  78.                 </form:label>  
  79.                 <form:textarea path="inquiryDetails" />  
  80.             </fieldset>  
  81.       
  82.             <fieldset class="checkbox">  
  83.                 <legend>Request Additional Info</legend>  
  84.                 <label><form:checkbox path="additionalInfo[mvc]" value="true" />on Spring MVC</label>  
  85.                 <label><form:checkbox path="additionalInfo[java]" value="true" />on Java (4-ever)</label>               
  86.             </fieldset>  
  87.                       
  88.             <fieldset class="radio">  
  89.                 <legend>Subscribe to Newsletter?</legend>  
  90.                 <label><form:radiobutton path="subscribeNewsletter" value="true" />Yes</label>  
  91.                 <label><form:radiobutton path="subscribeNewsletter" value="false" /> No</label>  
  92.             </fieldset>  
  93.       
  94.             <p><button type="submit">Submit</button></p>  
  95.         </form:form>  
  96.         <script type="text/javascript">  
  97.             $(document).ready(function() {  
  98.                 $("#form").submit(function() {    
  99.                     $.post($(this).attr("action"), $(this).serialize(), function(html) {  
  100.                         $("#formsContent").replaceWith(html);  
  101.                         $('html, body').animate({ scrollTop: $("#message").offset().top }, 500);  
  102.                     });  
  103.                     return false;    
  104.                 });           
  105.             });  
  106.         </script>  
  107.     </div>  
  108. <c:if test="${!ajaxRequest}">  
  109. </body>  
  110. </html>  
  111. </c:if>  
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值