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

在FileUpload一文中,我们初步了解了SpringMVC中View的用法,在例子中,通过给Model添加一个属性(model.addAttribute()),View对应的JSP就可以获取该值。本文再介绍一些View对应JSP取值的方式。

增加一个Controller,ViewsController:

Java代码 复制代码  收藏代码
  1. package org.springframework.samples.mvc.views;   
  2.   
  3. import javax.validation.Valid;   
  4.   
  5. import org.springframework.stereotype.Controller;   
  6. import org.springframework.ui.Model;   
  7. import org.springframework.web.bind.annotation.PathVariable;   
  8. import org.springframework.web.bind.annotation.RequestMapping;   
  9. import org.springframework.web.bind.annotation.RequestMethod;   
  10.   
  11. @Controller  
  12. @RequestMapping("/views/*")   
  13. public class ViewsController {   
  14.   
  15.     @RequestMapping(value="html", method=RequestMethod.GET)   
  16.     public String prepare(Model model) {   
  17.         model.addAttribute("foo""bar");   
  18.         model.addAttribute("fruit""apple");   
  19.         return "views/html";   
  20.     }   
  21.        
  22.     @RequestMapping(value="/viewName", method=RequestMethod.GET)   
  23.     public void usingRequestToViewNameTranslator(Model model) {   
  24.         model.addAttribute("foo""bar");   
  25.         model.addAttribute("fruit""apple");   
  26.     }   
  27.   
  28.     @RequestMapping(value="pathVariables/{foo}/{fruit}", method=RequestMethod.GET)   
  29.     public String pathVars(@PathVariable String foo, @PathVariable String fruit) {   
  30.         // No need to add @PathVariables "foo" and "fruit" to the model   
  31.         // They will be merged in the model before rendering   
  32.         return "views/html";   
  33.     }   
  34.   
  35.     @RequestMapping(value="dataBinding/{foo}/{fruit}", method=RequestMethod.GET)   
  36.     public String dataBinding(@Valid JavaBean javaBean, Model model) {   
  37.         // JavaBean "foo" and "fruit" properties populated from URI variables    
  38.         return "views/dataBinding";   
  39.     }   
  40.   
  41. }  
package org.springframework.samples.mvc.views;

import javax.validation.Valid;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
@RequestMapping("/views/*")
public class ViewsController {

	@RequestMapping(value="html", method=RequestMethod.GET)
	public String prepare(Model model) {
		model.addAttribute("foo", "bar");
		model.addAttribute("fruit", "apple");
		return "views/html";
	}
	
	@RequestMapping(value="/viewName", method=RequestMethod.GET)
	public void usingRequestToViewNameTranslator(Model model) {
		model.addAttribute("foo", "bar");
		model.addAttribute("fruit", "apple");
	}

	@RequestMapping(value="pathVariables/{foo}/{fruit}", method=RequestMethod.GET)
	public String pathVars(@PathVariable String foo, @PathVariable String fruit) {
		// No need to add @PathVariables "foo" and "fruit" to the model
		// They will be merged in the model before rendering
		return "views/html";
	}

	@RequestMapping(value="dataBinding/{foo}/{fruit}", method=RequestMethod.GET)
	public String dataBinding(@Valid JavaBean javaBean, Model model) {
		// JavaBean "foo" and "fruit" properties populated from URI variables 
		return "views/dataBinding";
	}

}

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

Html代码 复制代码  收藏代码
  1. <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>  
  2. <%@ page session="false" %>  
  3. <html>  
  4. <head>  
  5.     <title>My HTML View</title>  
  6.     <link href="<c:url value="/resources/form.css" />rel="stylesheet"  type="text/css" />        
  7. </head>  
  8. <body>  
  9. <div class="success">  
  10.     <h3>foo: "${foo}"</h3>  
  11.     <h3>fruit: "${fruit}"</h3>  
  12. </div>  
  13. </body>  
  14. </html>  
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ page session="false" %>
<html>
<head>
	<title>My HTML View</title>
	<link href="<c:url value="/resources/form.css" />" rel="stylesheet"  type="text/css" />		
</head>
<body>
<div class="success">
	<h3>foo: "${foo}"</h3>
	<h3>fruit: "${fruit}"</h3>
</div>
</body>
</html>

 prepare(Model model)同FileUpload一样,通过model.addAttribite("foo", "bar");,JSP的${foo}就能获得"bar"。

2. 访问"http://localhost:8080/web/views/viewName",返回到"webapp/WEB-INF/views/views/viewName.jsp",这个jsp文件和html.jsp一样。

usingRequestToViewNameTranslator(Model model)和prepare(Model model)稍有不同,该方法的返回值为void,SpringMVC认为返回值为void的View名字就是@RequestMapping中映射的完整路径,即"views/viewName"。

3. 访问"http://localhost:8080/web/views/pathVariables/bar/orange",返回到"webapp/WEB-INF/views/views/html.jsp"

pathVars方法多了@PathVariable注解,该注解解析URL路径,并赋值给带有@PathVaribale的变量,View对应的JSP可以直接读取到这个变量的值。

4. 访问"http://localhost:8080/web/views/dataBinding/bar/orange",返回到"webapp/WEB-INF/views/views/dataBinding.jsp"

Html代码 复制代码  收藏代码
  1. <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>  
  2. <%@ page session="false" %>  
  3. <html>  
  4. <head>  
  5.     <title>Data Binding with URI Template Variables</title>  
  6.     <link href="<c:url value="/resources/form.css" />rel="stylesheet"  type="text/css" />        
  7. </head>  
  8. <body>  
  9. <div class="success">  
  10.     <h3>javaBean.foo: ${javaBean.foo}</h3>  
  11.     <h3>javaBean.fruit: ${javaBean.fruit}</h3>  
  12. </div>  
  13. </body>  
  14. </html>  
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ page session="false" %>
<html>
<head>
	<title>Data Binding with URI Template Variables</title>
	<link href="<c:url value="/resources/form.css" />" rel="stylesheet"  type="text/css" />		
</head>
<body>
<div class="success">
	<h3>javaBean.foo: ${javaBean.foo}</h3>
	<h3>javaBean.fruit: ${javaBean.fruit}</h3>
</div>
</body>
</html>

 dataBinding(@Valid JavaBean javaBean, Model model)的方法参数中有个自定义的Java类,SpringMVC会自动解析URL路径,并且感知foo和fruit是否为JavaBean的属性,如果是,则将它赋值给JavaBean,非常智能。JSP现在得到的就是一个JavaBean,因此需要使用${javaBean.foo}获取具体值。

=================================================================================

=================================================================================

本节介绍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. }  
public class FormBean {
	
	@NotEmpty
	private String name;
	
	@Min(21)
	private int age;

	@DateTimeFormat(iso=ISO.DATE)
	@Past
	private Date birthDate;

	@MaskFormat("(###) ###-####")
	private String phone;

	@NumberFormat(pattern="$###,###.00")
	private BigDecimal currency;

	@NumberFormat(style=Style.PERCENT)
	private BigDecimal percent;
	
	private InquiryType inquiry;
	
	private String inquiryDetails;
	
	private boolean subscribeNewsletter;
	
	private Map<String, String> additionalInfo;
...
}

 需要一个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. }  
package org.springframework.samples.mvc.form;

import javax.validation.Valid;

import org.springframework.mvc.extensions.ajax.AjaxUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.context.request.WebRequest;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

@Controller
@RequestMapping("/form")
@SessionAttributes("formBean")
public class FormController {

	// Invoked on every request

	@ModelAttribute
	public void ajaxAttribute(WebRequest request, Model model) {
		model.addAttribute("ajaxRequest", AjaxUtils.isAjaxRequest(request));
	}

	// Invoked initially to create the "form" attribute
	// Once created the "form" attribute comes from the HTTP session (see @SessionAttributes)

	@ModelAttribute("formBean")
	public FormBean createFormBean() {
		return new FormBean();
	}
	
	@RequestMapping(method=RequestMethod.GET)
	public void form() {
	}

	@RequestMapping(method=RequestMethod.POST)
	public String processSubmit(@Valid FormBean formBean, BindingResult result, 
								@ModelAttribute("ajaxRequest") boolean ajaxRequest, 
								Model model, RedirectAttributes redirectAttrs) {
		if (result.hasErrors()) {
			return null;
		}
		// Typically you would save to a db and clear the "form" attribute from the session 
		// via SessionStatus.setCompleted(). For the demo we leave it in the session.
		String message = "Form submitted successfully.  Bound " + formBean;
		// Success response handling
		if (ajaxRequest) {
			// prepare model for rendering success message in this request
			model.addAttribute("message", message);
			return null;
		} else {
			// store a success message for rendering on the next request after redirect
			// redirect back to the form to render the success message along with newly bound values
			redirectAttrs.addFlashAttribute("message", message);
			return "redirect:/form";			
		}
	}
	
}

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

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

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

Java代码 复制代码  收藏代码
  1. @SessionAttributes("formBean")  
@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. }  
	@ModelAttribute
	public void ajaxAttribute(WebRequest request, Model model) {
		model.addAttribute("ajaxRequest", AjaxUtils.isAjaxRequest(request));
	}

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

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

访问"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. }  
	@RequestMapping(method=RequestMethod.POST)
	public String processSubmit(@Valid FormBean formBean, BindingResult result, 
								@ModelAttribute("ajaxRequest") boolean ajaxRequest, 
								Model model, RedirectAttributes redirectAttrs) {
		if (result.hasErrors()) {
			return null;
		}
		// Typically you would save to a db and clear the "form" attribute from the session 
		// via SessionStatus.setCompleted(). For the demo we leave it in the session.
		String message = "Form submitted successfully.  Bound " + formBean;
		// Success response handling
		if (ajaxRequest) {
			// prepare model for rendering success message in this request
			model.addAttribute("message", message);
			return null;
		} else {
			// store a success message for rendering on the next request after redirect
			// redirect back to the form to render the success message along with newly bound values
			redirectAttrs.addFlashAttribute("message", message);
			return "redirect:/form";			
		}
	}

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>  
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://www.springframework.org/tags" prefix="s" %>
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form" %>
<%@ page session="false" %>
<c:if test="${!ajaxRequest}">
<html>
<head>
	<title>forms | mvc-showcase</title>
	<link href="<c:url value="/resources/form.css" />" rel="stylesheet"  type="text/css" />		
	<script type="text/javascript" src="<c:url value="/resources/jquery/1.6/jquery.js" />"></script>
</head>
<body>
</c:if>
	<div id="formsContent">
		<h2>Forms</h2>
		<p>
			See the <code>org.springframework.samples.mvc.form</code> package for the @Controller code	
		</p>
		<form:form id="form" method="post" modelAttribute="formBean" cssClass="cleanform">
			<div class="header">
		  		<h2>Form</h2>
		  		<c:if test="${not empty message}">
					<div id="message" class="success">${message}</div>	
		  		</c:if>
		  		<s:bind path="*">
		  			<c:if test="${status.error}">
				  		<div id="message" class="error">Form has errors</div>
		  			</c:if>
		  		</s:bind>
			</div>
		  	<fieldset>
		  		<legend>Personal Info</legend>
		  		<form:label path="name">
		  			Name <form:errors path="name" cssClass="error" />
		 		</form:label>
		  		<form:input path="name" />
	
		  		<form:label path="age">
		  			Age <form:errors path="age" cssClass="error" />
		 		</form:label>
		  		<form:input path="age" />
		  		
		  		<form:label path="birthDate">
		  			Birth Date (in form yyyy-mm-dd) <form:errors path="birthDate" cssClass="error" />
		 		</form:label>
		  		<form:input path="birthDate" />
		  		 
		  		<form:label path="phone">
		  			Phone (in form (###) ###-####) <form:errors path="phone" cssClass="error" />
		  		</form:label>
		  		<form:input path="phone" />
	
		  		<form:label path="currency">
		  			Currency (in form $#.##) <form:errors path="currency" cssClass="error" />
		  		</form:label>
		  		<form:input path="currency" />
	
		  		<form:label path="percent">
		  			Percentage (in form ##%) <form:errors path="percent" cssClass="error" />
		  		</form:label>
		  		<form:input path="percent" />
	
		  	</fieldset>
	
			<fieldset>
				<legend>Inquiry</legend>
				<form:label path="inquiry">
					Type (select one)
				</form:label>
				<form:select path="inquiry">
					<form:option value="comment">Comment</form:option>
					<form:option value="feedback">Feedback</form:option>
					<form:option value="suggestion">Suggestion</form:option>
				</form:select>
				
		  		<form:label path="inquiryDetails">
		  			Details
		  		</form:label>
		  		<form:textarea path="inquiryDetails" />
		  	</fieldset>
	
			<fieldset class="checkbox">
				<legend>Request Additional Info</legend>
				<label><form:checkbox path="additionalInfo[mvc]" value="true" />on Spring MVC</label>
				<label><form:checkbox path="additionalInfo[java]" value="true" />on Java (4-ever)</label>				
			</fieldset>
		  		  	
			<fieldset class="radio">
				<legend>Subscribe to Newsletter?</legend>
				<label><form:radiobutton path="subscribeNewsletter" value="true" />Yes</label>
				<label><form:radiobutton path="subscribeNewsletter" value="false" /> No</label>
			</fieldset>
	
			<p><button type="submit">Submit</button></p>
		</form:form>
		<script type="text/javascript">
			$(document).ready(function() {
				$("#form").submit(function() {  
					$.post($(this).attr("action"), $(this).serialize(), function(html) {
						$("#formsContent").replaceWith(html);
						$('html, body').animate({ scrollTop: $("#message").offset().top }, 500);
					});
					return false;  
				});			
			});
		</script>
	</div>
<c:if test="${!ajaxRequest}">
</body>
</html>
</c:if>

猜你喜欢

转载自zxb1985.iteye.com/blog/1814101
今日推荐