SPRINGMVC中的RESTFUL风格
RESTful架构:
是一种设计的风格,并不是标准,只是提供了一组设计原则和约束条件,也是目前比较流行的一种互联网软件架构。它结构清晰、符合标准、易于理解、扩展方便,所以正得到越来越多网站的采用。
关于RESTful架构给你一个链接讲的挺好的:http://www.ruanyifeng.com/blog/2011/09/restful
这里我结合springMVC讲解一下RESTful在springMVC中的使用,在讲之前先来看看RESTful提倡哪些做法:
- 他会对url进行规范:
a) 非REST风格的url:localhost:8080/springmvc?name=admin&password=123;
b) REST风格的url:localhost:8080/springmvc/admin/123;
分析:
-
- 直观上看是不是比较简洁
- 看不懂:隐藏了参数名称,安全性,防止被攻击
- 所有的url都可以当成是资源
- 对http的方法进行规范
a)不管是删除,添加,更新….使用的url都是一致,那么如果需要删除,就把http的方法设置删除
b) 控制器:通过判断http的方法来执行操作(增删改查)
目前这种做法还没有被广泛采用
3.对contentType也进行规范
a) 就是在请求是指定contentType的类型(json交互中就有体现)
4.接下来看看springMVC中怎么实现RESTful风格
首先:你在请求路径上@RequestMapping(value = "/hello_rest/{name}/{password}")需要用{}来动态匹配参数
其次:方法的形参上要@PathVariable("name")来匹配上面的参数,这里@PathVariable中的字符串必须和你{}中的名字一致
访问路径:localhost:8080/工程名/hello_rest/xx/xx(其中xx就是你随便填写的内容,它会匹配到后台的name和password的值)
比如:
你输入的路径是:localhost:8080/工程名/hello_rest/admin/admin
后台会匹配到:name="amdin",password="admin"
/**
* 1.路径的变化:/hello_rest/{name}/{password}其中{}相当于可以的参数
* 2.参数的写法:需要利用@PathVariable("name")来匹配上面的参数
* 3.至于@PathVariable后面跟的形参你就可以随便命名了
* @param username
* @param password
* @return
*/
@RequestMapping(value = "/hello_rest/{name}/{password}")
public String hello_rest(@PathVariable("name") String username,
@PathVariable("password") String password) {
if("admin".equals(username)&"123".equals(password)){
System.out.println("登录成功");
return "hello";
}
return "hello";
}
这种写法和第一种类似,只不过是把{}动态匹配参数的放到前面去了,其原理是一样的,不多说,直接看看这个访问路径的写法就好
访问路径:localhost:8080/工程名/xx/xx/hello_rest
只是把参数由后面放到前面去了而已。
/**
* 1.路径的变化:/{name}/{password}/hello_rest其中{}相当于可以的参数
* 2.参数的写法:需要利用@PathVariable("name")来匹配上面的参数
* 3.至于@PathVariable后面跟的形参你就可以随便命名了
* @param username
* @param password
* @return
*/
@RequestMapping(value = "/{name}/{password}/hello_rest")
public String hello_rest2(@PathVariable("name") String username,
@PathVariable("password") String password) {
if("admin".equals(username)&"123".equals(password)){
System.out.println("登录成功");
return "hello";
}
return "hello";
}
http协议
@Controller
public class RestController {
/**
* /user/1
* @param userId
* @return
* @throws Exception
*/
@RequestMapping(value="/user/{id}",method=RequestMethod.GET)
public String index(@PathVariable(value="id") String userId) throws Exception{
return "/day1/user.jsp";
}
@RequestMapping(value="/user",method=RequestMethod.POST)
public String addUser(String name,HttpServletResponse response) throws Exception{
response.getWriter().println(name+" add success");
return null;
}
@RequestMapping(value="/user/{id}",method=RequestMethod.PUT)
public String updateUser(@PathVariable(value="id") String userId,HttpServletResponse response) throws Exception{
response.getWriter().println(userId+" update success");
return null;
}
@RequestMapping(value="/user/{id}",method=RequestMethod.DELETE)
public String deleteUser(@PathVariable(value="id") String userId,HttpServletResponse response) throws Exception{
response.getWriter().println(userId+" delete success");
return null;
}
}
这里必要要对应增删改查的动作 不然匹配不上会报405的错
<form action="${pageContext.request.contextPath}/user/12" method="post">
<input type="hidden" name="_method" value="delete"/>
<input type="text" name="name"/>
<input type="submit" value="提交"/>
</form>
推荐阅读
-
在CorelDRAW中绘制卡通风格的橘子
-
AI制作网页设中的GRUNGE风格
-
springMVC获取requestMapping、restful风格中路径的参数
-
JS中3种风格的For循环有什么异同?
-
SpringMVC中控制器接收JSP页面表单的参数接收方式详解及细节注意(400错误)
-
jQuery+SpringMVC中的复选框选择与传值实例
-
浅谈SpringMVC中post checkbox 多选框value的值(隐藏域方式)
-
网站建设中如何选择图片并设计出想要的风格?
-
在Restful风格中,axios请求中, method:“put“/‘delete‘/‘get‘/‘post‘四种方式
-
react开发中如何使用require.ensure加载es6风格的组件