Restful风格
基本概念
RESTFUL是一种网络应用程序的设计风格和开发方式,基于HTTP,可以使用XML格式定义或JSON格式定义。RESTFUL适用于移动互联网厂商作为业务接口的场景,实现第三方OTT调用移动网络资源的功能,动作类型为新增、变更、删除所调用资源。
与传统方式的区别
传统的方式 : http://localhost:8080/hello?a=1&b=2
Restful风格 : http://localhost:8080/hello/1/2
由上我们可以看到Restful风格更加简洁,有层次,更安全;别人并不知道1/2代表啥
Restful风格,两个同样的请求,可根据请求的方式不同访问不同的页面,比如:Get和Post的方式
实现
1. 新建一个空的Maven项目
添加Web依赖
2.导包
spring-webmvc,servlet-api,jsp-api,jstl
3.配置在WEB-INF下的web.xml文件
注册DispatcherServlet,关联配置文件,设置启动级别
<?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-name>SpringMVC</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!--关联配置文件-->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc-servlet.xml</param-value>
</init-param>
<!--启动级别-->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>SpringMVC</servlet-name>
<url-pattern>/</url-pattern>
<!--注意这里的/与/*是有区别的
/ :匹配所有请求(不包括.jsp)
/*:匹配所有请求(包括.jsp)
-->
</servlet-mapping>
</web-app>
4.在resources下编写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
https://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
https://www.springframework.org/schema/mvc/spring-mvc.xsd">
<context:component-scan base-package="com.feng.controller"/>
<!--处理映射器和处理设配器-->
<mvc:default-servlet-handler/>
<mvc:annotation-driven/>
<!--视图解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
5.编写Controller
注意使用Restful风格参数一定要注明@PathVariable
@Controller
public class Restful {
//可约束请求的类型,get的方式
@RequestMapping(value = "/hello/{a}/{b}",method = RequestMethod.GET)
public String test(@PathVariable int a,@PathVariable int b, Model model){
int result = a+b;
model.addAttribute("msg","Get结果为:"+result);
return "test";
}
//Post的方式
@PostMapping("/hello/{a}/{b}")
public String test2(@PathVariable int a,@PathVariable int b, Model model){
int result = a+b;
model.addAttribute("msg","Post结果为:"+result);
return "test";
}
}
方法级别变体
@GetMapping("/hello/{a}/{b}") <=> @RequestMapping(value = “/hello/{a}/{b}”,method = RequestMethod.GET)
@PostMapping
@PutMapping
@DeleteMapping
@PatchMapping
6.编写需要跳转,以及一个表单的页面
跳转页面:根据这个路径进行编写/WEB-INF/jsp/test.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
${msg}
</body>
</html>
表单页面:在web目录下新建一个form.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<form action="/hello/1/2" method="post">
<input type="submit">
</form>
</body>
</html>
7.配置Tomcat启动
Get方式
Post方式
由上观之,Restful风格同样的路径访问能得出不一样的结果!