您好,登錄后才能下訂單哦!
本篇內(nèi)容介紹了“springMVC路由跳轉(zhuǎn)怎么實現(xiàn)”的有關(guān)知識,在實際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領(lǐng)大家學(xué)習一下如何處理這些情況吧!希望大家仔細閱讀,能夠?qū)W有所成!
實現(xiàn)目標:使用springMVC前端控制器,跳轉(zhuǎn)到WEB-INF的templates下面的前端頁面
圖示
1.目錄結(jié)構(gòu)
2.創(chuàng)建一個maven的webapp項目,創(chuàng)建好之后記得把index.jsp文件刪除,否i則會首先跳到這個文件,我們要用前端控制器轉(zhuǎn)發(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的初始化時間提前到服務(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" >訪問目標</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路由跳轉(zhuǎn)怎么實現(xiàn)”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識可以關(guān)注億速云網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實用文章!
免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。