2014-06-28 228 views
1

有人可以幫我解決這個問題。我是新來的春天所以..在這種情況下,問題是 url =「localhost:8080/projectname/stuff」一切工作正常資源被導入,但當我添加「/」或任何@pathVariable「/ stuff/{id }」,ressources不再進口,所以我沒有像無CSS什麼春季資源映射mvc 3

爲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> 

彈簧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> 

回答

0

請參閱您的資源,這種方式: 加${pageContext.request.contextPath}之前assets/

例如:

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

@ user3786833你會把它標記爲答案嗎? –

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> 
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> 
0

我說:

$ {} pageContext.request.contextPath

之前資產似乎解決了問題謝謝 -