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




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