Spring MVC rest

来源:互联网 发布:java小游戏代码大全 编辑:程序博客网 时间:2024/04/29 07:56

列表页面  /user/users

更新页面  /user/20150128/update

删除页面  /user/20150218/delete

添加页面  /user/add

显示详情  /user/张无忌  

@RequestMapping(value="/{username}",method=RequestMethod.GET)   public String show(@PathVariable String username){      return "user/show"; }


一个相对完整的实例,只提供了现实列表功能 和  修改功能,添加删除基本一样,没做处理

@Controller@RequestMapping("/user")public class UserController {private DB db;@RequestMapping(value = "/add", method = RequestMethod.GET)public String add(User user) {return "add";}@RequestMapping(value = "/add", method = RequestMethod.POST)public String add(User user, Model model) {model.addAttribute("user", user);return "/user/users";}@RequestMapping(value = "/users")public String list(Model model) {Map<String, User> maps = db.getUserList();model.addAttribute("maps", maps);return "user/users";}@RequestMapping(value = "/{id}/update", method = RequestMethod.GET)public String update(@PathVariable(value = "id") String id, Model model) {User user = db.maps.get(id);model.addAttribute(user);return "/user/update";}@RequestMapping(value = "/{id}/update", method = RequestMethod.POST)public String update(@PathVariable(value = "id") String id, User user) {db.maps.remove(id);db.maps.put(id, user);return "redirect:/user/users";}}

public class User {public User() {}public User(String id, String name, String password, String email) {this.id = id;this.name = name;this.password = password;this.email = email;}private String id;private String name;private String password;private String email;public String getId() {return id;}public void setId(String id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public String getPassword() {return password;}public void setPassword(String password) {this.password = password;}public String getEmail() {return email;}public void setEmail(String email) {this.email = email;}@Overridepublic String toString() {return "User [name=" + name + ", password=" + password + ", email="+ email + "]";}}

public class DB {public static Map<String, User> maps = new LinkedHashMap<>();static {maps.put("1", new User("1", "张无忌", "123", "zhangwuji@qq.com"));maps.put("2", new User("2", "周芷若", "123", "zhouzhiruo01@sina.com.cn"));maps.put("3", new User("3", "包大人", "123", "baodaren@sohu.com.cn"));maps.put("4", new User("4", "吕布", "123", "lvbu008@163.com.cn"));}public static Map<String, User> getUserList() {return maps;}public static User getUserById(String id) {return maps.get(id);}public static void addUser(User user) {maps.put(String.valueOf(maps.size() + 1), user);}public static void removeUser(String id) {maps.remove(id);}public static void updateUser(User user) {if (maps.containsKey(user.getId())) {maps.remove(user.getId());maps.put(user.getId(), user);}}}

web.xml

<?xml version="1.0" encoding="UTF-8"?><web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee"xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"id="WebApp_ID" version="3.0"><!-- 加载顺序 context param  listener filter servlet --><filter><filter-name>CharacterEncodingFilter</filter-name><filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class><init-param><param-name>encoding</param-name><param-value>UTF-8</param-value></init-param></filter><filter-mapping><filter-name>CharacterEncodingFilter</filter-name><url-pattern>/*</url-pattern></filter-mapping><servlet><servlet-name>mvc</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class></servlet><servlet-mapping><servlet-name>mvc</servlet-name><url-pattern>/</url-pattern></servlet-mapping></web-app>

mvc-servlet.xml

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"xmlns:mvc="http://www.springframework.org/schema/mvc"xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsdhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"><mvc:annotation-driven /><context:component-scan base-package="com.cnpc.rest.*" /><mvc:resources location="/resources/" mapping="/resources/**" /><beanclass="org.springframework.web.servlet.view.InternalResourceViewResolver"><property name="prefix" value="/WEB-INF/jsp/"></property><property name="suffix" value=".jsp"></property></bean></beans>

users.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"pageEncoding="UTF-8"%><%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>Insert title here</title></head><body> <c:forEach items="${maps }" var="map">     ${map.value.id } -- <a href="../user/${map.value.id }/update">${map.value.name }</a> -- ${map.value.email } <br/>  </c:forEach></body></html>

update.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"pageEncoding="UTF-8"%><%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>Insert title here</title></head><body> <form action="../${user.id }/update" method="post">       <input type="text" name="id" id="id" value="${user.id }"/> <br/>用户名:<input type="text" name="name" id="name" value="${user.name }"/> <br/>密码:<input type="password" name="password" id="password" value="${user.password }"><br/>邮箱:<input type="text" name="email" id="email" value="${user.email }"/><br/><input type="submit" value="提交"/></form></body></html>


0 0
原创粉丝点击