簡單的一次springMVC路由跳轉實現(xiàn)
實現(xiàn)目標:使用springMVC前端控制器,跳轉到WEB-INF的templates下面的前端頁面
圖示

1.目錄結構

2.創(chuàng)建一個maven的webapp項目,創(chuàng)建好之后記得把index.jsp文件刪除,否i則會首先跳到這個文件,我們要用前端控制器轉發(fā)所有請求(如果有大佬知道怎么讓他存在,又不影響,希望可以學習一下)

3.在xml里面,配置springMVC前端控制器,
<!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>
<!-- 配置springMVC的前端控制器,對瀏覽器發(fā)送的請求進行統(tǒng)一處理-->
<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>
<!--匹配除了.jsp請求路徑的請求-->
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>4.創(chuàng)建并配置springMVC.xml,記得配置一下context(開啟掃描需要)
<?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">
<!-- 掃描組件-->
<context:component-scan base-package="com.atguigu.mvc.controller"></context:component-scan>
<!-- 配置Thymeleaf視圖解析器 -->
<bean id="viewResolver" class="org.thymeleaf.spring5.view.ThymeleafViewResolver">
<!--設置視圖解析器優(yōu)先級,可以設置多個-->
<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>
5.匹配路徑
@Controller
public class HelloController {
//"/" -->/web-inf/templates/index.html
//RequestMapping請求映射
//value可不寫
@RequestMapping(value="/")
public String tindex(){
//返回視圖名稱,因為我們在視圖解析器里面,配置了后綴,所以這里不用寫了
return "index";
}
@RequestMapping(value="/target")
public String toTarget(){
return "target";
}
}
index.html
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>/</title>
</head>
<body>
<!--/瀏覽器從哪個localhost:8080開始-->
<!--th:被thymeleaf解析,可從localhost:8080:項目名字開始-->
<!--@{}檢測到絕對路徑,自動添加上下文路徑-->
<a th:href="@{/target}" rel="external nofollow" >訪問目標</a>
ahhahahahah
</body>
</html>
target.html
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
HELLOWORLD
</body>
</html>
到此這篇關于簡單的一次springMVC路由跳轉實現(xiàn)的文章就介紹到這了,更多相關springMVC 路由跳轉內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
ConstraintValidator類如何實現(xiàn)自定義注解校驗前端傳參
這篇文章主要介紹了ConstraintValidator類實現(xiàn)自定義注解校驗前端傳參的操作,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-06-06
解決Springboot項目打包后的頁面丟失問題(thymeleaf報錯)
這篇文章主要介紹了解決Springboot項目打包后的頁面丟失問題(thymeleaf報錯),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-11-11
spring boot + mybatis實現(xiàn)動態(tài)切換數(shù)據(jù)源實例代碼
這篇文章主要給大家介紹了關于spring boot + mybatis實現(xiàn)動態(tài)切換數(shù)據(jù)源的相關資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2018-10-10
IntelliJ IDEA 2020.3.3現(xiàn)已發(fā)布!新增“受信任項目”功能
這篇文章主要介紹了IntelliJ IDEA 2020.3.3現(xiàn)已發(fā)布!新增“受信任項目”功能,本文給大家分享了idea2020.3.3激活碼的詳細破解教程,每種方法都很好用,使用idea2020.3以下所有版本,需要的朋友可以參考下2021-03-03
SpringBoot切面攔截@PathVariable參數(shù)及拋出異常的全局處理方式
這篇文章主要介紹了SpringBoot切面攔截@PathVariable參數(shù)及拋出異常的全局處理方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-08-08
java 定時器線程池(ScheduledThreadPoolExecutor)的實現(xiàn)
這篇文章主要介紹了java 定時器線程池(ScheduledThreadPoolExecutor),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-06-06

