MemberController.java
3.29 KB
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
/**
* Copyright © 2015-2018 ODM All rights reserved.
*/
package com.pudp.bae.controller;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import com.pudp.bae.base.BaseMultiController;
import com.pudp.bae.model.Member;
import com.pudp.bae.service.MemberService;
@Controller
@RequestMapping(value = "/member")
public class MemberController extends BaseMultiController {
@Autowired
private MemberService memberService;
public void setMemberService(MemberService memberService) {
this.memberService = memberService;
}
@RequestMapping(value = { "/add", "/add.html" }, method = { RequestMethod.GET })
public ModelAndView add(HttpServletRequest request, HttpServletResponse response) {
Map<String, Object> map = new HashMap<String, Object>();
Member member = new Member();
member.setId("1");
member.setNickname("guoxiaoming");
this.memberService.add(member);
return toView("add", map);
}
@RequestMapping(value = { "/add", "/add.html" }, method = { RequestMethod.POST })
public ModelAndView addMember(HttpServletRequest request, HttpServletResponse response,
@ModelAttribute("member") Member member) {
Map<String, Object> map = new HashMap<String, Object>();
System.out.println(member);
map.put("message", "成功添加数据到库," + member);
this.memberService.add(member);
return toView("message", map);
}
@RequestMapping(value = { "/{id:\\d+}/query", "/{id:\\d+}/query.html" }, method = {
RequestMethod.GET, RequestMethod.POST })
public ModelAndView queryMember(HttpServletRequest request,
HttpServletResponse response, @PathVariable("id") String id) {
Map<String, Object> map = new HashMap<String, Object>();
System.out.println(id);
Member member = this.memberService.get(id);
if (null != member) {
map.put("message", "查询Id=" + id + "的用户名为:" + member.getNickname());
} else {
map.put("message", "没有查询到与Id=" + id + "相关的数据");
}
return toView("message", map);
}
@RequestMapping(value = { "/{id:\\d+}/delete", "/{id:\\d+}/delete.html" }, method = {
RequestMethod.GET, RequestMethod.POST })
public ModelAndView deleteMember(HttpServletRequest request,
HttpServletResponse response, @PathVariable("id") String id) {
Map<String, Object> map = new HashMap<String, Object>();
try {
this.memberService.delete(id);
map.put("message", "删除Id为" + id + "的用户成功.");
} catch (Exception e) {
e.printStackTrace();
map.put("message", "删除Id为" + id + "的用户失败, " + e.getMessage());
}
return toView("message", map);
}
}