依赖
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jsp-api</artifactId>
<version>2.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>taglibs</groupId>
<artifactId>standard</artifactId>
<version>1.1.2</version>
</dependency>
通过注解写 servlet 实现 HttpServlet
@WebServlet("/first")
public class FirstServlet extends HttpServlet {
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("====进入了servlet===");
req.setAttribute("username","sy");
req.getRequestDispatcher("/test.jsp").forward(req,resp);
}
}
通过 web.xml 配置
<web-app>
<display-name>Archetype Created Web Application</display-name>
<servlet>
<servlet-name>first-servlet</servlet-name>
<servlet-class>cn.sycoder.FirstServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>first-servlet</servlet-name>
<url-pattern>/test</url-pattern>
</servlet-mapping>
</web-app>
开启 el 表达式支持
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@page isELIgnored="false" %>
<html>
<head>
<title>Title</title>
</head>
<body>
${username}
</body>
</html>
获取请求参数不方便
//获取请求参数
String username = req.getParameter("username");
String password = req.getParameter("password");
给出的解决方案
如果可以直接从方法入参传参是不是很方便,可以直接拿来用
protected void service(String username,String password)
设置响应视图或者设置响应的参数类型不方便
req.getRequestDispatcher("/test.jsp").forward(req,resp);
给出的解决方案
如果不需要设置响应类型
如果不需要设置请求转发,直接跳转更方便
req.getRequestDispatcher("/test.jsp").forward(req,resp);
//return "test";
导入依赖
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.2.17.RELEASE</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jsp-api</artifactId>
<version>2.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>taglibs</groupId>
<artifactId>standard</artifactId>
<version>1.1.2</version>
</dependency>
配置文件
<?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"
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">
<!-- 配置包扫描-->
<context:component-scan base-package="cn.sycoder.controller"/>
<bean id="view" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 配置视图前缀-->
<property name="prefix" value="/WEB-INF/pages/"/>
<!-- 配置视图后缀-->
<property name="suffix" value=".jsp"/>
</bean>
</beans>
web.xml
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<display-name>Archetype Created Web Application</display-name>
<servlet>
<servlet-name>spring-mvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 初始化 spring-mvc 配置-->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-mvc.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>spring-mvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
配置
@Controller
public class XmlController {
@RequestMapping("/test")
public void test(){
System.out.println("---------");
}
}
视图解析器
<?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"
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">
<!-- 配置包扫描-->
<context:component-scan base-package="cn.sycoder.controller"/>
<bean id="view" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 配置视图前缀-->
<property name="prefix" value="/WEB-INF/pages/"/>
<!-- 配置视图后缀-->
<property name="suffix" value=".jsp"/>
</bean>
</beans>
视图解析操作
导入依赖
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.2.17.RELEASE</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jsp-api</artifactId>
<version>2.0</version>
<scope>provided</scope>
</dependency>
创建配置类
@Configuration
@ComponentScan("cn.sycoder.controller")
public class SpringMvcConfig {
}
替换操作
public class ServletConfig extends AbstractDispatcherServletInitializer {
@Override
protected WebApplicationContext createServletApplicationContext() {
//获取SpringMVC容器
AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
context.register(SpringMvcConfig.class);// 同等与mvc配置类替换xml文件
return context;
}
@Override
protected String[] getServletMappings() {
return new String[]{"/"};
}
@Override
protected WebApplicationContext createRootApplicationContext() {
return null;
}
}
配置Controller
@Controller
public class SpringMvcController {
@RequestMapping("/get")
public void getSpring(){
System.out.println("========");
}
}
作用:设置请求映射路径
SpringMVC 接收到指定请求时,在映射关系中找到对应的控制方法
名称 | @RequestMapping |
---|---|
位置 | 类或接口、或者方法上 |
作用 | 设置定义控制器方法的访问路径 |
属性 | String[] value() 设置访问路径的,RequestMethod[] method() 指定访问方法 |
当需要返回json串的时候才需要使用@ResponseBody
使用位置
@Controller
@RequestMapping("/requestMappingController")
public class RequestMappingController {
@RequestMapping(headers ={"header=123"} ,value = {"/test"})// /requestMapping/test
@ResponseBody
public String test(){
System.out.println("============");
return "OK";
}
}
指定请求类型
@RequestMapping(value = "/test1",method = RequestMethod.POST)
@ResponseBody
public String test1(){
System.out.println("============");
return "OK";
}
指定请求头(可以做权限拦截)(了解一下就行)
@RequestMapping(headers ={"header=123"} ,value = {"/test"})// /requestMapping/test
@ResponseBody
public String test(){
System.out.println("============");
return "OK";
}
指定请求参数(没用)
@RequestMapping(params = {"username=123"},value = {"/test2"})// /requestMapping/test
@ResponseBody
public String test2(){
System.out.println("============");
return "OK";
}
获取方式
@Controller
@RequestMapping("/requestParams")
public class RequestParams {
@RequestMapping("/test")
@ResponseBody
public String test(String username,String password){
System.out.println("username:"+username+",password:"+password);
return "ok";
}
}
@RequestParam
名称 | @RequestParam |
---|---|
位置 | 控制器方法中的形参上 |
属性 | boolean required() 表示是否必填String value(),参数重命名名称 |
作用 | 绑定请求参数与处理器方法之间形参的关系 |
出现问题,获取不到参数值
解决方案
@Controller
@RequestMapping("/requestParams")
public class RequestParams {
@RequestMapping("/test")
@ResponseBody
public String test(@RequestParam(value = "un",required = false) String username,
@RequestParam(value = "password1",required = false) String password){
System.out.println("username:"+username+",password:"+password);
return "ok";
}
}
传递方式
@RequestMapping("/user")
@ResponseBody
public String user(User user){
System.out.println("username:"+user.getAccount()+",password:"+user.getPassword());
return "ok";
}
图示
如果出现对象嵌套,直接通过属性名称.传参即可
public class User {
private String account;
private String password;
private Address address;
}
传数组
@RequestMapping("/array")
@ResponseBody
public String array(String[] cities){
System.out.println(Arrays.toString(cities));
return "ok";
}
传递参数
传集合
@RequestMapping("/list")
@ResponseBody
public String list(@RequestParam List<String> cities){
System.out.println(cities);
return "ok";
}
传参图示
依赖
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.12.5</version>
</dependency>
开启支持
@Configuration
@ComponentScan("cn.sycoder.controller")
@EnableWebMvc
public class SpringMvcConfig {
}
@EnableWebMvc
名称 | @EnableWebMvc |
---|---|
位置 | SpringMVC 配置类上 |
作用 | 开启SpringMVC 的辅助功能支持 |
@RequestBody
名称 | @RequestBody |
---|---|
位置 | SpringMVC控制方法形参里面 |
作用 | 将请求中的请求体包含的数据传递给形参 |
属性 | boolean required() 表示是否必填 |
传送 json 用户数据到后台,只有加了@RequestBody这个注解,才可以通过json传递数据,并再前端需要设置contentType:application/json
@Controller
@RequestMapping("/RequestBodyController")
public class RequestBodyController {
@PostMapping("/user/save")
@ResponseBody
public String saveUser(@RequestBody User user){
System.out.println(user);
return "OK";
}
}
图示
如果 json 参数有嵌套也是老样子传参即可
{ "account": " 123", "password": "123456", "address": { "city": "杭州" } }
接收图示
传递json 数组
@PostMapping("/user/lists")
@ResponseBody
public String saveUser(@RequestBody List<String> lists){
System.out.println(lists);
return "OK";
}
图示
传递对象集合json数据
@PostMapping("/user/lists/user")
@ResponseBody
public String listUser(@RequestBody List<User> lists){
System.out.println(lists);
return "OK";
}
传递图示
json 数据
[{"account":" 123","password":"123456","address":{"city":"杭州"}},{"account":" sy","password":"123456","address":{"city":"北京"}}]
控制器
@Controller
@RequestMapping("/date")
public class DateController {
@RequestMapping("/test")
@ResponseBody
public String testDate(Date date){
System.out.println(date);
return "ok";
}
}
图示
出现错误
定制化格式操作
@Controller
@RequestMapping("/date")
public class DateController {
@RequestMapping("/test")
@ResponseBody
public String testDate(@DateTimeFormat(pattern = "yyyy-MM-dd") Date date){
System.out.println(date);
return "ok";
}
}
名称 | @DateTimeFormat |
---|---|
作用 | 指定日期格式(yyyy-MM-dd) |
属性 | String pattern() 指定日期格式 |
使用位置 | SpringMVC 控制器方法形参前面,还可以使用到具体的对象属性上 |
配置
public class ServletConfig extends AbstractAnnotationConfigDispatcherServletInitializer {
@Override
protected Class<?>[] getRootConfigClasses() {
return new Class[0];
}
@Override
protected Class<?>[] getServletConfigClasses() {
return new Class[]{SpringMvcConfig.class};
}
@Override
protected String[] getServletMappings() {
return new String[]{"/"};
}
}
注解配置视图解析器
@Configuration
@ComponentScan("cn.sycoder.controller")
@EnableWebMvc
public class SpringMvcConfig implements WebMvcConfigurer {
@Bean
public ViewResolver viewResolver(){
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setPrefix("/WEB-INF/pages/");
resolver.setSuffix(".jsp");
return resolver;
}
}
配置视图解析器之后跳转
@Controller
public class TestController {
@RequestMapping("/test")
public String test(){
return "ok.jsp";
}
//配置视图解析器之后的视图跳转
@RequestMapping("/view")
public String view(){
return "view";
}
}
图示
如果没有使用 @ResponseBody 会出现如下问题
@RequestMapping("/respbody")
// @ResponseBody
public String respbody(){
return "ok";
}
响应文本类型使用 @ResponseBody
@RequestMapping("/respbody")
@ResponseBody
public String respbody(){
return "ok";
}
使用 @ResponseBody + @EnableWebMvc 才能返回 json
SpringMVC 配置类
@Configuration
@ComponentScan("cn.sycoder.controller")
@EnableWebMvc
public class SpringMvcConfig {
@Bean
public ViewResolver viewResolver(){
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setPrefix("/WEB-INF/pages/");
resolver.setSuffix(".jsp");
return resolver;
}
}
控制器方法使用 @ResponseBody
@RequestMapping("/userjson")
@ResponseBody
public User respUserJson(){
User user = new User();
user.setAddTime(new Date());
user.setName("sy");
user.setUsername("sy666");
user.setId(1L);
return user;
}
控制器方法
@RequestMapping("/listuserjson")
@ResponseBody
public List<User> respListUserJson(){
List<User> list = new ArrayList<>();
User user = new User();
user.setAddTime(new Date());
user.setName("sy");
user.setUsername("sy666");
user.setId(1L);
list.add(user);
User user1 = new User();
user1.setAddTime(new Date());
user1.setName("sy");
user1.setUsername("sy666");
user1.setId(1L);
list.add(user1);
return list;
}
@RestController = @Controller + @ResponseBody
应用
@RestController
public class MyRestController {
@RequestMapping("/listuserjson")
public List<User> respListUserJson(){
List<User> list = new ArrayList<>();
User user = new User();
user.setAddTime(new Date());
user.setName("sy");
user.setUsername("sy666");
user.setId(1L);
list.add(user);
User user1 = new User();
user1.setAddTime(new Date());
user1.setName("sy");
user1.setUsername("sy666");
user1.setId(1L);
list.add(user1);
return list;
}
}
名称 | @ResponseBody |
---|---|
作用 | 设置控制器的返回值作为响应体,如果返回的是对象类型,会转换成 json 对象传输 |
位置 | 可以使用到类上,或者控制器方法上 |
具体应用,就是jsp中可以获取后端的数据,通过el表达式获取到数据
@RequestMapping("/ok")
public String okServletRequest(HttpServletRequest request){
request.setAttribute("ok","66666");
return "ok";
}
jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@page isELIgnored="false" %>
Title
${ok}
具体应用
@RequestMapping("/modelAndView")
public ModelAndView modelAndView(){
ModelAndView view = new ModelAndView();
view.setViewName("ok");
view.addObject("ok","==============");
return view;
}
jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@page isELIgnored="false" %>
Title
${ok}
具体应用
@RequestMapping("/testMap")
public String testMap(Map<String,Object> map){
map.put("ok","-------------");
return "ok";
}
jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@page isELIgnored="false" %>
Title
${ok}
操作 | 传统方式 | REST风格 |
---|---|---|
查询操作 | /student/getById?id=5 | student/5 get请求 |
保存操作 | /saveStudent | student post 请求 |
更新 | /updateStudent | student put 请求 |
删除 | /deleteById?id=5 | student/5 delete 请求 |
实现
@Controller
@RequestMapping("/item")
public class ItemController {
//增加
@RequestMapping("/save")
@ResponseBody
public String save(@RequestBody Item item){
System.out.println(item);
return "save OK";
}
//删除
@RequestMapping("/delete")
@ResponseBody
public String delete(Long id){
System.out.println(id);
return "delete OK";
}
//修改
@RequestMapping("/update")
@ResponseBody
public String update(@RequestBody Item item){
System.out.println(item);
return "update OK";
}
//查询
@RequestMapping("/getById")
@ResponseBody
public String getById(Long id){
System.out.println(id);
return "getById OK";
}
}
实现
@RestController
public class ItemRestController {
//增加
@PostMapping("/item")
public String save(@RequestBody Item item){
System.out.println(item);
return "rest save OK";
}
//删除
@DeleteMapping("/item/{id}")
public String delete(@PathVariable Long id){
System.out.println(id);
return "rest delete OK";
}
//修改
@PutMapping("/item")
public String update(@RequestBody Item item){
System.out.println(item);
return "rest update OK";
}
//查询
@GetMapping("/item/{id}")
public String getById(@PathVariable Long id){
System.out.println(id);
return "rest getById OK";
}
}
但是出现如下问题
原因:SpringMVC 拦截了静态资源,根据 /pages/item.html 去找对于的 controller 方法执行,匹配我们配置的 / 的getServletMappings,找不到所以 404
配置静态资源放行就可以了
配置如下
@Configuration
public class StaticSupport extends WebMvcConfigurationSupport {
@Override
protected void addResourceHandlers(ResourceHandlerRegistry registry) {
//当访问 /pages/ 资源的时候,设置放行
registry.addResourceHandler("/pages/**").addResourceLocations("/pages/");
registry.addResourceHandler("/js/**").addResourceLocations("/js/");
registry.addResourceHandler("/css/**").addResourceLocations("/css/");
registry.addResourceHandler("/plugins/**").addResourceLocations("/plugins/");
}
}
注意,需要将静态支持交给 springmvc 管理
@Configuration
@ComponentScan("cn.sycoder")
@EnableWebMvc
public class SpringMvcConfig {
}