文章目录
- RESTFul
- 一、基础概念
- 二、增删改查
- 1.查询全部用户信息 (GET)
- 2.根据id查询用户信息
- 3.添加用户(POST)
- 4.修改用户 (PUT)
- 5.删除用户 (DELETE)
RESTFul
一、基础概念
二、增删改查
配置web.xml,使可以使用PUT,DELETE
<!-- 配置请求方法--><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>
1.查询全部用户信息 (GET)
@RequestMapping(value = "/user",method = RequestMethod.GET)public String getAllUser(){System.out.println("获取全部用户信息");return "target";}
2.根据id查询用户信息
@RequestMapping(value = "/user/{id}",method = RequestMethod.GET)public String getUserById(){System.out.println("根据ID获取用户信息");return "target";}
3.添加用户(POST)
@RequestMapping(value = "/user",method = RequestMethod.POST)public String addUser(String username,String password){System.out.println(username+","+password);return "target";}
4.修改用户 (PUT)
@RequestMapping(value = "/user",method = RequestMethod.PUT)public String updateUser(String username,String password){System.out.println("修改信息");return "target";}
<form th:action="@{/user}" method="post"><input type="hidden" name="_method" value="PUT">用户名<input name="username" type="text"><br>密码<input type="password" name="password"> <br><input type="submit" value="修改">
</form>
注意添加这个后 </input type=“hidden” name=“_method” value=“PUT”>,这样请求方法才变成PUT
5.删除用户 (DELETE)
@RequestMapping(value = "/user/{id}",method = RequestMethod.DELETE)public String deleteUser(){System.out.println("删除信息");return "target";}
<form th:action="@{/user/1}" method="post"><input type="hidden" name="_method" value="DELETE">用户名<input name="username" type="text"><br>密码<input type="password" name="password"> <br><input type="submit" value="删除">
</form>
注意 </input type=“hidden” name=“_method” value=“DELETE”>,这样请求方法才变成DELETE