Spring MVC3构建Web应用第四步
现在我们开始设计控制器,鼠标右击src目录,选择新建-包,如下图:
并将包的名命名为paul.sydney.controller,在这个包中,新建立一个类,命名为PersonDisplay,将代码修改如下:
1 package paul.sydney.controller;
2
3import org.springframework.beans.factory.annotation.Autowired;
4import org.springframework.stereotype.Controller;
5import org.springframework.ui.ModelMap;
6import org.springframework.web.bind.annotation.RequestMapping;
7import org.springframework.web.bind.annotation.RequestParam;
8
9import paul.sydney.service.DummyService;
10
11/**
12 * PersonDisplay class, display controller for the 'personDisplay.jsp'
13 * Copyright : adobocode.com , 2010
14 * @author Paul Sydney Orozco | xtrycatchx@gmail.com
15 *
16 */
17@Controller
18public class PersonDisplay {
19
20 private final DummyService dummyService;
21
22 @Autowired
23 public PersonDisplay(DummyService dummyService) {
24 this.dummyService = dummyService;
25 }
26
27 @RequestMapping("/personDisplay.htm")
28 public ModelMap defaultHandler() {
29 return new ModelMap("personList", this.dummyService.getDummyList());
30 }
31 }
2
3import org.springframework.beans.factory.annotation.Autowired;
4import org.springframework.stereotype.Controller;
5import org.springframework.ui.ModelMap;
6import org.springframework.web.bind.annotation.RequestMapping;
7import org.springframework.web.bind.annotation.RequestParam;
8
9import paul.sydney.service.DummyService;
10
11/**
12 * PersonDisplay class, display controller for the 'personDisplay.jsp'
13 * Copyright : adobocode.com , 2010
14 * @author Paul Sydney Orozco | xtrycatchx@gmail.com
15 *
16 */
17@Controller
18public class PersonDisplay {
19
20 private final DummyService dummyService;
21
22 @Autowired
23 public PersonDisplay(DummyService dummyService) {
24 this.dummyService = dummyService;
25 }
26
27 @RequestMapping("/personDisplay.htm")
28 public ModelMap defaultHandler() {
29 return new ModelMap("personList", this.dummyService.getDummyList());
30 }
31 }
这里,使用注解@Controller表示这个是一个控制器,而 @RequestMapping("/personDisplay.htm")则表示对于personDisplay.htm这样的请求,使用defaultHandler这个方法去处理。而在defaultHandler中,又调用了逻辑服务层的
dummyService.getDummyList()方法获得人员列表,最后把列表存放到一个ModelMap中去(可以先理解为Map的一种数据结构)。
同样地,在paul.sydney.controller包中,新建一个类PersonForm,并将代码修改如下:
1package paul.sydney.controller;
2
3import org.springframework.beans.factory.annotation.Autowired;
4import org.springframework.stereotype.Controller;
5import org.springframework.ui.ModelMap;
6import org.springframework.validation.BindingResult;
7import org.springframework.web.bind.annotation.ModelAttribute;
8import org.springframework.web.bind.annotation.RequestMapping;
9import org.springframework.web.bind.annotation.RequestMethod;
10import org.springframework.web.bind.annotation.RequestParam;
11import org.springframework.web.bind.annotation.SessionAttributes;
12import org.springframework.web.bind.support.SessionStatus;
13
14import paul.sydney.model.Person;
15import paul.sydney.service.DummyService;
16@Controller
17@RequestMapping("/personForm.htm")
18@SessionAttributes("person")
19public class PersonForm {
20
21 private final DummyService dummyService;
22
23 @Autowired
24 public PersonForm(DummyService dummyService) {
25 this.dummyService = dummyService;
26 }
27
28 @RequestMapping(method = RequestMethod.GET)
29 public String setupForm(@RequestParam("personId") int id, ModelMap model) {
30 Person person = this.dummyService.retrievePerson(id);
31 model.addAttribute("person", person);
32 return "personForm";
33 }
34
35 @RequestMapping(method = RequestMethod.POST)
36 public String processSubmit(@ModelAttribute("person") Person person, BindingResult result, SessionStatus status) {
37 this.dummyService.savePerson(person);
38 status.setComplete();
39 return "redirect:personDisplay.htm";
40 }
41}
2
3import org.springframework.beans.factory.annotation.Autowired;
4import org.springframework.stereotype.Controller;
5import org.springframework.ui.ModelMap;
6import org.springframework.validation.BindingResult;
7import org.springframework.web.bind.annotation.ModelAttribute;
8import org.springframework.web.bind.annotation.RequestMapping;
9import org.springframework.web.bind.annotation.RequestMethod;
10import org.springframework.web.bind.annotation.RequestParam;
11import org.springframework.web.bind.annotation.SessionAttributes;
12import org.springframework.web.bind.support.SessionStatus;
13
14import paul.sydney.model.Person;
15import paul.sydney.service.DummyService;
16@Controller
17@RequestMapping("/personForm.htm")
18@SessionAttributes("person")
19public class PersonForm {
20
21 private final DummyService dummyService;
22
23 @Autowired
24 public PersonForm(DummyService dummyService) {
25 this.dummyService = dummyService;
26 }
27
28 @RequestMapping(method = RequestMethod.GET)
29 public String setupForm(@RequestParam("personId") int id, ModelMap model) {
30 Person person = this.dummyService.retrievePerson(id);
31 model.addAttribute("person", person);
32 return "personForm";
33 }
34
35 @RequestMapping(method = RequestMethod.POST)
36 public String processSubmit(@ModelAttribute("person") Person person, BindingResult result, SessionStatus status) {
37 this.dummyService.savePerson(person);
38 status.setComplete();
39 return "redirect:personDisplay.htm";
40 }
41}
这里,首先通过依赖注入,注入了服务层逻辑dummyService,然后在setupForm中,根据传入的参数personId,通过服务层逻辑dummyService找出这个人,然后将其保存到一个Model中去,返回给视图层personForm.jsp显示;而processSubmit是调用服务层逻辑保存用户的资料(通过this.dummyService.savePerson(person)实现),最后使用redirect跳转到personDisplay.htm。