Springmvc訪問靜態資源

今天在搭建一個Springmvc的demo的時候,開始的時候只是一個普通的Java web項目,訪問html文件的時候是可以訪問的,但是後來配置Springmvc的時候,如果像之前一樣訪問html靜態資源的時候就不能夠訪問了。

web.xml配置如下

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
	id="WebApp_ID" version="2.5">
	
	<!--配置Spring -->
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>
	
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath:spring/applicationContext.xml</param-value>
	</context-param>
	
	<!-- 配置Springmvc -->
	<servlet>
		<servlet-name>springmvc</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>classpath:spring/spring-mvc.xml</param-value>
		</init-param>
		<load-on-startup>1</load-on-startup>
	</servlet>
	<servlet-mapping>
		<servlet-name>springmvc</servlet-name>
		<url-pattern>/</url-pattern>
	</servlet-mapping>
	
</web-app>

spring-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:mvc="http://www.springframework.org/schema/mvc"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
	       http://www.springframework.org/schema/beans/spring-beans.xsd
	       http://www.springframework.org/schema/mvc
    	   http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
    	   http://www.springframework.org/schema/aop
           http://www.springframework.org/schema/aop/spring-aop.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context.xsd"
	default-autowire="byName">
	
	<!-- 默認的註解映射的支持 -->  
    <mvc:annotation-driven />
    
    <!-- 自動掃描該包,使SpringMVC認爲包下用了@controller註解的類是控制器 -->  
    <context:component-scan base-package="com.lovin" />
    
    <!-- 定義跳轉的文件的前後綴 ,視圖模式配置 -->  
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">  
        <!-- 這裏的配置我的理解是自動給後面action的方法return的字符串加上前綴和後綴,變成一個 可用的url地址 -->  
        <property name="prefix" value="" />  
        <property name="suffix" value=".html" />  
    </bean>
    
</beans>

整個項目的目錄結構如下:



這個時候啓動項目  訪問

http://localhost:8088/SSIDemo/view/excel.html  是會拋出404的錯誤,具體的原因是因爲我們web.xml中配置的

<url-pattern>/</url-pattern>
是捕獲所有的url,這個時候我們就得不到對應的解析後的文件,所以拋出404,在此只是做一個簡要的描述,大家可以具體去研究springmvc的源代碼和分析具體的原因



解決方法如下:

1、將web.xml中的配置捕獲的路徑從  /  修改成 *.do  不過由於 *.do已經不適用目前比較流行的rest風格的url,

      所以這種方式不推薦

2、第二種方式 修改spring-mvc.xml中 添加 

<mvc:resources location="/view/" mapping="/view/**"/>
<mvc:default-servlet-handler />

我們的靜態頁面就在 view中,這個時候我們就可以進行訪問


當然還有其他的解決方法,目前我使用的是這種方式

可以參考 http://www.cnblogs.com/soundcode/p/6368259.html

               http://www.cnblogs.com/tyoyi/p/5556664.html




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章