2013-12-12 147 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