2013-12-12 145 views
2

我已經看過很多解決方案在這個網站和其他許多人的這個問題,但似乎沒有爲我工作。所以我再次把這個普遍的問題是如何與Spring MVC的配置CSS在春季添加CSS mvc

這裏有我的配置

的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_3_0.xsd" 
    id="WebApp_ID" version="3.0"> 

    <context-param> 
     <param-name>log4jConfigLocation</param-name> 
     <param-value>classpath:log4j.xml</param-value> 
    </context-param> 
    <context-param> 
     <param-name>contextConfigLocation</param-name> 
     <param-value> 
      /WEB-INF/spring-servlet.xml 
      /WEB-INF/security-context.xml 
     </param-value> 
    </context-param> 
    <listener> 
     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 
    </listener> 
    <listener> 
     <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class> 
    </listener> 
    <!-- Define a filter to enable Spring Security, be sure to use the suggested 
     name 'springSecurityFilterChain' --> 
    <filter> 
     <filter-name>springSecurityFilterChain</filter-name> 
     <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class> 
    </filter> 
    <filter-mapping> 
     <filter-name>springSecurityFilterChain</filter-name> 
     <url-pattern>/*</url-pattern> 
    </filter-mapping> 

    <servlet> 
     <servlet-name>spring</servlet-name> 
     <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
     <load-on-startup>1</load-on-startup> 
    </servlet> 
    <servlet-mapping> 
     <servlet-name>spring</servlet-name> 
     <url-pattern>/</url-pattern> 
    </servlet-mapping> 
    <servlet-mapping> 
     <servlet-name>default</servlet-name> 
     <url-pattern>*.css</url-pattern> 
    </servlet-mapping> 

    <welcome-file-list> 
     <welcome-file>index.html</welcome-file> 
     <welcome-file>index.htm</welcome-file> 
     <welcome-file>index.jsp</welcome-file> 
     <welcome-file>default.html</welcome-file> 
     <welcome-file>default.htm</welcome-file> 
     <welcome-file>default.jsp</welcome-file> 
    </welcome-file-list> 
</web-app> 

爲spring-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:aop="http://www.springframework.org/schema/aop" 
    xmlns:context="http://www.springframework.org/schema/context" 
    xmlns:jee="http://www.springframework.org/schema/jee" xmlns:lang="http://www.springframework.org/schema/lang" 
    xmlns:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx" 
    xmlns:util="http://www.springframework.org/schema/util" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd 
     http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd 
     http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd 
     http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd 
     http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang.xsd 
     http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd 
     http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd"> 



    <context:annotation-config /> 
    <context:component-scan base-package="com.scratch" /> 
    <!-- config for back button wont work secured element --> 
    <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"> 
     <property name="cacheSeconds" value="0" /> 
    </bean> 


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

    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" 
     destroy-method="close" p:driverClassName="${jdbc.driverClassName}" 
     p:url="${jdbc.databaseurl}" p:username="${jdbc.username}" p:password="${jdbc.password}" /> 
    <bean id="sessionFactory" 
     class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> 
     <property name="dataSource" ref="dataSource" /> 
     <property name="configLocation"> 
      <value>classpath:hibernate.cfg.xml</value> 
     </property> 
     <property name="configurationClass"> 
      <value>org.hibernate.cfg.AnnotationConfiguration</value> 
     </property> 
     <property name="hibernateProperties"> 
      <props> 
       <prop key="hibernate.dialect">${jdbc.dialect}</prop> 
       <prop key="hibernate.show_sql">true</prop> 
      </props> 
     </property> 
    </bean> 
    <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> 
    <tx:annotation-driven /> 
    <bean id="transactionManager" 
     class="org.springframework.orm.hibernate3.HibernateTransactionManager"> 
     <property name="sessionFactory" ref="sessionFactory" /> 
    </bean> 
</beans> 

我css文件位置

src/main/webapp/css/test.css 

JSP代碼

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> 
<%@taglib uri="http://www.springframework.org/tags" prefix="spring"%> 
<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%> 
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> 
<%@ taglib uri="http://www.springframework.org/security/tags" prefix="sec"%> 
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %> 
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
<html> 
<head> 
<title>Login</title> 
<link href="${pageContext.request.contextPath}/css/test.css" rel="stylesheet" type="text/css" /> 

</head> 
<body> 


    <div class="test"><h1>test</h1></div> 
</body> 
</html> 

test.css

body {color:blue;} 
h1 {color:#00ff00;} 

請告訴我在哪裏犯錯 謝謝

+1

將您的css文件夾WEB-INF內,添加,它將起作用 –

+0

並更改web.XML中的映射,以便將每個請求映射到spring調度程序servlet。 – Ralph

+0

並在jsp中使用而不是$ {pageContext.request.contextPath} /css/test.c – Ralph

回答

3

假設你使用最新春季版刪除您定義爲AnnotationMethodHandlerAdapter和將其替換爲<mvc:annotation-driven />。在旁邊添加<mvc:default-servlet-handler />。在你的web.xml中,去掉default servlet的映射*.css

<mvc:annotation-driven /> 
<mvc:default-servlet-handler /> 

如果你動了你的WEB-INF文件夾內的css文件旁邊一個<mvc:default-servlet-handler /><mvc:resources mapping="/css/**" location="/WEB-INF/css/" />元素添加到您的配置。 (從你的web.xml中刪除默認映射後的Ofcourse)。

下面的部分必須從您的web.xml中刪除,這將由<mvc:default-servlet-handler /><mvc:resources ... />處理。

<servlet-mapping> 
    <servlet-name>default</servlet-name> 
    <url-pattern>*.css</url-pattern> 
</servlet-mapping> 


其他改進
一對夫婦的其他改進(不涉及您的問題,但清理你的配置)。

  1. <context:annotation-driven />已經被<context:component-scan />
  2. 使用隱含的<context:property-placeholder />代替了PropertyPlaceholderConfigurer直接
  3. 更換LocalSessionFactoryBeanAnnotationSessionFactoryBean那麼你可以刪除configurationClass的顯式設置。
  4. 而不是UrlBasedViewResolver使用InternalResourceViewResolver並刪除明確的設置viewClass

離開這樣的事情

<?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:mvc="http://www.springframework.org/schema/mvc" 
    xmlns:p="http://www.springframework.org/schema/p" 
    xmlns:tx="http://www.springframework.org/schema/tx" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd 
     http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd 
     http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd 
     http://www.springframework.org/schema/mvc http://www.springframework.org/schema/util/spring-mvc.xsd"> 

    <context:component-scan base-package="com.scratch" /> 

    <mvc:annotation-driven /> 
    <mvc:default-servlet-handler /> 

    <context:property-placeholder location="/WEB-INF/jdbc.properties"/> 

    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" 
     destroy-method="close" p:driverClassName="${jdbc.driverClassName}" 
     p:url="${jdbc.databaseurl}" p:username="${jdbc.username}" p:password="${jdbc.password}" /> 

    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.AnnotationSessionFactoryBean"> 
     <property name="dataSource" ref="dataSource" /> 
     <property name="configLocation" value="classpath:hibernate.cfg.xml" /> 
     <property name="hibernateProperties"> 
      <props> 
       <prop key="hibernate.dialect">${jdbc.dialect}</prop> 
       <prop key="hibernate.show_sql">true</prop> 
      </props> 
     </property> 
    </bean> 

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

    <tx:annotation-driven /> 

    <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> 
     <property name="sessionFactory" ref="sessionFactory" /> 
    </bean> 

</beans> 

你也有豆複製一個問題。您的ContextLoaderListenerDispatcherServlet都會加載spring-servlet.xml文件,導致複製其中的所有bean。一般來說,你應該拆分你的通用bean(服務,dao,基礎設施)和你的web相關的bean(處理程序,視圖),並分別使用ContextLoaderListenerDispatcherServlet加載它們。

+0

嘿,我是新來的春天。如果它不是太麻煩,那麼你能否解釋你的配置如何更清潔。最後你寫了關於重複的豆。如何解決這個問題? – LynAs

-1

你應該將你的源到WEB-INF出於安全原因:WEB-INF/CSS/test.css

在爲spring-servlet.xml

<mvc:annotation-driven> 
<mvc:resources location="/WEB-INF/css/test.css" mapping="/css/**"> 

在你的JSP

<link href="css/test.css" rel="stylesheet"> 
3

spring-servlet.xml添加

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

web.xml刪除

<servlet-mapping> 
    <servlet-name>default</servlet-name> 
    <url-pattern>*.css</url-pattern> 
</servlet-mapping> 

終於在serurity-context.xml添加

<http pattern="/css/**" security="none" /> 
+0

只是想添加這爲我工作,但我沒有添加security =「none」標記。這給我一個錯誤。我還看到了很多教程,說的地方CSS下WebContent,但它從來沒有爲我工作。而是將其放入WEB-INF作品中。 – ishanbakshi