使用SpringMVC创建项目需要添加相关的依赖:
创建项目(如果Archetype选择了wabapp,会自动创建webapp相关包,但是java、resources、test等文件夹需要自己手动创建)
添加TomCat服务器(需要注意spring的版本,如果是spring6需要加载的TomCat10.1以上的版本)
根据使用的spring版本改变tomcat的版本
添加依赖
springmvc6
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-contextartifactId>
<version>6.0.3version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-webmvcartifactId>
<version>6.0.3version>
dependency>
<dependency>
<groupId>jakarta.servletgroupId>
<artifactId>jakarta.servlet-apiartifactId>
<version>6.0.0version>
<scope>providedscope>
dependency>
springmvc5
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-webmvcartifactId>
<version>5.3.22version>
dependency>
<dependency>
<groupId>javax.servletgroupId>
<artifactId>javax.servlet-apiartifactId>
<version>4.0.1version>
<scope>providedscope>
dependency>
创建webapp文件
DispatcherServlet其实也是一个Servlet,而servlet必须要在web.xml中配置,Spring才能找得到(设置成spring启动时就初始化 )
<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">
<context-param>
<param-name>contextConfigLocationparam-name>
<param-value>classpath:*.xmlparam-value>
context-param>
<filter>
<filter-name>encodingFilterfilter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilterfilter-class>
<init-param>
<param-name>encodingparam-name>
<param-value>UTF-8param-value>
init-param>
<init-param>
<param-name>forceEncodingparam-name>
<param-value>trueparam-value>
init-param>
filter>
<filter-mapping>
<filter-name>encodingFilterfilter-name>
<url-pattern>/*url-pattern>
filter-mapping>
<servlet>
<servlet-name>springmvcservlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
<init-param>
<param-name>contextConfigLocationparam-name>
<param-value>classpath:springmvc.xmlparam-value>
init-param>
<load-on-startup>1load-on-startup>
servlet>
<servlet-mapping>
<servlet-name>springmvcservlet-name>
<url-pattern>/url-pattern>
servlet-mapping>
web-app>
先导入支持JSP页面的依赖
<dependency>
<groupId>javax.servlet.jspgroupId>
<artifactId>jsp-apiartifactId>
<version>2.2version>
dependency>
<dependency>
<groupId>javax.servletgroupId>
<artifactId>jstlartifactId>
<version>1.2version>
dependency>
在resources下配置SpringMVC的 xml配置文件,创建视图解析器,用于拼接jsp相关页面
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
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/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<bean id="InternalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<property name="prefix" value="/WEB-INF/"/>
<property name="suffix" value=".jsp"/>
bean>
<context:component-scan base-package="controller路径"/>
<mvc:annotation-driven>
<mvc:message-converters>
<bean class="org.springframework.http.converter.StringHttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>text/plain;charset=UTF-8value>
<value>text/html;charset=UTF-8value>
<value>application/json;charset=UTF-8value>
<value>application/x-www-form-urlencoded;charset=UTF-8value>
list>
property>
bean>
mvc:message-converters>
mvc:annotation-driven>
beans>
配置好前面那些其实就已经可以运行了,运行后会自动打开 index.jsp页面
创建页面和controller进行视图跳转
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
Title
${msg}
@Controller //注解表示为一个控制器,要配置包的扫描
public class HelloController {
//访问到这个方法的路径
@RequestMapping("/hello")
public String test(Model model){ //model操作视图内容
model.addAttribute("msg","欢迎你啊!!!!");
//跳转到jsp页面。路径为:视图解析器前缀+jsppage/hello+后缀
return "jsppage/hello";
}
}
Thymeleaf 是一款模板引擎,主要用于前后端分离时使用,它既可以静态显示,也可以通过web访问实现动态显示,是SpringBoot中大力推崇的一套模板引擎。
它是用来代替JSP使用的,JSP 页面中常常会掺杂这一些后端的 Java 代码,不太符合前后端分离的思想,所以现在都陆续使用其他技术来代替 JSP ,而 Thymeleaf 就是其中较为优秀的技术之一。
SpringMVC使用 Thymeleaf 需要先导入相关的依赖:
<dependency>
<groupId>org.thymeleafgroupId>
<artifactId>thymeleaf-spring5artifactId>
<version>3.0.15.RELEASEversion>
dependency>
<dependency>
<groupId>org.attoparsergroupId>
<artifactId>attoparserartifactId>
<version>2.0.5.RELEASEversion>
dependency>
<dependency>
<groupId>org.unbescapegroupId>
<artifactId>unbescapeartifactId>
<version>1.1.6.RELEASEversion>
dependency>
然后在springmvc的配置文件中配置Thymeleaf的视图解析器,Thymeleaf 使用的HTML 页面,所以需要重新配置视图解析器,使用 Thymeleaf,并解析HTML页面。
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
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/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="controller路径"/>
<bean class="org.thymeleaf.spring5.view.ThymeleafViewResolver">
<property name="order" value="1"/>
<property name="characterEncoding" value="UTF-8"/>
<property name="templateEngine">
<bean class="org.thymeleaf.spring5.SpringTemplateEngine">
<property name="templateResolver">
<bean class="org.thymeleaf.spring5.templateresolver.SpringResourceTemplateResolver">
<property name="prefix" value="/WEB-INF/templates/"/>
<property name="suffix" value=".html"/>
<property name="templateMode" value="HTML5"/>
<property name="characterEncoding" value="UTF-8"/>
bean>
property>
bean>
property>
bean>
beans>
编写 HTML页面,然后使用Controller进行跳转:
DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Titletitle>
head>
<body>
<h1 th:text="${name}+你好啊">helloh1>
body>
html>
@Controller
public class SpringMVC5TestController {
//跳转到主页
@RequestMapping({"/","/index"})
public String intdex(){
return "index";
}
@RequestMapping("/hello")
public void hello(Model model){
System.out.println("执行");
model.addAttribute("name","yu");
}
}
不用web访问时静态页面:
web访问动态页面