博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Spring 表单处理
阅读量:6703 次
发布时间:2019-06-25

本文共 9643 字,大约阅读时间需要 32 分钟。

1. SimpleFormController vs @Controller

In XML-based Spring MVC web application, you create a form controller by extending the SimpleFormController class.

In annotation-based, you can use @Controller instead.

SimpleFormController

public class CustomerController extends SimpleFormController{
//...}

Annotation

@Controller@RequestMapping("/customer.htm")public class CustomerController{
//...}
 

2. formBackingObject() vs RequestMethod.GET

In SimpleFormController, you can initialize the command object for binding in the formBackingObject() method. In annotation-based, you can do the same by annotated the method name with @RequestMapping(method = RequestMethod.GET).

SimpleFormController

@Override	protected Object formBackingObject(HttpServletRequest request)		throws Exception {
  Customer cust = new Customer(); //Make "Spring MVC" as default checked value cust.setFavFramework(new String []{
"Spring MVC"});  return cust; }

Annotation

@RequestMapping(method = RequestMethod.GET)	public String initForm(ModelMap model){
  Customer cust = new Customer(); //Make "Spring MVC" as default checked value cust.setFavFramework(new String []{
"Spring MVC"});  //command object model.addAttribute("customer", cust);  //return form view return "CustomerForm"; }

3. onSubmit() vs RequestMethod.POST

In SimpleFormController, the form submission is handle by the onSubmit() method. In annotation-based, you can do the same by annotated the method name with @RequestMapping(method = RequestMethod.POST).

SimpleFormController

@Override	protected ModelAndView onSubmit(HttpServletRequest request,		HttpServletResponse response, Object command, BindException errors)		throws Exception {
  Customer customer = (Customer)command; return new ModelAndView("CustomerSuccess");  }

Annotation

@RequestMapping(method = RequestMethod.POST)	public String processSubmit(		@ModelAttribute("customer") Customer customer,		BindingResult result, SessionStatus status) {
  //clear the command object from the session status.setComplete();   //return form success view return "CustomerSuccess";  }

4. referenceData() vs @ModelAttribute

In SimpleFormController, usually you put the reference data in model via referenceData() method, so that the form view can access it. In annotation-based, you can do the same by annotated the method name with @ModelAttribute.

SimpleFormController

@Override	protected Map referenceData(HttpServletRequest request) throws Exception {
  Map referenceData = new HashMap();  //Data referencing for web framework checkboxes List
webFrameworkList = new ArrayList
(); webFrameworkList.add("Spring MVC"); webFrameworkList.add("Struts 1"); webFrameworkList.add("Struts 2"); webFrameworkList.add("JSF"); webFrameworkList.add("Apache Wicket"); referenceData.put("webFrameworkList", webFrameworkList);  return referenceData; }

Spring’s form

Annotation

@ModelAttribute("webFrameworkList")	public List
populateWebFrameworkList() {
  //Data referencing for web framework checkboxes List
webFrameworkList = new ArrayList
(); webFrameworkList.add("Spring MVC"); webFrameworkList.add("Struts 1"); webFrameworkList.add("Struts 2"); webFrameworkList.add("JSF"); webFrameworkList.add("Apache Wicket");  return webFrameworkList; }

Spring’s form

5. initBinder() vs @InitBinder

In SimpleFormController, you define the binding or register the custom property editor via initBinder() method. In annotation-based, you can do the same by annotated the method name with @InitBinder.

SimpleFormController

protected void initBinder(HttpServletRequest request,		ServletRequestDataBinder binder) throws Exception {
  SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true)); }

Annotation

@InitBinder	public void initBinder(WebDataBinder binder) {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");  binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true)); }

From Validation

In SimpleFormController, you have to register and map the validator class to the controller class via XML bean configuration file, and the validation checking and work flows will be executed automatically.

In annotation-based, you have to explicitly execute the validator and define the validation flow in the @Controller class manually. See the different :

SimpleFormController

 

Annotation

@Controller@RequestMapping("/customer.htm")public class CustomerController{
  CustomerValidator customerValidator;  @Autowired public CustomerController(CustomerValidator customerValidator){
this.customerValidator = customerValidator; }  @RequestMapping(method = RequestMethod.POST) public String processSubmit( @ModelAttribute("customer") Customer customer, BindingResult result, SessionStatus status) {
  customerValidator.validate(customer, result);  if (result.hasErrors()) {
//if validator failed return "CustomerForm"; } else {
status.setComplete(); //form success return "CustomerSuccess"; } } //...

Full Example

See a complete @Controller example.

package com.mkyong.customer.controller; import java.sql.Date;import java.text.SimpleDateFormat;import java.util.ArrayList;import java.util.LinkedHashMap;import java.util.List;import java.util.Map; import org.springframework.beans.factory.annotation.Autowired;import org.springframework.beans.propertyeditors.CustomDateEditor;import org.springframework.stereotype.Controller;import org.springframework.ui.ModelMap;import org.springframework.validation.BindingResult;import org.springframework.web.bind.WebDataBinder;import org.springframework.web.bind.annotation.InitBinder;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.support.SessionStatus; import com.mkyong.customer.model.Customer;import com.mkyong.customer.validator.CustomerValidator; @Controller@RequestMapping("/customer.htm")public class CustomerController{
  CustomerValidator customerValidator;  @Autowired public CustomerController(CustomerValidator customerValidator){
this.customerValidator = customerValidator; }  @RequestMapping(method = RequestMethod.POST) public String processSubmit( @ModelAttribute("customer") Customer customer, BindingResult result, SessionStatus status) {
  customerValidator.validate(customer, result);  if (result.hasErrors()) {
//if validator failed return "CustomerForm"; } else {
status.setComplete(); //form success return "CustomerSuccess"; } }  @RequestMapping(method = RequestMethod.GET) public String initForm(ModelMap model){
  Customer cust = new Customer(); //Make "Spring MVC" as default checked value cust.setFavFramework(new String []{
"Spring MVC"});  //Make "Make" as default radio button selected value cust.setSex("M");  //make "Hibernate" as the default java skills selection cust.setJavaSkills("Hibernate");  //initilize a hidden value cust.setSecretValue("I'm hidden value");  //command object model.addAttribute("customer", cust);  //return form view return "CustomerForm"; }   @ModelAttribute("webFrameworkList") public List
populateWebFrameworkList() {
  //Data referencing for web framework checkboxes List
webFrameworkList = new ArrayList
(); webFrameworkList.add("Spring MVC"); webFrameworkList.add("Struts 1"); webFrameworkList.add("Struts 2"); webFrameworkList.add("JSF"); webFrameworkList.add("Apache Wicket");  return webFrameworkList; }  @InitBinder public void initBinder(WebDataBinder binder) {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");  binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));  }  @ModelAttribute("numberList") public List
populateNumberList() {
  //Data referencing for number radiobuttons List
numberList = new ArrayList
(); numberList.add("Number 1"); numberList.add("Number 2"); numberList.add("Number 3"); numberList.add("Number 4"); numberList.add("Number 5");  return numberList; }  @ModelAttribute("javaSkillsList") public Map
populateJavaSkillList() {   //Data referencing for java skills list box Map
javaSkill = new LinkedHashMap
(); javaSkill.put("Hibernate", "Hibernate"); javaSkill.put("Spring", "Spring"); javaSkill.put("Apache Wicket", "Apache Wicket"); javaSkill.put("Struts", "Struts");  return javaSkill; }  @ModelAttribute("countryList") public Map
populateCountryList() {   //Data referencing for java skills list box Map
country = new LinkedHashMap
(); country.put("US", "United Stated"); country.put("CHINA", "China"); country.put("SG", "Singapore"); country.put("MY", "Malaysia");  return country; }}

To make annotation work, you have to enable the component auto scanning feature in Spring.

 
 
 
 
/WEB-INF/pages/
.jsp

Download Source Code

Download it –   (12KB)

转载地址:http://bbgoo.baihongyu.com/

你可能感兴趣的文章
持续集成与持续部署宝典Part 2:创建持续集成流水线
查看>>
javascript高级程序设计学习之数值转换 |Number(),parseInt(),parseFloat()
查看>>
Angular属性型指令
查看>>
如何处理错误信息 Pricing procedure could not be determined
查看>>
【CentOS 7笔记11】,目录权限,所有者与所有组,隐藏权限#171022
查看>>
Mybatis 详解--- 一级缓存、二级缓存
查看>>
2013 ACM/ICPC Asia Regional Changsha Online - C
查看>>
ACM中java快速入门
查看>>
discuz x2.5插件开发傻瓜图文教程,用demo说话
查看>>
利用HTML中的XML数据岛记录浏览
查看>>
unicode字符、python乱码问题
查看>>
cobbler get-loaders 通过代理下载
查看>>
通过脚本测试ubuntu的源
查看>>
一些不错的网站
查看>>
safari的一些问题
查看>>
面试官问我:平常如何对你的 Java 程序进行调优?
查看>>
Java中对象和引用的理解
查看>>
如何有效抓取SQL Server的BLOCKING信息
查看>>
bash中(),{},(()),[],[[]]的区别
查看>>
Oracle PL/SQL匿名块(三)
查看>>