<? xml version = "1.0" encoding = "UTF-8" ?>
|
02 |
< web-app version = "2.5" |
03 |
xmlns = "http://java.sun.com/xml/ns/javaee" |
04 |
xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance" |
05 |
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
|
06 |
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
|
07 |
< servlet >
|
08 |
< servlet-name >user</ servlet-name >
|
09 |
< servlet-class >org.springframework.web.servlet.DispatcherServlet</ servlet-class >
|
10 |
< load-on-startup >1</ load-on-startup >
|
11 |
</ servlet >
|
12 |
< servlet-mapping >
|
13 |
< servlet-name >user</ servlet-name >
|
14 |
< url-pattern >/</ url-pattern >
|
15 |
</ servlet-mapping >
|
16 |
|
17 |
< filter >
|
18 |
< filter-name >SpringCharacterEncodingFilter</ filter-name >
|
19 |
< filter-class >org.springframework.web.filter.CharacterEncodingFilter</ filter-class >
|
20 |
< init-param >
|
21 |
< param-name >encoding</ param-name >
|
22 |
< param-value >UTF-8</ param-value >
|
23 |
</ init-param >
|
24 |
</ filter >
|
25 |
< filter-mapping >
|
26 |
< filter-name >SpringCharacterEncodingFilter</ filter-name >
|
27 |
< url-pattern >/*</ url-pattern >
|
28 |
</ filter-mapping >
|
29 |
</ web-app >
|
然后是SpringMVC的配置文件user-servlet.xml
01 |
<? xml version = "1.0" encoding = "UTF-8" ?>
|
02 |
< beans xmlns = "http://www.springframework.org/schema/beans" |
03 |
xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance" |
04 |
xmlns:mvc = "http://www.springframework.org/schema/mvc" |
05 |
xmlns:context = "http://www.springframework.org/schema/context" |
06 |
xsi:schemaLocation="http://www.springframework.org/schema/beans
|
07 |
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
|
08 |
http://www.springframework.org/schema/mvc
|
09 |
http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
|
10 |
http://www.springframework.org/schema/context
|
11 |
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
|
12 |
< context:component-scan base-package = "com.jadyer" />
|
13 |
|
14 |
< mvc:annotation-driven />
|
15 |
|
16 |
< bean class = "org.springframework.web.servlet.view.InternalResourceViewResolver" >
|
17 |
< property name = "prefix" value = "/WEB-INF/jsp/" />
|
18 |
< property name = "suffix" value = ".jsp" />
|
19 |
</ bean >
|
20 |
</ beans >
|
用来添加用户信息的//WEB-INF//jsp/user/add.jsp
01 |
<%@ |
02 |
<%@ |
03 |
|
04 |
<%-- |
05 |
|
06 |
< form:form method = "POST" modelAttribute = "user" >
|
07 |
username: < form:input path = "username" />< br />
|
08 |
nickname: < form:input path = "nickname" />< br />
|
09 |
password: < form:password path = "password" />< br />
|
10 |
yourmail: < form:input path = "email" />< br />
|
11 |
< input type = "submit" value = "添加新用户" />
|
12 |
</ form:form >
|
用来列出所有用户信息的//WEB-INF//jsp/user/list.jsp
01 |
<%@ |
02 |
<%@ |
03 |
< c:forEach items = "${users}" var = "user" >
|
04 |
${user.value.username}----${user.value.nickname}----${user.value.password}----${user.value.email}
|
05 |
< a href="<%=request.getContextPath()%>/user/${user.value.username}">查看</ a >
|
06 |
< a href="<%=request.getContextPath()%>/user/${user.value.username}/update">编辑</ a >
|
07 |
< a href="<%=request.getContextPath()%>/user/${user.value.username}/delete">删除</ a >
|
08 |
< br />
|
09 |
</ c:forEach >
|
10 |
< br />
|
11 |
< a href="<%=request.getContextPath()%>/user/add">继续添加用户</ a >
|
用来显示单个用户信息的//WEB-INF//jsp/user/show.jsp
1 |
<%@ |
2 |
${user.username}----${user.nickname}----${user.password}----${user.email} |
3 |
< br />
|
4 |
< br />
|
5 |
< a href="<%=request.getContextPath()%>/user/add">继续添加用户</ a >
|
用来更新单个用户信息的//WEB-INF//jsp/user/update.jsp
1 |
<%@ |
2 |
<%@ |
3 |
< form:form method = "POST" modelAttribute = "user" >
|
4 |
username: < form:input path = "username" />< br />
|
5 |
nickname: < form:input path = "nickname" />< br />
|
6 |
password: < form:password path = "password" />< br />
|
7 |
yourmail: < form:input path = "email" />< br />
|
8 |
< input type = "submit" value = "更新用户信息" />
|
9 |
</ form:form >
|
01 |
package com.jadyer.model;
|
02 |
|
03 |
/** |
04 |
* User
|
05 |
* @author http://blog.csdn.net/jadyer
|
06 |
* @create May 12, 2012 1:24:43 AM
|
07 |
*/ |
08 |
public class User {
|
09 |
private String username;
|
10 |
private String nickname;
|
11 |
private String password;
|
12 |
private String email;
|
13 |
|
14 |
public User() {}
|
15 |
public User(String username, String nickname, String password, String email) {
|
16 |
this .username = username;
|
17 |
this .nickname = nickname;
|
18 |
this .password = password;
|
19 |
this .email = email;
|
20 |
}
|
21 |
|
22 |
public String getUsername() {
|
23 |
return username;
|
24 |
}
|
25 |
public void setUsername(String username) {
|
26 |
this .username = username;
|
27 |
}
|
28 |
public String getNickname() {
|
29 |
return nickname;
|
30 |
}
|
31 |
public void setNickname(String nickname) {
|
32 |
this .nickname = nickname;
|
33 |
}
|
34 |
public String getPassword() {
|
35 |
return password;
|
36 |
}
|
37 |
public void setPassword(String password) {
|
38 |
this .password = password;
|
39 |
}
|
40 |
public String getEmail() {
|
41 |
return email;
|
42 |
}
|
43 |
public void setEmail(String email) {
|
44 |
this .email = email;
|
45 |
}
|
46 |
} |
01 |
package com.jadyer.controller;
|
02 |
|
03 |
import java.util.HashMap;
|
04 |
import java.util.Map;
|
05 |
|
06 |
import org.springframework.stereotype.Controller;
|
07 |
import org.springframework.ui.Model;
|
08 |
import org.springframework.web.bind.annotation.PathVariable;
|
09 |
import org.springframework.web.bind.annotation.RequestMapping;
|
10 |
import org.springframework.web.bind.annotation.RequestMethod;
|
11 |
|
12 |
import com.jadyer.model.User;
|
13 |
|
14 |
/** |
15 |
* UserController
|
16 |
* @author http://blog.csdn.net/jadyer
|
17 |
* @create May 12, 2012 1:26:21 AM
|
18 |
*/ |
19 |
@Controller |
20 |
@RequestMapping ( "/user" )
|
21 |
public class UserController {
|
22 |
private final static Map<String,User> users = new HashMap<String,User>();
|
23 |
|
24 |
//模拟数据源,构造初始数据
|
25 |
public UserController(){
|
26 |
users.put( "张起灵" , new User( "张起灵" , "闷油瓶" , "02200059" , "menyouping@yeah.net" ));
|
users.put( "李寻欢" , new User( "李寻欢" , "李探花" , "08866659" , "lixunhuan@gulong.cn" ));
|
"拓拔野" , new User( "拓拔野" , "搜神记" , "05577759" , "tuobaye@manhuang.cc" ));
|
users.put( "孙悟空" , new User( "孙悟空" , "美猴王" , "03311159" , "sunhouzi@xiyouji.zh" ));
|
30 |
}
|
31 |
|
32 |
/**
|
33 |
* 添加新用户
|
34 |
* @see 访问/user/add时,GET请求就执行addUser(Model model)方法,POST请求就执行addUser(User user)方法
|
35 |
*/ |
36 |
@RequestMapping (value= "/add" , method=RequestMethod.GET)
|
37 |
public String addUser(Model model){
|
38 |
//这里要传给前台一个空对象,否则会报告java.lang.IllegalStateException异常
|
39 |
//异常信息为Neither BindingResult nor plain target object for bean name 'user' available as request attribute
|
40 |
//并且传过去的key值要与前台modelAttribute属性值相同,即model.addAttribute("user", new User());
|
41 |
//我们也可以写成下面这种方式,此时SpringMVC会自动把对象名转换为小写值作为key,即User-->user
|
42 |
model.addAttribute( new User());
|
43 |
return "user/add" ;
|
44 |
}
|
45 |
@RequestMapping (value= "/add" , method=RequestMethod.POST)
|
46 |
public String addUser(User user){ //这里参数中的user就应该与add.jsp中的modelAttribute="user"一致了
|
47 |
users.put(user.getUsername(), user);
|
48 |
return "redirect:/user/list" ;
|
49 |
}
|
50 |
|
51 |
/**
|
52 |
* 列出所有用户信息
|
53 |
*/ |
54 |
@RequestMapping ( "/list" )
|
55 |
public String list(Model model){
|
56 |
model.addAttribute( "users" , users);
|
57 |
return "user/list" ;
|
58 |
}
|
59 |
|
60 |
/**
|
61 |
* 查询用户信息
|
62 |
* @see 访问该方法的路径就应该是"/user/具体的用户名"
|
63 |
* @see 这里value="/{username}"的写法,需要格外注意一下,它是一个路径变量,此时用来接收前台的一个资源
|
64 |
* @see 这时value="/{username}"就会到方法参数中找@PathVariable String username,并将路径变量值传给username参数
|
65 |
*/ |
66 |
@RequestMapping (value= "/{myname}" , method=RequestMethod.GET)
|
67 |
public String show( @PathVariable String myname, Model model){
|
68 |
model.addAttribute(users.get(myname));
|
69 |
return "user/show" ;
|
70 |
}
|
71 |
|
72 |
/**
|
73 |
* 编辑用户信息
|
74 |
* @see 访问该方法的路径就应该是"/user/具体的用户名/update"
|
75 |
*/ |
76 |
@RequestMapping (value= "/{myname}/update" , method=RequestMethod.GET)
|
77 |
public String update( @PathVariable String myname, Model model){
|
78 |
model.addAttribute(users.get(myname));
|
79 |
return "user/update" ;
|
80 |
}
|
81 |
@RequestMapping (value= "/{myname}/update" , method=RequestMethod.POST)
|
82 |
public String update(User user){
|
83 |
users.put(user.getUsername(), user);
|
84 |
return "redirect:/user/list" ; //也可以retun "forward:/user/list",此时浏览器地址栏会有不同
|
85 |
}
|
86 |
|
87 |
/**
|
88 |
* 删除用户信息
|
89 |
*/ |
90 |
@RequestMapping (value= "/{myname}/delete" , method=RequestMethod.GET)
|
91 |
public String delete( @PathVariable String myname){
|
92 |
users.remove(myname);
|
93 |
return "redirect:/user/list" ; //删除完成后显示当前存在的所有用户信息
|
94 |
}
|
95 |
} |