在DispatcherServlet中找不到带有URI [/FirstSpringMVCProject/stylegreen.css]的HTTP请求的映射,其名称为' spring-dispatcher'

时间:2016-06-22 16:58:06

标签: java spring jsp spring-mvc

我试图通过更改AdmissionForm.jsp文件中h3标记的内容来提供一些主题多重支持的主页,但是在点击红色或绿色链接后我遇到了问题,我收到以下警告:< / p>

No mapping found for HTTP request with URI [/FirstSpringMVCProject/stylegreen.css] in DispatcherServlet with name 'spring-dispatcher'
or
No mapping found for HTTP request with URI [/FirstSpringMVCProject/stylered.css] in DispatcherServlet with name 'spring-dispatcher'

AdmissionForm.jsp文件中的一些代码

<%@taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%@taglib prefix="spring" uri="http://www.springframework.org/tags" %>
<html>
<body>
<a href="/FirstSpringMVCProject/admissionForm.html?sitelanguage=en"> English </a> |
<a href="/FirstSpringMVCProject/admissionForm.html?sitelanguage=de"> German </a>

    <!--Added tut 27 for differnt themes -->
    <link rel="stylesheet" href="<spring:theme code='styleSheet'/>" type="text/css" />

    <p> <a href="/FirstSpringMVCProject/admissionForm.html?siteTheme=green">Green</a> |
        <a href="/FirstSpringMVCProject/admissionForm.html?siteTheme=red">Red</a> </p>

    <h1><spring:message code="label.headerMessage" /></h1>

    <h3> <spring:message code="label.admissionForm" /></h3>

</body>
</html>

弹簧调度-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: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.xsd">


    <context:component-scan base-package="com.stack" />
    <mvc:annotation-driven />

    <!-- Added -->
    <mvc:interceptors>

        <bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
            <property name="paramName" value="sitelanguage" />
        </bean>

        <!--Added tut 27 for diffent themes -->
        <bean class="org.springframework.web.servlet.theme.ThemeChangeInterceptor">
            <property name="paramName" value="siteTheme" />
        </bean>

    </mvc:interceptors>


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

    <bean id="messageSource"
        class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
        <property name="basename" value="/WEB-INF/studentmessages" />
        <property name="cacheSeconds" value="1" />
    </bean>

    <bean id="localeResolver"
        class="org.springframework.web.servlet.i18n.CookieLocaleResolver">
        <property name="defaultLocale" value="de_DE" />
        <!-- <property name="cookieName" value="idiomaCookie" /> <property name="cookieMaxAge" 
            value="3600" /> -->
    </bean>

    <!--Added tut 27 for diffent themes -->
    <bean id="themeSource"          class="org.springframework.ui.context.support.ResourceBundleThemeSource">
        <property name="basenamePrefix" value="theme" />
    </bean>

    <bean id="themeResolver"
        class="org.springframework.web.servlet.theme.CookieThemeResolver">
        <property name="defaultThemeName" value="green" />
    </bean>


</beans>

theme-green.properties

styleSheet=./stylegreen.css

theme-red.properties

styleSheet=./stylered.css

stylegreen

H3{
    color: green; fornt-family: sams-serif; font-size: 20pt;
}

stylered

H3{
    color: red; fornt-family: arial; font-size: 20pt;
}

的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"
    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">
    <display-name>FirstSpringMVCProject</display-name>

    <servlet>
        <servlet-name>spring-dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

        <!-- value >= 0 means that the servlet is loaded when the web-app is deployed  -->
        <load-on-startup>1</load-on-startup>

    </servlet>

    <servlet-mapping>
        <servlet-name>spring-dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>


</web-app>

enter image description here

enter image description here

2 个答案:

答案 0 :(得分:1)

资源不应该存在于WEB-INF下,因为servlet容器绝不能直接从那里提供任何服务。在将调度程序servlet映射到/而不是/*时,可以直接由tomcat为WEB-INF外部的资源提供服务。

直接在WebContent下将css文件移动一级。

答案 1 :(得分:0)

根据您的堆栈跟踪,没有任何控制器映射来为您的资源提供服务。您需要添加mvc:resources标记才能为静态资源配置处理程序。

作为建议,不要将静态资源保留在WEB-INF文件夹中,而是在WebContent目录下创建一个子文件夹,并将其命名为资源或静态资源,因为稍后您可能想要添加一个全新的servlet处理资源请求。

此外,我还要添加一个拦截器,以防您想要将静态资源保留在浏览器的缓存中。只需将其添加到<mvc:interceptors>部分即可。

                          <!--  Dont Keep It Under WEB-INF -->
<mvc:resources mapping="/static-resources/**" location="/WEB-INF/static-resources/"/>

<mvc:interceptors>
  <mvc:interceptor>
    <mvc:mapping path="/static-resources/**" />
       <bean id="webContentInterceptor" class="org.springframework.web.servlet.mvc.WebContentInterceptor">
<!--                cache will expire the next Year(365 days) -->
                <property name="cacheSeconds" value="31556926" />
                <property name="useExpiresHeader" value="true" />
                <property name="useCacheControlHeader" value="true" />
                <property name="useCacheControlNoStore" value="true" />
        </bean>
     </mvc:interceptor>
</mvc:interceptors>