SpringMVC的配置

学习SpringMVC配置过程的笔记,包括以配置文件和注解的方式完成配置,还有遇到的一些问题和解决办法

一.以配置文件的方式

1.创建一个web项目

先创建应该普通的Maven项目,如何再添加一个子模块

在子模块上右键点击Add Framework Support…,勾选Web Application

2.导入依赖

在父工程的 pom.xml 中导入依赖

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.1.9.RELEASE</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.2</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
</dependencies>

由于Maven可能存在资源过滤的问题,我们将配置完善

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<build>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
</build>

3.配置web.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
<?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">

<!--配置DispatchServlet:这个是SpringMVC的核心,也叫 请求分发器 或 前端控制器-->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!--DispatchServlet要绑定SpringMVC的配置文件-->
<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>

<!--解决乱码问题的过滤器-->
<filter>
<filter-name>encoding</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>utf-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

</web-app>

4.编写SpringMVC的配置文件

就是上一步中的 springmvc-servlet.xml ,命名方式是中的名字加 ‘-servlet’

①新建一个spring配置文 下面的代码自动生成

1
2
3
4
5
6
7
8
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">


</beans>

②添加 处理映射器

1
<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>

③添加 处理适配器

1
<bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/>

④添加 视图解析器

1
2
3
4
5
6
7
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" 
id="internalResourceViewResolver">
<!--前缀-->
<property name="prefix" value="/WEB-INF/jsp/"/>
<!--后缀-->
<property name="suffix" value=".jsp"/>
</bean>

5.编写操作业务的Controller

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
package com.kuang.controller;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
//1.让该类实现Controller接口,并重写方法
public class HelloController implements Controller {
@Override
public ModelAndView handleRequest
(HttpServletRequest httpServletRequest,
HttpServletResponse httpServletResponse)
throws Exception {
//2.new一个ModelAndView对象作为返回的视图
ModelAndView mv = new ModelAndView();
//3.编写业务代码
String res = "hello MVC";
mv.addObject("msg",res);
//4.视图跳转
// 因为在springmvc-servlet.xml中配置了视图解析器
// 所有这里写test就是指访问/WEB-INF/jsp/test.jsp
mv.setViewName("test");
//5.返回这个视图
return mv;
}
}

6.将controller类交给IOC容器,注册bean

在springmvc-servlet.xml中加入

1
<bean id="/hello" class="com.kuang.controller.HelloController"/>

当访问/hello时就会进入HelloController

7.创建视图层

即编写一个将要跳转至此的页面(test.jsp),写在 /WEB-INF/jsp 目录下

1
2
3
4
5
6
7
8
9
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Kuangshen</title>
</head>
<body>
${msg}
</body>
</html>

8.启动Tomcat进行测试

首先要确保项目结构中Artifacts下,发布的项目是否有lib,没有的话必须创建,如何点击加号,添加上所有Project Libraries.

否则访问的时候会报404

当前项目的tomcat配置如下

启动后在浏览器可以看到

二.以注解的方式

1.创建一个web项目

先创建应该普通的Maven项目,如何再添加一个子模块

在子模块上右键点击Add Framework Support…,勾选Web Application

2.导入依赖

在父工程的 pom.xml 中导入依赖

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.1.9.RELEASE</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.2</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
</dependencies>

由于Maven可能存在资源过滤的问题,我们将配置完善

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<build>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
</build>

3.配置web.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
<?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">

<!--配置DispatchServlet:这个是SpringMVC的核心,也叫 请求分发器 或 前端控制器-->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!--DispatchServlet要绑定SpringMVC的配置文件-->
<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>

<!--解决乱码问题的过滤器-->
<filter>
<filter-name>encoding</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>utf-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

</web-app>

4.编写SpringMVC的配置文件

在resource目录下添加 springmvc-servlet.xml 配置文件

就是上一步中的 springmvc-servlet.xml ,命名方式是 中的名字加 ‘-servlet’

①新建一个spring配置文 下面的代码自动生成

1
2
3
4
5
6
7
8
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">


</beans>

②添加context和mvc的XML命名空间,并通过指定它们的XSD schema文件的位置,告诉Spring Framework如何解析这些命名空间中的元素和属性。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<?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">


</beans>

③开启组件扫描,让指定包下的注解生效,由IOC容器统一管理

1
<context:component-scan base-package="com.kuang.controller"/>

④静态资源过滤

让Spring MVC不处理静态资源:HTML . JS . CSS . 图片 , 视频 …..

1
<mvc:default-servlet-handler />

⑤开启mvc注解驱动,同时完成JSON传参中文出现乱码的问题

在spring中一般采用@RequestMapping注解来完成映射关系,要想使@RequestMapping注解生效,必须向上下文中注册DefaultAnnotationHandlerMapping,和一个AnnotationMethodHandlerAdapter实例,这两个实例分别在类级别和方法级别处理。 而annotation-driven配置帮助我们自动完成上述两个实例的注入。 –>

1
2
3
4
5
6
7
8
9
10
11
12
<mvc:annotation-driven>
<mvc:message-converters register-defaults="true">
<bean class="org.springframework.http.converter.StringHttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>text/html;charset=UTF-8</value>
<value>application/json;charset=UTF-8</value>
</list>
</property>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>

⑥添加 视图解析器

1
2
3
4
5
6
7
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" 
id="internalResourceViewResolver">
<!--前缀-->
<property name="prefix" value="/WEB-INF/jsp/"/>
<!--后缀-->
<property name="suffix" value=".jsp"/>
</bean>

5.编写操作业务的Controller

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
package com.kuang.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("/HelloController")
public class HelloController {

//真实访问地址 : 项目名/HelloController/hello
@RequestMapping("/hello")
public String sayHello(Model model){
//向模型中添加属性msg与值,可以在JSP页面中取出并渲染
model.addAttribute("msg","hello,SpringMVC");
//web-inf/jsp/hello.jsp
return "hello";
}
}

6.创建视图层

即编写一个将要跳转至此的页面(hello.jsp),写在 /WEB-INF/jsp 目录下

1
2
3
4
5
6
7
8
9
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>SpringMVC</title>
</head>
<body>
${msg}
</body>
</html>

7.启动Tomcat进行测试

首先要确保项目结构中Artifacts下,发布的项目是否有lib,没有的话必须创建,如何点击加号,添加上所有Project Libraries.

否则访问的时候会报404.(与配置文件的方式中一致)。

三.中途遇到的问题

问题

Caused by: java.lang.IllegalArgumentException: Unsupported class file major version 62

解决

最终在CSDN上找到解决办法:

[(37条消息) Caused by: org.springframework.core.NestedIOException: ASM ClassReader failed to parse class file…_此方星河的博客-CSDN博客](https://blog.csdn.net/m0_51426055/article/details/124812823?ops_request_misc=%7B%22request%5Fid%22%3A%22168172018016800180618068%22%2C%22scm%22%3A%2220140713.130102334.pc%5Fall.%22%7D&request_id=168172018016800180618068&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~all~first_rank_ecpm_v1~rank_v31_ecpm-1-124812823-null-null.142^v83^insert_down38,239^v2^insert_chatgpt&utm_term=Caused by%3A org.springframework.core.NestedIOException%3A ASM ClassReader failed to parse class file - probably due to a new Java class file version that isnt supported yet%3A file [E%3A\IDEA\MySpringMVC\ou&spm=1018.2226.3001.4187)

总的来说也就是版本不匹配的问题,多调一下版本就可以了。

在pom.xml中加入以下插件,就能解决该问题

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<build>
<!--插件-->
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>