Controller及RestFul风格
一、控制器Controller
-
控制器复杂提供访问应用程序的行为,通常通过接口定义或注解定义两种方法实现。
-
控制器负责解析用户的请求并将其转换为一个模型。
-
在Spring MVC中一个控制器类可以包含多个方法。
-
在Spring MVC中,对于Controller的配置方式有很多种。
(一)、实现Controller接口
1. 说明
Controller是一个接口,在org.springframework.web.servlet.mvc包下,接口中只有一个方法;
//定义控制器
//注意点:不要导错包,实现Controller接口,重写方法;
public class ControllerTest1 implements Controller {
public ModelAndView handleRequest(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception {
//返回一个模型视图对象
ModelAndView mv = new ModelAndView();
mv.addObject("msg","Test1Controller");
mv.setViewName("test");
return mv;
}
}
2. 步骤:
- 先配置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">
<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>
</servlet-mapping>
</web-app>
- 在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
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!--添加 视图解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!--前缀-->
<property name="prefix" value="/WEB-INF/jsp/"/>
<!--后缀-->
<property name="suffix" value=".jsp"/>
</bean>
<bean name="/t1" class="com.xqs.contaoller.ControllerTest1"/>
</beans>
- Controller类
package com.xqs.contaoller;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* @Author: qsX
* @Time: 2020-12-29 19:59
* @Description:只要实现了 Controller 接口的类,说明这就是一个控制器
*/
public class ControllerTest1 implements Controller {
@Override
public ModelAndView handleRequest(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception {
ModelAndView mv = new ModelAndView();
//添加数据
mv.addObject("msg","ControllerTest1");
//要跳转到哪里 会拼接成 .../test.jsp
mv.setViewName("test");
return mv;
}
}
- jsp页面👇
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
${msg}
</body>
</html>
3. 缺点
-
实现接口Controller定义控制器是较老的办法
-
一个控制器中只有一个方法,如果要多个方法则需要定义多个Controller;定义的方式比较麻烦
(二)、@Controller注解
步骤
- 配置web.xml与上面一致
- 在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
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!--自动扫面包,让指定包下的注解无效,由IOC容器统一管理-->
<context:component-scan base-package="com.xqs.contaoller"/>
<!--让springMVC不处理静态资源-->
<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>
- Controller类
package com.xqs.contaoller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
/**
* @Author: qsX
* @Time: 2020-12-29 20:56
* @Description:注解
*/
@Controller
public class ControllerTest2 {
@RequestMapping("/t2")
public String test1(Model model) {
model.addAttribute("msg","java牛逼!");
return "test";
}
}
- jsp页面也与上面一致
(三)、@RequestMapping注解
-
@RequestMapping注解用于映射url到控制器类或一个特定的处理程序方法。可用于类或方法
上。 -
用于类上,表示类中的所有响应请求的方法都是以该地址作为父路径。
-
注解在方法上👇
@RequestMapping("/t")
public String test1 (Model model) {
model.addAttribute("msg","c3/t");
return "test";
}
访问路径为:http://localhost:8080/项目名/t
- 注解在类上👇
@Controller
@RequestMapping("/c3")
public class ControllerTest3 {
@RequestMapping("/t")
public String test1 (Model model) {
model.addAttribute("msg","c3/t");
return "test";
}
}
访问路径为:http://localhost:8080/项目名/c3/t
二、RestFul风格
(一)、概念
Restful就是一个资源定位及资源操作的风格。不是标准也不是协议,只是一种风格。基于这个风格设计的软件可以更简洁,更有层次,更易于实现缓存等机制。
(二)、功能
资源:互联网所有的事物都可以被抽象为资源
资源操作:使用POST、DELETE、PUT、GET,使用不同方法对资源进行操作。
分别对应 添加、 删除、修改、查询。
传统方式操作资源 :通过不同的参数来实现不同的效果!方法单一,post 和 get
http://127.0.0.1/item/queryItem.action?id=1 查询,GET
http://127.0.0.1/item/saveItem.action 新增,POST
http://127.0.0.1/item/updateItem.action 更新,POST
http://127.0.0.1/item/deleteItem.action?id=1 删除,GET或POST
使用RESTful操作资源 :可以通过不同的请求方式来实现不同的效果!如下:请求地址一样,但是功能可以不同!
http://127.0.0.1/item/1 查询,GET
http://127.0.0.1/item 新增,POST
http://127.0.0.1/item 更新,PUT
http://127.0.0.1/item/1 删除,DELETE
(三)、学习
- 普通👇
@Controller
public class RestFulController {
@RequestMapping("/add")
public String test1(int a, int b, Model model) {
int res = a + b;
model.addAttribute("msg", "结果为" + res);
return "test";
}
}
- 访问方式👇:http://localhost:8080/add?a=1&b=3
- RestFul
@Controller
public class RestFulController {
//原来的 :http://localhost:8080/add?a=1&b=3
//RestFul:http://localhost:8080/add/a/b
@RequestMapping("/add/{a}/{b}")
public String test1(@PathVariable int a, @PathVariable int b, Model model) {
int res = a + b;
model.addAttribute("msg", "结果为" + res);
return "test";
}
}
- 访问方式👇: http://localhost:8080/add/1/2
- 组合注解
@RequestMapping(value = "/add/{a}/{b}",method = RequestMethod.GET)
public String test2(@PathVariable int a, @PathVariable int b, Model model) {
int res = a + b;
model.addAttribute("msg", "结果为" + res);
return "test";
}
@GetMapping("/add/{a}/{b}")
public String test3(@PathVariable int a, @PathVariable int b, Model model) {
int res = a + b;
model.addAttribute("msg", "结果为" + res);
return "test";
}
还有其他的 @GetMapping @PostMapping @PutMapping @DeleteMapping @PatchMapping
@GetMapping
相当于 @RequestMapping(value = "/add/{a}/{b}",method = RequestMethod.GET)