springMVC(二)、开发环境搭建

一、开发环境搭建

1.配置 pom.xml 文件

<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 " target="_blank">http://maven.apache.org/xsd/maven-4.0.0.xsd">

<modelVersion>4.0.0</modelVersion>

<groupId>com.yinhai.gc</groupId>

<artifactId>mvc</artifactId>

<version>1.0-SNAPSHOT</version>

<packaging>war</packaging>

<dependencies>

<!-- webmvc -->

<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.11.RELEASE</version>

</dependency>

</dependencies>

</project>

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">

<!-- 配置Spring的前端控制器,对浏览器发送的请求统一进行处理-->

<servlet>

<servlet-name>SpringMvc</servlet-name>

<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

<!-- 配置SpringMVC配置文件的位置和名称 -->

<init-param>

<param-name>contextConfigLocation</param-name>

<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>

</web-app>

3 . 配置 SpringMvc.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"

xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd

http://www.springframework.org/schema/context " target="_blank">http://www.springframework.org/schema/context/spring-context.xsd">

<!-- 开启注解扫描 -->

<context:component-scan base-package="com.yinhai.gc.controller"/>

<!-- 配置Thymeleaf视图解析器 -->

<bean id="viewResolver" 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>

4 . 控制器

@Controller

public class HelloController {

@RequestMapping("/")

public String index(){

return "index";

}

5 . index.xml 项目启动跳转页面

<!DOCTYPE html>

<html lang="en" xmlns:th="http://www.thymeleaf.org">

<head>

<meta charset="UTF-8">

<title>Title</title>

</head>

<body>

你好!

<a th:href="@{/target}">狂飙电视剧入口!</a>

</body>

</html>

6 . 项目结构

7、总结

note : 浏览器发送请求,若请求地址符合前端控制器的url-pattern,该请求就会被前端控制器DispatcherServlet处理 ;前端控制器会读取SpringMVC的核心配置文件,通过扫描组件找到控制器,将请求地址和控制器中的@RequestMapping注解的value属性值进行匹配,若匹配成功,该注解所标识控制器方法就是处理请求的方法 ;处理请求的方法需要返回一个字符串类型的视图名称,改视图名称会被视图解析器解析,加上前缀和后缀组成视图的路径,通过Thymeleaf对视图进行渲染,最终转发到视图所对应页面

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一只鸟儿

你的鼓励是我最大的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值