在spring mvc 3中的资源映射

时间:2014-06-28 23:47:44

标签: java spring java-ee spring-mvc mapping

有人可以帮我解决这个问题。我刚开始如此......问题是 在这种情况下url =“localhost:8080 / projectname / stuff”一切工作正常导入资源但是当我添加“/”或任何@pathVariable“/ stuff / {id}”时,不再导入资源,所以我没有图像没有css什么

spring-servlet.xml

     <import resource="data-config.xml"/>

     <mvc:resources mapping="/assets/**" location="/WEB-INF/assets/" />

     <tx:annotation-driven transaction-manager="transactionManager"/>
          <bean id="propertyConfigurer"
    class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
        p:location="classpath:/META-INF/spring/jdbc.properties" />
    <bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />
        <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="${db.driver}" />  
        <property name="url" value="${db.url}"/>
        <property name="username" value="${db.username}"/>
        <property name="password" value="${db.password}"/>
    </bean>
    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
      <property name="entityManagerFactory" ref="entityManagerFactory" />
    </bean>

    <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="jpaVendorAdapter" ref="jpaAdapter" />
        <property name="persistenceUnitName" value="persistence" />
        <property name="loadTimeWeaver">
    <bean class="org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver" />
        </property>
    </bean>

     <bean id="jpaAdapter" class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
        <property name="databasePlatform" value="org.hibernate.dialect.MySQL5InnoDBDialect" />
        <property name="generateDdl" value="true" />
        <property name="showSql" value="true" />
    </bean>

<context:annotation-config />
<mvc:default-servlet-handler/>
<context:component-scan base-package="com.bd"  annotation-config="true" /> 
<mvc:annotation-driven /> 
<bean id="viewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver">
     <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
     <property name="prefix" value="/WEB-INF/jsp/"/>
     <property name="suffix" value=".jsp"/>
</bean>
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/>
<bean id="localeChangeInterceptor"
class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
   <property name="paramName" value="language" />
</bean>
<bean id="handlerMapping"
class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
    <property name="interceptors">
    <ref bean="localeChangeInterceptor" />
    </property>    
</bean> 

</beans>

spring-security.xml

  <http pattern="/login" security="none"/>
  <http pattern="/assets/**" security="none"/>


 <http auto-config="true"  use-expressions="true">
        <access-denied-handler error-page="/denied" />
        <intercept-url pattern="/login" access="permitAll"></intercept-url>
        <intercept-url pattern="/logout" access="permitAll"></intercept-url>
        <intercept-url pattern="/" access="isAnonymous()"></intercept-url>
        <intercept-url pattern="/admin*" access="isAuthenticated()"/>
        <intercept-url pattern="/admin/*" access="isAuthenticated()"/>
        <form-login login-page="/login" default-target-url="/admin/question" authentication-failure-url="/login"
        username-parameter="username"
        password-parameter="password"
        />


        <logout logout-success-url="/login?logout" invalidate-session="true"  delete-cookies="JSESSIONID"/>
    </http>

<beans:bean id="userDetailsService" class="com.bd.service.implement.UserDetailsServiceImpl"></beans:bean>

<authentication-manager>
<authentication-provider user-service-ref="userDetailsService">
<password-encoder hash="md5"/>
</authentication-provider>
</authentication-manager>
</beans:beans>

4 个答案:

答案 0 :(得分:0)

以这种方式参考您的资源: 在${pageContext.request.contextPath}

之前添加assets/

示例:

<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/assets/PATH_TO_YOUR_CSS_FILE/style.css" >

答案 1 :(得分:0)

您的配置存在多种方面的缺陷。您已<mvc:annotation-driven />已配置RequestMappingHandlerMapping。接下来,DefaultAnnotationHandlerMapping已弃用并替换为RequestMappingHandlerMapping,目前他们互相竞争。修复很简单,同时删除它们。

<context:compont-scan />已隐含<context:annotation-driven />且已包含PersistenceAnnotationBeanPostProcessor

Hibernate不需要加载时间编织器,所以你应该通过删除它。

删除所有杂乱的内容,请注意以下事项。

<beans ...>

    <import resource="data-config.xml"/>

    <context:component-scan base-package="com.bd" /> 
    <context:property-placeholder location="classpath:/META-INF/spring/jdbc.properties" />

    <mvc:annotation-driven />

    <mvc:interceptors>
        <bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
            <property name="paramName" value="language" />
        </bean>
    </mvc:interceptors>

    <mvc:default-servlet-handler/> 
    <mvc:resources mapping="/assets/**" location="/WEB-INF/assets/" />

    <tx:annotation-driven transaction-manager="transactionManager"/>

    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="${db.driver}" />  
        <property name="url" value="${db.url}"/>
        <property name="username" value="${db.username}"/>
        <property name="password" value="${db.password}"/>
    </bean>

    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
      <property name="entityManagerFactory" ref="entityManagerFactory" />
    </bean>

    <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="jpaVendorAdapter" ref="jpaAdapter" />
        <property name="persistenceUnitName" value="persistence" />
    </bean>

     <bean id="jpaAdapter" class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
        <property name="databasePlatform" value="org.hibernate.dialect.MySQL5InnoDBDialect" />
        <property name="generateDdl" value="true" />
        <property name="showSql" value="true" />
    </bean>

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

</beans>

答案 2 :(得分:0)

我按照你对spring-servlet.xml的建议 该文件现在看起来像这样

 <import resource="data-config.xml"/>

     <mvc:resources mapping="/assets/**" location="/WEB-INF/assets/" />

     <tx:annotation-driven transaction-manager="transactionManager"/>

     <bean id="propertyConfigurer"
        class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
        p:location="classpath:/META-INF/spring/jdbc.properties" />


     <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="${db.driver}" />  
        <property name="url" value="${db.url}"/>
        <property name="username" value="${db.username}"/>
        <property name="password" value="${db.password}"/>
    </bean>

    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="entityManagerFactory" />
    </bean>

    <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="jpaVendorAdapter" ref="jpaAdapter" />
        <property name="persistenceUnitName" value="persistence" />

    </bean>

     <bean id="jpaAdapter" class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
        <property name="databasePlatform" value="org.hibernate.dialect.MySQL5InnoDBDialect" />
        <property name="generateDdl" value="true" />
        <property name="showSql" value="true" />
    </bean>

    <context:component-scan base-package="com.bd"  annotation-config="true" /> 

    <mvc:annotation-driven /> 


    <bean id="viewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver">
        <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
        <property name="prefix" value="/WEB-INF/jsp/"/>
        <property name="suffix" value=".jsp"/>
    </bean>

    </beans>

答案 3 :(得分:0)

我补充说:

  

$ {pageContext.request.contextPath}

之前资产似乎解决了问题谢谢 -