RestFul的优势:简洁,高效,安全 还能实现url的复用
restful风格的注解
变量上的注解:@PathVariable
方法上的注解
@PostMapping("/h1/{a}/{b}") <==========> @RequestMapping(path = "/h1/{a}/{b}",method = RequestMethod.POST) @GetMapping() @PutMapping() @DeleteMapping() @PatchMapping()
首先要知道最初的传递参数的方式?a=1&b=2 ,这样的传参,暴露了我们的参数是不安全的,而restful风格的传参是/1/2,只是传递了值。
再写一遍web.xml文件和springmvc-servlet.xml文件,其实在我们的开发中这两个配置文件基本上是不需要在做改变的
web.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<!--注册DispatcherServlet 本质就是一个servlet-->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!--加载springmvc-servlet.xml文件-->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc-servlet.xml</param-value>
</init-param>
<!--配置web.xml文件的启动级别-->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
springmvc-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/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!--扫描特定包下的注解-->
<context:component-scan base-package="com.zkw.restful"/>
<!--过滤静态资源-->
<mvc:default-servlet-handler/>
<!--mvc注解驱动-->
<mvc:annotation-driven/>
<!--视图解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="internalResourceViewResolver">
<!--前缀-->
<property name="prefix" value="/WEB-INF/jsp/"/>
<!--后缀-->
<property name="suffix" value=".jsp"/>
</bean>
</beans>
hello.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
${result}
</body>
</html>
RestfulController.java
package com.zkw.restful;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@Controller
@RequestMapping("/restful")
public class RestfulController {
@RequestMapping(path = "/h1/{a}/{b}",method = RequestMethod.POST)
public String Restful(@PathVariable int a, @PathVariable String b, Model model){
model.addAttribute("result","结果为"+(a+b));
return "hello";
}
@GetMapping("/h1/{a}/{b}")
public String Restful(@PathVariable int a, @PathVariable int b, Model model){
model.addAttribute("result","结果为"+(a+b));
return "hello";
}
//原始的风格
@RequestMapping("/h2")
public String Restful(String a, int b, Model model){
model.addAttribute("result","结果为"+(a+b));
return "hello";
}
}
解释一下,在类上边的@RequestMapping("/restful")与这个类下边方法上的所有@RequestMapping("/h2")和 @GetMapping("/h1/{a}/{b}")是父子关系,相当于你要想访问这各类里边的方法,首先要先访问这个类。
原始风格的结果为
先说一下,它默认的提交方式是get,当我使用restful风格访问的时候,它走得是 @GetMapping("/h1/{a}/{b}")这个
最后再说一下url的复用,当我在另一个页面上使用post提交表单的时候它请求的方法是第一个
首先写一个测试页面test.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<form action="/springmvc_04_restful_war_exploded/restful/h1/1/2" method="post">
<input type="submit">
</form>
</body>
</html>
结果为