2013年9月22日星期日

spring MVC static resource access issues.

 This post last edited by the shanxiuwei on 2013-09-18 16:08:30
Problem Description : The following file removed , can not access static files. If you do not remove the only access jsp and static files.
solved !


Here is the configuration of 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&amp;characterEncoding=UTF-8&amp;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>


------ Solution ------------------------------------- -------
better with 3 bars , one is making a good jpg ah , etc. , one is mvc: resources, there is a mvc: default
- ----- For reference only ---------------------------------------

------ For reference only ---------------------------------------

plus is to seek access to static pictures.
------ For reference only -------------------------------------- -


just to access a static file. One can not just let me access the files it today . .
------ For reference only -------------------------------------- -

saying this is how we access the static files
------ For reference only -------------------------- -------------

In fact, I have tried this . The same effect. Can only access static files. My controller using annotations , is not there with it ?
------ For reference only -------------------------------------- -


that can be directly configured in web.xml , static file access to pay the default container servlet, but you must have one by one with each suffix again.
This way it can, and I want to know is how the other two not ?
------ For reference only -------------------------------------- -
  The reply deleted by an administrator at 2013-09-19 09:11:34

------ For reference only ---------------------------------- -----


problem has been resolved . If you click one of the configuration using static file access, you need to add two configuration !

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


need to add configuration

<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"/>
    <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/>

can refer to the chapter file http://www.blogjava.net/badqiu/archive/2009/09/22/296082.html
------ For reference only ------- --------------------------------
right. Yes, ha ha must want . Did not pay attention to see .

没有评论:

发表评论