使用SpringMVC是遇到的问题:在配置文件里加入之后工程无法访问

大笑了几声 2014-10-27 04:38:41
为了解决SpringMVC在使用<url-pattern>/</url-pattern>时会屏蔽掉所有静态资源的问题,我在配置文件中加入了如下代码:<mvc:resources location="/img/" mapping="/img/**"/>
结果整个工程无法访问,求大神指点!
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">
<display-name>springMVC_01</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>

<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*:conf/springmvc-servlet.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>

springmvc-servlet.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"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
">

<context:annotation-config></context:annotation-config>
<mvc:resources location="/img/" mapping="/img/**"/>
<bean name="helloController" class="com.hupan.controller.HelloController"></bean>

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/" />
<property name="suffix" value=".jsp" />
</bean>

</beans>
...全文
1379 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
Sam.Shi 2017-06-14
  • 打赏
  • 举报
回复
引用 4 楼 yingyoulan 的回复:
[quote=引用 2 楼 shiyong1949 的回复:]
我有另外一个问题,就是配置了<mvc:resources mapping="/page/**" location="/WEB-INF/page/"/>之后 我可以通过路径:http://localhost:8080/项目名称/page/NewFile.html访问到这个html页面 可是同样里面的jsp页面却不可以访问是为什么?http://localhost:8080/项目名称/page/NewFile.jsp 是只对静态资源有效?可我的jsp页面也是写的静态的内容,无需通过控制器的呢?为什么不能这样访问? 除了把jsp页面改为html页面,有没有其他方法可以做到?[/quote] 这要看你项目的框架是怎么配置的,你看看有没有配置拦截器,另外下面这个配置是设置对静态资源访问的 <mvc:resources mapping="/css/**" location="/css/" cache-period="10"/> <mvc:resources mapping="/image/**" location="/image/" cache-period="10"/> <mvc:resources mapping="/js/**" location="/js/" cache-period="10"/>
yingyoulan 2017-06-13
  • 打赏
  • 举报
回复
引用 2 楼 shiyong1949 的回复:
我有另外一个问题,就是配置了<mvc:resources mapping="/page/**" location="/WEB-INF/page/"/>之后 我可以通过路径:http://localhost:8080/项目名称/page/NewFile.html访问到这个html页面 可是同样里面的jsp页面却不可以访问是为什么?http://localhost:8080/项目名称/page/NewFile.jsp 是只对静态资源有效?可我的jsp页面也是写的静态的内容,无需通过控制器的呢?为什么不能这样访问? 除了把jsp页面改为html页面,有没有其他方法可以做到?
liushuang6018 2017-03-15
  • 打赏
  • 举报
回复
引用 2 楼 shiyong1949 的回复:
刚开始没有配置mvc:resources,controller能够正确访问,但是由于web.xml使用/拦截了所有的请求,所以静态资源访问不上 增加mvc:resources之后,静态资源是能访问上了,但是注解配置的controller却又找不到了 原因是少了 <mvc:annotation-driven />的配置,在没有配置mvc:resources的时候没有问题,一旦配置了mvc:resources,注解方式的url就没有加载 补上 <mvc:annotation-driven/> 就可以解决问题
亲测有效,感谢层主
Sam.Shi 2016-08-31
  • 打赏
  • 举报
回复
刚开始没有配置mvc:resources,controller能够正确访问,但是由于web.xml使用/拦截了所有的请求,所以静态资源访问不上 增加mvc:resources之后,静态资源是能访问上了,但是注解配置的controller却又找不到了 原因是少了 <mvc:annotation-driven />的配置,在没有配置mvc:resources的时候没有问题,一旦配置了mvc:resources,注解方式的url就没有加载 补上 <mvc:annotation-driven/> 就可以解决问题

10,606

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 其他
社区管理员
  • 其他
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧