ssm框架restful风格实现增删改查

1、什么是restful风格

大家在做Web开发的过程中,method常用的值是get和post. 可事实上,method值还可以是put和delete等等其他值。
既然method值如此丰富,那么就可以考虑使用同一个url,但是约定不同的method来实施不同的业务,这就是Restful的基本考虑。

2、restful风格独特之处

2.1 url的不同

  • 非restful风格:http://.../queryItems.action?id=001&type=T01
  • restful风格:http://..../items/001

2.2 请求方法的不同

学习javaweb的时候,我们只知道了get和post两种请求方法,当restful却不止这两种

  • 获取数据:GET请求
  • 增加数据:POST请求
  • 修改数据:PUT请求
  • 删除数据:DELETE请求

3、ssm实现restful风格增删改查

3.1 配置HiddenHttpMethodFilter过滤器

浏览器form表单只支持get和post请求,而delete和put请求并不支持。HiddenHttpMethodFilter是spring中自带的一个过滤器,可以将浏览器表单请求转换为标准的http请求,使它们支持get、post、delete、put请求。

<filter>
    <filter-name>HiddenHttpMethodFilter</filter-name>
    <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
</filter>
<filter-mapping>
    <filter-name>HiddenHttpMethodFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

3.2 在jsp中如何指定form表单的请求方式

在表单中加入隐藏域,name属性一定要为 ”_method“,这样刚才配的HiddenHttpMethodFilter拦截器才能够识别,value属性就是我们需要的请求方式。这样springmvc就会帮我们做

<input type="hidden" name="_method" value="PUT">

3.3 restful参数传递

在获取一条数据时或删除一条数据时,一般情况下前端要向后端传入一个id值,restful风格的参数传递与之前讲的注解@RequestMapping()注解映射请求中的URI模板一样,将参数作为变量放在URL中。
示例:
前端传入id值为10000,id为以参数变量

前端浏览器URL:http://www.example.com/users/10000
@RequestMapping()注解:@RequestMapping("/users/{id}")
方法中接收id:public String getOneUser(@PathVariable("id") int id){}

3.4增删改查实例

controller:

@Controller
public class UserController {

    @Resource(name="userService")
    UserService userService;
    
    /*
     * 查询所有用户
     */
    @RequestMapping(value="/users",method=RequestMethod.GET)
    public String index(Model model) {
        List<User> users = userService.getAllUser();
        model.addAttribute("users", users);
        return "users";
    }
    
    /*
     * 转发到添加用户的视图
     */
    @RequestMapping("/addUserView")
    public String addUserView() {
        return "add";
    }
    
    /*
     * 添加用户
     */
    @RequestMapping(value="/addUser",method=RequestMethod.POST)
    public String addUser(User user) {
        userService.addUser(user);
        return "redirect:/users";
    }
    
    /*
     * 获取所要修改用户的信息
     */
    @RequestMapping(value="/user/{id}",method=RequestMethod.GET)
    public String updateUserView(@PathVariable("id") Integer id,Model model) {
        User user = userService.getOneUserById(id);
        model.addAttribute("user",user);
        return "updateUserView";
    }
    
    /*
     * 修改用户信息
     */
    @RequestMapping(value="/user",method=RequestMethod.PUT)
    public String updateUser(User user) {
        userService.updateUser(user);
        return "redirect:/users";
    }
    
    /*
     * 删除用户
     */
    @RequestMapping(value="/user/{id}",method=RequestMethod.DELETE)
    public String deleteUser(@PathVariable("id") Integer id) {
        userService.deleteUser(id);
        return "redirect:/users";
    }
    
}

jsp:

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>
<html>
<head>
<meta charset="ISO-8859-1">
<title>获取全部用户信息</title>
</head>
<body>
    <table border="1" >
        <tr>
            <th>编号</th>
            <th>用户名</th>
            <th>密码</th>
            <th colspan="2">操作</th>
        </tr>
        <c:forEach var="user" items="${requestScope.users }" >
            <tr>
                <td>${user.id }</td>
                <td>${user.username }</td>
                <td>${user.password }</td>
                <td><a href="/ssm/user/${user.id }">修改</a></td>
                <td><form action="/ssm/user/${user.id }" method="post">
                    <input type="hidden" name="_method" value="DELETE">
                    <input type="submit" value="删除">
                </form></td>
            </tr>
        </c:forEach>
    </table>
    <a href="/ssm/addUserView">添加员工</a>
    
</body>
</html>

add.jsp:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
    <form action="/ssm/addUser" method="post">
        用户名:<input type="text" name="username"><br><br>
        密  码:<input type="text" name="password"><br><br>
        <input type="submit" value="提交">
    </form>
</body>
</html>

updateUserView.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>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
    <form action="/ssm/user" method="post">
        <input type="hidden" name="_method" value="PUT">
        <table border="1">
            <tr>
                <th>编号</th>
                <td><input type="text" name="id"
                    value="${requestScope.user.id }"></td>
            </tr>
            <tr>
                <th>用户名</th>
                <td><input type="text" name="username"
                    value="${requestScope.user.username }"></td>
            </tr>
            <tr>
                <th>密码</th>
                <td><input type="text" name="password"
                    value="${requestScope.user.password }"></td>
            </tr>
            <tr>
                <td><input type="submit" value="修改"></td>
            </tr>
        </table>
    </form>
</body>
</html>

猜你喜欢

转载自www.cnblogs.com/hamawep789/p/10896030.html