spring MVC之注解开发控制器(二)
开发表单控制器
在传统的Spring MVC开发方法中,是通过扩展SimpleFormController类来创建简单的表单控制器。这样就定义了基本的表单处理流程,并允许通过覆盖几个生命周期方法来定制流程。在基于注解的Spring MVC开发方法中,可以利用注解模拟表单处理流程.
在基于注解的方法中,添加了@Controller注解的基本控制器类也可以处理表单。你首先要做的就是通@RequestMapping
注解将URL模式映射到这个控制器类中。对于处理表单的控制器,必须提供两个重要的方法。一个是用来响应HTTP GET请求,呈现表单的。另一个是为HTTP POST请求处理表单提交的。这些方法的名称可以是任意的,但是必须利用@RequestMapping注解与HTTP方法关联起来。
|
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50 |
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()方法相对应。
请注意,这个控制器的成功视图是一个重定向到预订成功页面的视图。你可以为该视图创建另一个基于注解的控制器。
|
1
2
3
4
5
6
7
8
9
10 |
package
com.apress.springrecipes.court.web;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;@Controllerpublic class ReservationSuccessController{ @RequestMapping("/reservationSuccess.htm") public
String reservationSuccess(){ return
"reservationSuccess"; }} |
最后,在这个表单控制器生效之前,必须定义一具验证器实例,让Spring进行自动装配。
|
1
2 |
<bean id="reservationValidator" class="com.apress.springrecipes.court.domain.ReservationValidator"/> |
为了重用,可以从每个控制器类中提取绑定器初始化任务,形成绑定初始器。
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 |
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指定这个绑定初始器,以便所有的处理程序方法都可以共用相同的属性编辑器。
|
1
2
3
4
5 |
<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之注解开发控制器(二),布布扣,bubuko.com
原文:http://www.cnblogs.com/gxbk629/p/3632035.html