Spring MVC項目配置項目主頁

但願訪問項目根路徑的時候就跳轉到spring控制的首頁html

一種作法是Spring處理"*.html路徑",另外一種作法是Spring處理「/」路徑java

一:Spring處理"*.html路徑":web

一、修改web.xml的歡迎頁,手動指定歡迎頁,後綴就是url-pattern裏的配置,這樣會把首頁的訪問交給spring處理spring

<welcome-file>index.html</welcome-file>

 

<servlet>
    <servlet-name>antsclub</servlet-name>
	<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
	<load-on-startup>3</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>antsclub</servlet-name>
    <url-pattern>*.html</url-pattern>
</servlet-mapping>
	
<welcome-file-list>
    <welcome-file>index.html</welcome-file>
</welcome-file-list>

 

spring會自動去找antsclub-servlet.xml配置文件,這個文件不存在會報錯瀏覽器

二、controller處理路徑spring-mvc

@RequestMapping(value = "/index.html")
public String loginPage(){
    return "login";
}

 

這裏的login會跳轉到指定的view,view的路徑和格式在antsclub-servlet.xml裏面配置安全

<!-- 配置視圖解析器,將ModelAndView及字符串解析爲具體的頁面 -->
<bean
    class="org.springframework.web.servlet.view.InternalResourceViewResolver"
    p:viewClass="org.springframework.web.servlet.view.JstlView" 
	p:prefix="/WEB-INF/jsp/"
	p:suffix=".jsp" />

WEB-INF被稱爲安全目錄,該目錄下的文件只有服務端能夠訪問,客戶端不能訪問。服務器

該目錄下的jsp只能Forward不能Redirect。mvc

forward是服務器請求資源,服務器直接訪問目標地址的URL,把那個URL的響應內容讀取過來,而後把這些內容再發給瀏覽器,瀏覽器根本不知道服務器發送的內容是從哪兒來的,因此它的地址欄中仍是原來的地址。
redirect就是服務端根據邏輯,發送一個狀態碼,告訴瀏覽器從新去請求那個地址,通常來講瀏覽器會用剛纔請求的全部參數從新請求,地址欄會變app

2、Spring處理「/」路徑的步驟:

一、web.xml配置

<servlet-mapping>
		<servlet-name>tsingyu</servlet-name>
		<url-pattern>/</url-pattern>
	</servlet-mapping>

二、Controller代碼:

@RequestMapping(value = "/")
	public String index(){
		return "login";
	}

可是這樣處理會有一個問題:靜態資源訪問不到了,報404錯誤。

解決辦法(針對spring3.0.5適用):修改ApplicationContext-mvc.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:p="http://www.springframework.org/schema/p"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
       http://www.springframework.org/schema/mvc 
	   http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
       http://www.springframework.org/schema/context 
       http://www.springframework.org/schema/context/spring-context-3.0.xsd">
       
    <mvc:annotation-driven/>	
    <!-- 掃描web包,應用Spring的註解 -->
	<context:component-scan base-package="cn.tsingyu.spring.example.controller"/>
	<mvc:resources mapping="/static/**" location="/static/" />
	<!-- 配置視圖解析器,將ModelAndView及字符串解析爲具體的頁面 -->
	<bean
		class="org.springframework.web.servlet.view.InternalResourceViewResolver"
		p:viewClass="org.springframework.web.servlet.view.JstlView" 
		p:prefix="/WEB-INF/jsp/"
		p:suffix=".jsp" />
</beans>

其中mvc部分是新增的內容包括

xmlns:mvc="http://www.springframework.org/schema/mvc"

http://www.springframework.org/schema/mvc 
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd

<mvc:annotation-driven/>
<mvc:resources mapping="/static/**" location="/static/" />

附:web.xml中url-pattern匹配規則

相關文章
相關標籤/搜索