spring MVC 静态资源访问问题。

只是_曾经 2013-09-18 04:07:12
问题描述:下面的文件中去掉<mvc:default-servlet-handler/>,不能访问静态文件。如果不去掉只能访问jsp和静态文件。
求解!


下面是spring mvc的配置


<?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"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<!-- 连接池配置 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="url"
value="jdbc:mysql://localhost:3306/myBatis?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull"></property>
<property name="username" value="root"></property>
<property name="password" value="root"></property>
<property name="maxActive" value="100"></property>
<property name="maxIdle" value="30"></property>
<property name="maxWait" value="500"></property>
<property name="defaultAutoCommit" value="true"></property>
</bean>
<!-- MyBatis的 sqlSessionFactory -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="configLocation" value="classpath:MyBatis-Configuration.xml"></property>
<property name="dataSource" ref="dataSource" />
</bean>
<!-- 以下是springMVC配置 -->
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>

<!-- 使Spring关注Annotation -->

<context:annotation-config />

<!-- 让Spring通过自动扫描来查询和管理Bean -->
<context:component-scan base-package="com.controller" />
<context:component-scan base-package="com.dao" />
<context:component-scan base-package="com.service.impl" />
<context:component-scan base-package="com.validator" />

<!-- 静态资源 -->
<mvc:default-servlet-handler/>
<!-- 拦截器配置-->
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/user/getUser/**" />
<bean class="com.Interceptor.UserInterceptor"></bean>
</mvc:interceptor>
</mvc:interceptors>

<!-- 总错误处理-->
<bean id="exceptionResolver" class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
<property name="defaultErrorView">
<value>error/error</value>
</property>
<property name="defaultStatusCode">
<value>500</value>
</property>
<property name="warnLogCategory">
<value>org.springframework.web.servlet.handler.SimpleMappingExceptionResolver</value>
</property>
</bean>
<!-- 配置事务管理器 -->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 配置事务的传播特性 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="save*" propagation="REQUIRED" />
<tx:method name="update*" propagation="REQUIRED" />
<tx:method name="delete*" propagation="REQUIRED" />
<tx:method name="query*" propagation="REQUIRED" />
<tx:method name="*" read-only="true" />
</tx:attributes>
</tx:advice>

<!-- 配置参与事务的类或方法 -->
<aop:config>
<aop:pointcut id="allServiceMethod" expression="execution(* com.service.*.*.*(..))" />
<aop:advisor pointcut-ref="allServiceMethod" advice-ref="txAdvice" />
</aop:config>


</beans>

...全文
780 12 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
aiwuhui 2014-10-27
  • 打赏
  • 举报
回复
引用 9 楼 shanxiuwei 的回复:
[quote=引用 6 楼 rui888 的回复:] 好些有3中吧,一种是 制定好jpg 啊等等,一种是mvc:resources ,还有一种是 mvc:default
问题已经解决。如果使用一下其中一个配置静态文件访问,需要添加两句配置!

<mvc:default-servlet-handler/> 
<!-- <mvc:resources mapping="/staticFile/**" location="/staticFile/"/>-->
需要添加的配置

<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"/>
    <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/>
可以参考篇文件http://www.blogjava.net/badqiu/archive/2009/09/22/296082.html[/quote] 罗嗦,你把上面两句去掉,换上这个 就OK了。 <mvc:annotation-driven />,这个配置就包含了上面你写的两个
骑驴找大爷 2014-07-08
  • 打赏
  • 举报
回复
写在哪儿 啊,9楼 问题已经解决。如果使用一下其中一个配置静态文件访问,需要添加两句配置! XML/HTML code ? 1 2 <mvc:default-servlet-handler/> <!-- <mvc:resources mapping="/staticFile/**" location="/staticFile/"/>--> 需要添加的配置 XML/HTML code ? 1 2 <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"/> <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAd
tony4geek 2013-09-22
  • 打赏
  • 举报
回复
对的。是的,哈哈必须要的。没注意看到。
只是_曾经 2013-09-22
  • 打赏
  • 举报
回复
引用 6 楼 rui888 的回复:
好些有3中吧,一种是 制定好jpg 啊等等,一种是mvc:resources ,还有一种是 mvc:default
问题已经解决。如果使用一下其中一个配置静态文件访问,需要添加两句配置!

<mvc:default-servlet-handler/> 
<!-- <mvc:resources mapping="/staticFile/**" location="/staticFile/"/>-->
需要添加的配置

<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"/>
    <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/>
可以参考篇文件http://www.blogjava.net/badqiu/archive/2009/09/22/296082.html
只是_曾经 2013-09-18
  • 打赏
  • 举报
回复
引用 6 楼 rui888 的回复:
好些有3中吧,一种是 制定好jpg 啊等等,一种是mvc:resources ,还有一种是 mvc:default
是的可以直接配置在web.xml中,把静态文件访问交个容器的默认servlet,但是必须每一种后缀都挨个配一遍。 这种方式是可以的,我想知道的是另外两种为什么出不行?
tony4geek 2013-09-18
  • 打赏
  • 举报
回复
好些有3中吧,一种是 制定好jpg 啊等等,一种是mvc:resources ,还有一种是 mvc:default
只是_曾经 2013-09-18
  • 打赏
  • 举报
回复
引用 4 楼 cbxjj 的回复:
<mvc:resources mapping="/images/**" location="/images/"/> 话说我们是这样访问静态文件的
其实这个我也试了。效果一样。只能访问静态文件。我的controller用的是注解,是不是有关系呢?
剑神一笑 2013-09-18
  • 打赏
  • 举报
回复
<mvc:resources mapping="/images/**" location="/images/"/> 话说我们是这样访问静态文件的
只是_曾经 2013-09-18
  • 打赏
  • 举报
回复
引用 2 楼 rui888 的回复:
加上的目的就是想访问静态图片。
只是为了访问静态文件。单也不能只让我访问今天文件吧。。
tony4geek 2013-09-18
  • 打赏
  • 举报
回复
加上的目的就是想访问静态图片。
只是_曾经 2013-09-18
  • 打赏
  • 举报
回复
Spring MVC 3.0实战指南》,参考《Spring 3.x企业应用开发实战》。 内容简介: 1、Spring MVC框架简介 2、HTTP请求地址映射 3、HTTP请求数据的绑定 4、数据转换、格式化、校验 5、数据模型控制 6、视图及解析器 7、其它 目录: Spring MVC 3.0新特性 Spring MVC框架结构 Spring MVC框架结构 框架的实现者 目录 HTTP请求映射原理 Spring MVC进行映射的依据 通过URL限定:URL表达式 通过URL限定:绑定{xxx}中的值 通过请求方法限定:请求方法 通过请求方法限定:代码示例 通过请求方法限定:模拟请求方法 通过请求/请求头参数限定:示例 通过请求/请求头参数限定:更多 目录 通过注解绑定:示意图 通过注解绑定:示例 通过注解绑定:小心抛出异常 使用命令/表单对象绑定 使用Servlet API对象作为入参 使用Spring的Servlet API代理类 使用IO对象作为入参 其他类型的参数 HttpMessageConverter HttpMessageConverter实现类 使用@RequestBody/@ResponseBody 使用HttpEntity/ResponseEntity 输出XML和JSON 使用HttpEntity/ResponseEntity 目录 数据绑定机理 数据类型转换 PropertyEditor依然有效 强大的ConversionService,让很多梦想成真 基于ConversionService体系,定义自定义的类型转换器 格式化:带格式字符串内部对象 相互转换 使用支持格式化的转换器 数据校验框架 JSR 303 数据校验框架 如何使用注解驱动的校验 使用校验功能时,处理方法要如何签名?? 校验错误信息存放在什么地方?? 页面如何显示错误信息 如何对错误信息进行国际化(1) 如何对错误信息进行国际化(2) 目录 数据模型访问结构 访问数据模型:ModelAndView 访问数据模型:@ModelAttribute 访问数据模型:Map及Model 访问数据模型:@SessionAttributes 一场由@SessionAttributes引发的血案... 如何避免@SessionAttributes引发的血案 目录 Spring MVC如何解析视图 视图解析器类型 基于协商的视图解析器 目录 本地化:基础原理 本地化:Spring MVC的本地化解析器 本地化:Spring MVC的本地化解析器 LocaleChangeInterceptor:通过URL参数指定 静态资源处理 静态资源处理:使REST风格的URL成为实现 静态资源处理:原理 静态资源处理:如何配置? 静态资源处理:如何配置? 物理静态资源路径映射逻辑资源路径 允许利用浏览器的缓存且不当心不同步 AQ?

81,120

社区成员

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

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