spring MVC之注解开发控制器(二)
2014-03-29 01:17
465 查看
开发表单控制器
在传统的Spring MVC开发方法中,是通过扩展SimpleFormController类来创建简单的表单控制器。这样就定义了基本的表单处理流程,并允许通过覆盖几个生命周期方法来定制流程。在基于注解的Spring MVC开发方法中,可以利用注解模拟表单处理流程.
在基于注解的方法中,添加了@Controller注解的基本控制器类也可以处理表单。你首先要做的就是通@RequestMapping
注解将URL模式映射到这个控制器类中。对于处理表单的控制器,必须提供两个重要的方法。一个是用来响应HTTP GET请求,呈现表单的。另一个是为HTTP POST请求处理表单提交的。这些方法的名称可以是任意的,但是必须利用@RequestMapping注解与HTTP方法关联起来。
Java代码
与HTTP GET方法相关联的setupForm()方法,对应于SimpleFormController的formBackingObject()方法,它将初始化用于绑定的命令对象。在这个方法中,为了初始化命令对象,可以利用@RequestParam注解获取任何请求参数。然后,你亲自创建命令对象,并将它保存到模型属性中。模型属性的名称实际上是可以在jsp文件中访问到的命令对象名称。如果想要将命令对象保存在会话中,就像启用SimpleFormController的sessionForm属性一样,可以将@SessionAttributes注解应用到控制器类中,并指定要保存在会话中的模型属性的名称。
与HTTP POST方法相关联的processSubmit()方法,对应于SimpleFormController的onSubmit()方法。在这个方法中,可以利用@ModelAttribute注解,从模型中得到命令对象。在被应用到方法参数时,@ModelAttribute注解会将模型属性绑定到方法参数上。与SimpleFormController的onSubmit()方法不同,你必须亲自执行表单验证,并决定是呈现
表单视图琮是成功视图。表单提交处理成功之后,调用SessionStatus的setComplete()方法,从会话中清除命令对象.当被应用到像populateSportTypes()这样的方法中时,@ModelAttribute注解会将引用数据填充到模型中。这样做的效果与覆盖SimpleFormController的referenceData()方法相同。
添加了@InitBinder注解的initBinder()方法将定制的属性编辑器注册到了绑定器对象中。它与SimpleFormController
的ininBinder()方法相对应。
请注意,这个控制器的成功视图是一个重定向到预订成功页面的视图。你可以为该视图创建另一个基于注解的控制器。
Java代码
最后,在这个表单控制器生效之前,必须定义一具验证器实例,让Spring进行自动装配。
Xml代码
为了重用,可以从每个控制器类中提取绑定器初始化任务,形成绑定初始器。
Java代码
然后为AnnotationMethodHandlerAdapter指定这个绑定初始器,以便所有的处理程序方法都可以共用相同的属性编辑器。
Java代码
现在可以删除ReservationFormController中的initBinder()方法,因为绑定喊叫已经能在每个基于注解的控制器中共享了。
原文地址:http://fhd001.iteye.com/blog/1136725
如需转载收藏请注明出处,尊重他人的劳动成果!
在传统的Spring MVC开发方法中,是通过扩展SimpleFormController类来创建简单的表单控制器。这样就定义了基本的表单处理流程,并允许通过覆盖几个生命周期方法来定制流程。在基于注解的Spring MVC开发方法中,可以利用注解模拟表单处理流程.
在基于注解的方法中,添加了@Controller注解的基本控制器类也可以处理表单。你首先要做的就是通@RequestMapping
注解将URL模式映射到这个控制器类中。对于处理表单的控制器,必须提供两个重要的方法。一个是用来响应HTTP GET请求,呈现表单的。另一个是为HTTP POST请求处理表单提交的。这些方法的名称可以是任意的,但是必须利用@RequestMapping注解与HTTP方法关联起来。
Java代码
package com.apress.springrecipes.court.web; ... import org.springframework.beans.factory.annotation.Autowired; ... @Controller @RequestMapping("/reservationForm.htm") @SessionAttributes("reservation") public class ReservationFormController{ private ReservationService reservationService; private ReservationValicator validator; @Autowired public ReservationFormController(ReservationService reservationService, ReservationValidator validator){ this.reservationService = reservationService; this.validator = validator; } @InitBinder public void initBinder(WebDataBinder binder){ SimpleDateFormat dateFormat = new SimpleDateFormat("yyy-MM-dd"); dateFormat.setLenient(false); binder.registerCustomEditor(Date.class,new CustomDateEditor( dateFormat,true)); binder.registerCustomEditor(SportType.class,new SportTypeEditor( reservationService)); } @ModelAttribute("sportTypes") public List<SportType> populateSportTypes(){ return reservationService.getAllSportTypes(); } @RequestMapping(method=RequestMethod.GET) public String setupForm(@RequestParam(required=false,value="username") String username,ModelMap model){ Reservation reservation = new Reservation(); reservation.setPlayer(new Player(username,null)); model.addAttribute("reservation",reservation); return "reservationForm"; } @RequestMapping(method=RequestMethod.POST) public String processSubmit(@ModelAttribute("reservation")Reservation reservation, BindingResult result,SessionStatus status){ validator.validate(reservation,result); if(result.hasErrors()){ return "reservationForm"; }else{ reservationService.make(reservation); status.setComplete(); return "redirect:reservationSuccess.htm"; } } }
与HTTP GET方法相关联的setupForm()方法,对应于SimpleFormController的formBackingObject()方法,它将初始化用于绑定的命令对象。在这个方法中,为了初始化命令对象,可以利用@RequestParam注解获取任何请求参数。然后,你亲自创建命令对象,并将它保存到模型属性中。模型属性的名称实际上是可以在jsp文件中访问到的命令对象名称。如果想要将命令对象保存在会话中,就像启用SimpleFormController的sessionForm属性一样,可以将@SessionAttributes注解应用到控制器类中,并指定要保存在会话中的模型属性的名称。
与HTTP POST方法相关联的processSubmit()方法,对应于SimpleFormController的onSubmit()方法。在这个方法中,可以利用@ModelAttribute注解,从模型中得到命令对象。在被应用到方法参数时,@ModelAttribute注解会将模型属性绑定到方法参数上。与SimpleFormController的onSubmit()方法不同,你必须亲自执行表单验证,并决定是呈现
表单视图琮是成功视图。表单提交处理成功之后,调用SessionStatus的setComplete()方法,从会话中清除命令对象.当被应用到像populateSportTypes()这样的方法中时,@ModelAttribute注解会将引用数据填充到模型中。这样做的效果与覆盖SimpleFormController的referenceData()方法相同。
添加了@InitBinder注解的initBinder()方法将定制的属性编辑器注册到了绑定器对象中。它与SimpleFormController
的ininBinder()方法相对应。
请注意,这个控制器的成功视图是一个重定向到预订成功页面的视图。你可以为该视图创建另一个基于注解的控制器。
Java代码
package com.apress.springrecipes.court.web; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; @Controller public class ReservationSuccessController{ @RequestMapping("/reservationSuccess.htm") public String reservationSuccess(){ return "reservationSuccess"; } }
最后,在这个表单控制器生效之前,必须定义一具验证器实例,让Spring进行自动装配。
Xml代码
<bean id="reservationValidator" class="com.apress.springrecipes.court.domain.ReservationValidator"/>
为了重用,可以从每个控制器类中提取绑定器初始化任务,形成绑定初始器。
Java代码
package com.apress.springrecipes.court.web; ... import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.propertyeditors.CustomDateEditor; import org.springframework.web.bind.WebDataBinder; import org.springframework.web.bind.support.WebBindingInitializer; import org.springframework.web.context.request.WebRequest; public class ReservationBindingInitializer implements WebBindingInitializer{ private ReservationService reservationService; @Autowired public ReservationBindingInitializer(ReservationService reservationService){ this.reservationService = reservationService; } public void initBinder(WebDatabinder binder,WebRequest request){ SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); dateFormat.setLenient(false); binder.registerCustomEditor(Date.class,new CustomDateEditor( dateFormat,true)); binder.registerCustomEditor(SportTupe.class,new SportTypeEditor( reservationService)); } }
然后为AnnotationMethodHandlerAdapter指定这个绑定初始器,以便所有的处理程序方法都可以共用相同的属性编辑器。
Java代码
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"> <property name="webBindingInitializer"> <bean class="com.apress.springrecipes.court.web.ReservationBindingInitializer"/> </property> </bean>
现在可以删除ReservationFormController中的initBinder()方法,因为绑定喊叫已经能在每个基于注解的控制器中共享了。
原文地址:http://fhd001.iteye.com/blog/1136725
如需转载收藏请注明出处,尊重他人的劳动成果!
相关文章推荐
- Spring MVC 4.1.3 + MyBatis 零基础搭建Web开发框架(注解模式哦)
- 04springMVC结构,mvc模式,spring-mvc流程,spring-mvc的第一个例子,三种handlerMapping,几种控制器,springmvc基于注解的开发,文件上传,拦截器,s
- Spring MVC —— 基于注解的控制器
- spring mvc系列文章 - springmvc spring mybatis ibatis freemark整合开发(2.0版:基于注解)
- Controller注解形式做Spring MVC开发
- (*****)Spring MVC 4.1.3 + MyBatis 零基础搭建Web开发框架(注解模式哦)
- Spring MVC---基于注解的控制器
- spring boot之Spring Mvc开发和常用注解说明
- Spring MVC 4.1.3 + MyBatis零基础搭建Web开发框架(注解模式)
- 基于注解的Spring MVC控制器
- Spring MVC使用注解开发
- Spring MVC(学习笔记五)--控制器的注解(四) -之全局异常处理操作
- Spring MVC 4.1.3 + MyBatis 零基础搭建Web开发框架(注解模式哦)
- Spring MVC 系列(二)——基于注解的开发
- Spring MVC---基于注解的控制器
- 【笔记】Spring MVC学习指南(四)基于注解的控制器
- Spring MVC-04循序渐进之基于注解的控制器
- 学习Spring MVC,关于注解的Spring MVC,关于控制器的Spring MVC,学习Spring,简单Spring MVC实例
- Spring MVC 注解开发备忘录
- 使用注解开发Spring mvc