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

1.目錄結(jié)構(gòu)

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

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ā)送的請求進(jìn)行統(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的初始化時間提前到服務(wù)器啟動時-->
<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">
<!--設(shè)置視圖解析器優(yōu)先級,可以設(shè)置多個-->
<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" >訪問目標(biāo)</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>
到此這篇關(guān)于簡單的一次springMVC路由跳轉(zhuǎn)實現(xiàn)的文章就介紹到這了,更多相關(guān)springMVC 路由跳轉(zhuǎn)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
解決SpringBoot中使用@Transactional注解遇到的問題
這篇文章主要介紹了SpringBoot中使用@Transactional注解遇到的問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-09-09
Dwr3.0純注解(純Java Code配置)配置與應(yīng)用淺析一之零配置文件化
Dwr對我來說最重要的功能點就是反向Ajax調(diào)用,通俗來將就是后端可以直接調(diào)用前端的JS方法(只要在所能訪問的范圍內(nèi)),這也就是Dwr的真正來由,當(dāng)然它也有最基本的前端直接調(diào)用后端的特性,省去了我們經(jīng)常的一般Ajax調(diào)用2016-04-04
SpringBoot響應(yīng)處理之以Json數(shù)據(jù)返回的實現(xiàn)方法
這篇文章主要介紹了SpringBoot整合Web開發(fā)其中Json數(shù)據(jù)返回的實現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-09-09

