这是一个WebProject,用到的是spring-framework-3.1.1.RELEASE
首先是web.xml
<? 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"> |
08 | < servlet-name >user</ servlet-name > |
09 | < servlet-class >org.springframework.web.servlet.DispatcherServlet</ servlet-class > |
10 | < load-on-startup >1</ load-on-startup > |
13 | < servlet-name >user</ servlet-name > |
14 | < url-pattern >/</ url-pattern > |
18 | < filter-name >SpringCharacterEncodingFilter</ filter-name > |
19 | < filter-class >org.springframework.web.filter.CharacterEncodingFilter</ filter-class > |
21 | < param-name >encoding</ param-name > |
22 | < param-value >UTF-8</ param-value > |
26 | < filter-name >SpringCharacterEncodingFilter</ filter-name > |
27 | < url-pattern >/*</ url-pattern > |
然后是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" /> |
14 | < mvc:annotation-driven /> |
16 | < bean class = "org.springframework.web.servlet.view.InternalResourceViewResolver" > |
17 | < property name = "prefix" value = "/WEB-INF/jsp/" /> |
18 | < property name = "suffix" value = ".jsp" /> |
用来添加用户信息的//WEB-INF//jsp/user/add.jsp
01 | <%@ page language="java" pageEncoding="UTF-8"%> |
02 | <%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%> |
04 | <%-- 这里并没有指定action="",说明表单是提交给自己的,说白了就是"哪来的回哪儿去 --%> |
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 = "添加新用户" /> |
用来列出所有用户信息的//WEB-INF//jsp/user/list.jsp
01 | <%@ page language="java" pageEncoding="UTF-8"%> |
02 | <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> |
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 > |
11 | < a href="<%=request.getContextPath()%>/user/add">继续添加用户</ a > |
用来显示单个用户信息的//WEB-INF//jsp/user/show.jsp
1 | <%@ page language="java" pageEncoding="UTF-8"%> |
2 | ${user.username}----${user.nickname}----${user.password}----${user.email} |
5 | < a href="<%=request.getContextPath()%>/user/add">继续添加用户</ a > |
用来更新单个用户信息的//WEB-INF//jsp/user/update.jsp
1 | <%@ page language="java" pageEncoding="UTF-8"%> |
2 | <%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%> |
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 = "更新用户信息" /> |
下面是用户的实体类User.java
01 | package com.jadyer.model; |
05 | * @author http://blog.csdn.net/jadyer |
06 | * @create May 12, 2012 1:24:43 AM |
09 | private String username; |
10 | private String nickname; |
11 | private String password; |
15 | public User(String username, String nickname, String password, String email) { |
16 | this .username = username; |
17 | this .nickname = nickname; |
18 | this .password = password; |
22 | public String getUsername() { |
25 | public void setUsername(String username) { |
26 | this .username = username; |
28 | public String getNickname() { |
31 | public void setNickname(String nickname) { |
32 | this .nickname = nickname; |
34 | public String getPassword() { |
37 | public void setPassword(String password) { |
38 | this .password = password; |
40 | public String getEmail() { |
43 | public void setEmail(String email) { |
最后是核心的UserController.java
01 | package com.jadyer.controller; |
03 | import java.util.HashMap; |
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; |
12 | import com.jadyer.model.User; |
16 | * @author http://blog.csdn.net/jadyer |
17 | * @create May 12, 2012 1:26:21 AM |
20 | @RequestMapping ( "/user" ) |
21 | public class UserController { |
22 | private final static Map<String,User> users = new HashMap<String,User>(); |
25 | public UserController(){ |
26 | users.put( "张起灵" , new User( "张起灵" , "闷油瓶" , "02200059" , "menyouping@yeah.net" )); |
| users.put( "李寻欢" , new User( "李寻欢" , "李探花" , "08866659" , "lixunhuan@gulong.cn" )); |
| users.put( "拓拔野" , new User( "拓拔野" , "搜神记" , "05577759" , "tuobaye@manhuang.cc" )); |
| users.put( "孙悟空" , new User( "孙悟空" , "美猴王" , "03311159" , "sunhouzi@xiyouji.zh" )); |
34 | * @see 访问/user/add时,GET请求就执行addUser(Model model)方法,POST请求就执行addUser(User user)方法 |
36 | @RequestMapping (value= "/add" , method=RequestMethod.GET) |
37 | public String addUser(Model model){ |
42 | model.addAttribute( new User()); |
45 | @RequestMapping (value= "/add" , method=RequestMethod.POST) |
46 | public String addUser(User user){ |
47 | users.put(user.getUsername(), user); |
48 | return "redirect:/user/list" ; |
54 | @RequestMapping ( "/list" ) |
55 | public String list(Model model){ |
56 | model.addAttribute( "users" , users); |
62 | * @see 访问该方法的路径就应该是"/user/具体的用户名" |
63 | * @see 这里value="/{username}"的写法,需要格外注意一下,它是一个路径变量,此时用来接收前台的一个资源 |
64 | * @see 这时value="/{username}"就会到方法参数中找@PathVariable String username,并将路径变量值传给username参数 |
66 | @RequestMapping (value= "/{myname}" , method=RequestMethod.GET) |
67 | public String show( @PathVariable String myname, Model model){ |
68 | model.addAttribute(users.get(myname)); |
74 | * @see 访问该方法的路径就应该是"/user/具体的用户名/update" |
76 | @RequestMapping (value= "/{myname}/update" , method=RequestMethod.GET) |
77 | public String update( @PathVariable String myname, Model model){ |
78 | model.addAttribute(users.get(myname)); |
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" ; |
90 | @RequestMapping (value= "/{myname}/delete" , method=RequestMethod.GET) |
91 | public String delete( @PathVariable String myname){ |
93 | return "redirect:/user/list" ; |