开发工具

IDE:idea 2019.3.2

搭建专用工具:maven3.5.4

网络服务器:tomcat 9.0.30

Spring版本号:5.3.1

建立maven工程项目

image

image

加上装包方法:war
image

引进依靠

<dependencies>
    <!-- SpringMVC -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>5.3.1</version>
    </dependency>

    <!-- 日志 -->
    <dependency>
        <groupId>ch.qos.logback</groupId>
        <artifactId>logback-classic</artifactId>
        <version>1.2.3</version>
    </dependency>

    <!-- ServletAPI -->
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
        <version>3.1.0</version>
        <scope>provided</scope>
    </dependency>

    <!-- Spring5和Thymeleaf整合包 -->
    <dependency>
        <groupId>org.thymeleaf</groupId>
        <artifactId>thymeleaf-spring5</artifactId>
        <version>3.0.12.RELEASE</version>
    </dependency>
</dependencies>

配备web.xml

image

image

配备SpringMVC的前面控制板,对电脑浏览器推送的要求统一开展解决

可根据init-param标识设定SpringMVC环境变量的具体位置和名字,根据load-on-startup标识设定SpringMVC前面控制板DispatcherServlet的重置時间

<servlet>
    <servlet-name>springMVC</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <!-- 根据复位主要参数特定SpringMVC环境变量的具体位置和名字 -->
    <init-param>
        <!-- contextConfigLocation为数值 -->
        <param-name>contextConfigLocation</param-name>
        <!-- 应用classpath:表明从类途径搜索环境变量,比如maven工程项目中的src/main/resources -->
        <param-value>classpath:springMVC.xml</param-value>
    </init-param>
    <!-- 
 		做为结构的主要部件,在运行流程中有大批量的复位实际操作要做
		而这种实际操作放到第一次要求时才实行会明显危害网站打开速度
		因而必须根据此标识将运行操纵DispatcherServlet的重置時间提早到网络服务器运作时
	-->
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>springMVC</servlet-name>
    <!--
        设定springMVC的关键控制板能够解决的要求的要求途径
        /所搭配的要求能够 是/login或.html或.js或.css方法的要求途径
        可是/不可以配对.jsp要求途径的要求
    -->
    <url-pattern>/</url-pattern>
</servlet-mapping>

\<url-pattern>标识中应用//*的差别:

/所搭配的要求能够 是/login.html.js.css方法的要求途径,可是/不可以配对.jsp要求途径的要求

因而就可以防止在浏览jsp网页页面时,该申请被DispatcherServlet解决,进而找不着对应的网页页面

/*则可以搭配全部要求,比如在应用过滤装置时,若必须对全部要求开展过虑,就必须应用\*的书写

建立springMVC的环境变量

src/main/resources文件目录中建立springmvc.xml环境变量

image

?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       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
                           http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 全自动扫描仪包 -->
<context:component-scan base-package="com.atguigu.mvc.controller"/>

<!-- 配备Thymeleaf主视图在线解析 -->
<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>

建立要求控制板

因为前面控制板对电脑浏览器推送的要求开展了统一的解决,可是主要的要求有不一样的处理方式,因而必须建立解决实际要求的类,即要求控制板

要求控制板中每一个解决要求的办法变成控制板方式

由于SpringMVC的控制板由一个POJO(一般的Java类)出任,因而必须根据@Controller注释将其标志为一个操纵层部件,交到Spring的IoC器皿管理方法,这时SpringMVC才可以鉴别控制板的存有

@Controller
public class HelloController {
}

检测

html文档放到src/main/webapp/WEB-INF/templates文件目录中

image

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>主页</title>
</head>
<body>
<h1>主页</h1>
</body>
</html>

在要求控制板中建立解决要求的方式

// @RequestMapping注释:解决要求和控制板方式 中间的映照关联
// @RequestMapping注释的value特性能够根据要求详细地址配对要求,/表明的当今工程项目的前后文途径
// localhost:8080/springMVC/
@RequestMapping("/")
public String index() {
    //设定主视图名字
    return "index";
}

根据网页链接自动跳转到特定网页页面

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>主页</title>
</head>
<body>
    <h1>主页</h1>
    <a th:href="@{/hello}">HelloWorld</a><br/>
</body>
</html>

在要求控制板中建立解决要求的方式

@RequestMapping("/hello")
public String HelloWorld() {
    return "target";
}

配备Tomcat

image

image

点一下Debug运行Tomcat就可以
image

评论(0条)

刀客源码 游客评论