Spring MVC rest
2015-01-28 20:30
183 查看
列表页面 /user/users
更新页面 /user/20150128/update
删除页面 /user/20150218/delete
添加页面 /user/add
显示详情 /user/张无忌
一个相对完整的实例,只提供了现实列表功能 和 修改功能,添加删除基本一样,没做处理
web.xml
mvc-servlet.xml
users.jsp
update.jsp
更新页面 /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; } @Override public 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.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://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/**" /> <bean class="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>
相关文章推荐
- SpringMVC Mybatis Shiro RestTemplate的实现客户端无状态验证及访问控制【转】
- 使用 Spring 3 MVC HttpMessageConverter 功能构建 RESTful web 服务(包括使用 RestTemplate 与 REST 服务进行通信)
- Bug 发现 spring mvc restcontroller
- springmvc+velocity+ Rest Services(xml,json)实例
- Spring Boot——2分钟构建spring web mvc REST风格HelloWorld
- javax.servlet.ServletException: Servlet.init() for servlet springmvc_rest threw exception
- spring mvc rest 方式
- 5分钟构建spring web mvc REST风格HelloWorld
- SpringMVC RestTemplate的几种请求调用
- Spring Boot Spring MVC Rest json输出日期的问题
- Java Spring MVC 和 REST 处理404等异常的不同
- 构建spring web mvc REST风格HelloWorld
- Spring Boot——2分钟构建spring web mvc REST风格HelloWorld
- 就是这么简单(续)!使用 RestAssuredMockMvc 测试 Spring MVC Controllers
- Spring Boot——spring web mvc REST风格HelloWorld
- Spring 4 MVC @RestController 注解实现REST Service(带源码)
- [转]Spring Boot——2分钟构建spring web mvc REST风格HelloWorld
- maven+springmvc错误 JAX-RS (REST Web Services) 2.0 can not be installed
- springmvc rest @PathVariable和@RequestParam的区别
- dubbox为dubbo提供REST服务(dubbox + springmvc)