在spring mvc中添加css文件

时间:2014-12-16 12:27:16

标签: spring-mvc

我想在spring mvc project中添加css文件。我正在使用myeclipse。

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

中添加了spring-servlet.xml

当我要去跑步时,我得到了异常

例外

  

javax.servlet.ServletException:servlet spring的Servlet.init()抛出异常       org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:104)       org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:261)       org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844)       org.apache.coyote.http11.Http11Protocol $ Http11ConnectionHandler.process(Http11Protocol.java:581)       org.apache.tomcat.util.net.JIoEndpoint $ Worker.run(JIoEndpoint.java:447)       java.lang.Thread.run(Thread.java:619)   根本原因

     

org.springframework.beans.factory.parsing.BeanDefinitionParsingException:配置问题:找不到元素[resources]的BeanDefinitionParser   违规资源:ServletContext资源[/WEB-INF/spring-servlet.xml]       org.springframework.beans.factory.parsing.FailFastProblemReporter.fatal(FailFastProblemReporter.java:59)       org.springframework.beans.factory.parsing.ReaderContext.fatal(ReaderContext.java:68)       org.springframework.beans.factory.parsing.ReaderContext.fatal(ReaderContext.java:55)       org.springframework.beans.factory.xml.NamespaceHandlerSupport.findParserForElement(NamespaceHandlerSupport.java:84)       org.springframework.beans.factory.xml.NamespaceHandlerSupport.parse(NamespaceHandlerSupport.java:73)       org.springframework.beans.factory.xml.BeanDefinitionParserDelegate.parseCustomElement(BeanDefinitionParserDelegate.java:1335)       org.springframework.beans.factory.xml.BeanDefinitionParserDelegate.parseCustomElement(BeanDefinitionParserDelegate.java:1325)       org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.parseBeanDefinitions(DefaultBeanDefinitionDocumentReader.java:136)       org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.registerBeanDefinitions(DefaultBeanDefinitionDocumentReader.java:93)       org.springframework.beans.factory.xml.XmlBeanDefinitionReader.registerBeanDefinitions(XmlBeanDefinitionReader.java:493)       org.springframework.beans.factory.xml.XmlBeanDefinitionReader.doLoadBeanDefinitions(XmlBeanDefinitionReader.java:390)       org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:334)       org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:302)       org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:143)       org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:178)       org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:149)       org.springframework.web.context.support.XmlWebApplicationContext.loadBeanDefinitions(XmlWebApplicationContext.java:124)       org.springframework.web.context.support.XmlWebApplicationContext.loadBeanDefinitions(XmlWebApplicationContext.java:93)       org.springframework.context.support.AbstractRefreshableApplicationContext.refreshBeanFactory(AbstractRefreshableApplicationContext.java:130)       org.springframework.context.support.AbstractApplicationContext.obtainFreshBeanFactory(AbstractApplicationContext.java:465)       org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:395)       org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:443)       org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:459)       org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:340)       org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:307)       org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:127)       javax.servlet.GenericServlet.init(GenericServlet.java:212)       org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:104)       org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:261)       org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844)       org.apache.coyote.http11.Http11Protocol $ Http11ConnectionHandler.process(Http11Protocol.java:581)       org.apache.tomcat.util.net.JIoEndpoint $ Worker.run(JIoEndpoint.java:447)       java.lang.Thread.run(Thread.java:619)

我的 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:p="http://www.springframework.org/schema/p"
 xmlns:context="http://www.springframework.org/schema/context"
 xmlns:mvc="http://www.springframework.org/schema/mvc"
 xsi:schemaLocation="http://www.springframework.org/schema/beans
 http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
 http://www.springframework.org/schema/mvc 
 http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
 http://www.springframework.org/schema/context
 http://www.springframework.org/schema/context/spring-context-3.0.xsd">
 <mvc:resources mapping="/resources/**" location="/WEB-INF/resources/" /> 
 <context:component-scan  base-package="com.arobol.controller" /> 
  <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
     <property name="prefix" value="/WEB-INF/jsp/" />  
     <property name="suffix" value=".jsp" />  
  </bean>
</beans>

2 个答案:

答案 0 :(得分:2)

确保xml文件的xml架构与你的spring版本相对应,因此对于spring 3.0应该是类似的

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

stacktrace问题说

Configuration problem: Cannot locate BeanDefinitionParser for element [resources] Offending resource: ServletContext resource [/WEB-INF/spring-servlet.xml] 

看起来好像你的罐子和包含的架构之间存在错误匹配

更新

更好的是,包括没有版本的XSD,它将使架构与您在应用中使用的架构相匹配。像

这样的东西
<?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"
     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/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">

答案 1 :(得分:0)

由于您说在添加<mvc:resources mapping="/resources/**" location="/WEB-INF/resources/" />

后收到错误

尝试删除<mvc:

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/mvc 
http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<resources mapping="/resources/**" location="/WEB-INF/resources/" /> 
<context:component-scan  base-package="com.arobol.controller" /> 
 <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
 <property name="prefix" value="/WEB-INF/jsp/" />  
 <property name="suffix" value=".jsp" />  
</bean>
</beans>

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


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

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

    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">  

        <property name="prefix" value="/WEB-INF/jsp/" />
        <property name="suffix" value=".jsp" /> 
    </bean> 
  </beans>

调用控制器异常后发生。