servlet-context.xml没有将资源映射为定向的mvc:resources

时间:2018-01-02 07:11:44

标签: spring spring-mvc

我已经从AJAX下载了themeforest模板。我希望保持模板文件夹结构不变,以便我可以轻松地进行更新和修补。

我的文件夹结构是

  - WebContent
     servlet-context.xml
     - WEB-INF\
         web.xml
         - Template\
           css\
           img\
        - views\
          index.html

我的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"

    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
    version="3.0">
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>root-context.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <listener>
        <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
    </listener>
    <servlet>
        <servlet-name>appServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>servlet-context.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>appServlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
    <filter>
        <filter-name>AuthenticationFilter</filter-name>
        <filter-class>MyAuthenticationFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>AuthenticationFilter</filter-name>
        <url-pattern>*</url-pattern>
    </filter-mapping>
    <session-config>
        <session-timeout>30</session-timeout>
    </session-config>
    <listener>
        <listener-class>MyMnoxWebContextListner</listener-class>
    </listener>
</web-app>

我的servlet-context.xml是

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

    <!-- DispatcherServlet Context: defines this servlet's request-processing 
        infrastructure -->

    <!-- Enables the Spring MVC @Controller programming model -->
    <mvc:annotation-driven />  
    <mvc:resources mapping="img/**" location="/WEB-INF/Template/img/" />
    <mvc:resources mapping="css/**" location="/WEB-INF/Template/css/" />
    <mvc:resources mapping="js/**" location="/WEB-INF/Template/js//" />

    <!-- Resolves views selected for rendering by @Controllers to .jsp resources 
        in the /WEB-INF/views directory -->    
    <mvc:default-servlet-handler />     
    <!-- <mvc:resources mapping="**" location="/WEB-INF/views/" />
 -->    
    <bean           class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/views/" />
        <property name="suffix" value=".html" />
    </bean>

    <!-- <bean id="springSecurityFilterChain" class="org.springframework.security.web.FilterChainProxy"> 
        properties </bean> -->

    <!-- <context:component-scan base-package="net.codejava.spring" /> -->
    <context:component-scan base-package="Mycontrollers" />

</beans>

我的login.html包含

        

    <link rel="stylesheet" type="text/css" media="screen" href="css/some.css">
    <link rel="stylesheet" type="text/css" media="screen" href="css/somemore.css">
    <link rel="stylesheet" type="text/css" media="screen" href="css/alotmore.css">

在servlet-context.xml中,如果我发表评论

<mvc:default-servlet-handler /> 

我得到了

I get HTTP Status 404 -
type Status report
message
description The requested resource is not available.
Apache Tomcat/8.0.41

如果我取消注释该行,login.html会打开,但img/中的路径(css/login.html)不会被映射。

这也意味着正在读取servlet-context.xml。控制台打印

2018-01-02 12:35:58 INFO  SimpleUrlHandlerMapping:315 - Mapped URL path [/img/**] onto handler 'org.springframework.web.servlet.resource.ResourceHttpRequestHandler#0'
2018-01-02 12:35:58 INFO  SimpleUrlHandlerMapping:315 - Mapped URL path [/css/**] onto handler 'org.springframework.web.servlet.resource.ResourceHttpRequestHandler#1'
2018-01-02 12:35:58 INFO  SimpleUrlHandlerMapping:315 - Mapped URL path [/js/**] onto handler 'org.springframework.web.servlet.resource.ResourceHttpRequestHandler#2'

我已经读过许多SO问题,并且已经走到了尽头。

注意:一旦我得到一些答案,我就会把问题变得更轻,并删除不必要的xml内容。

0 个答案:

没有答案