【问题标题】:No mapping found for HTTP request with URI [/FirstSpringMVCProject/stylegreen.css] in DispatcherServlet with name 'spring-dispatcher'在名称为“spring-dispatcher”的 DispatcherServlet 中找不到具有 URI [/FirstSpringMVCProject/stylegreen.css] 的 HTTP 请求的映射
【发布时间】:2016-10-24 18:14:59
【问题描述】:

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

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>

spring-dispatcher-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;
}

风格化

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>

【问题讨论】:

  • 大概你的问题是“为什么我在 /FirstSpringMVCProject/stylegreen.css 上得到 404?”为什么你认为你不应该收到 404?您认为 Spring MVC 应用程序的哪一部分服务于该路径?
  • 请发布您的 web.xml,我觉得您缺少一些配置。
  • @Azim:我已经添加了。
  • @SotiriosDelimanolis:我没有收到 404 错误我添加了另一个屏幕截图
  • 你需要 smth 来服务你的资源(css、js、图像)。检查这个link和这个answer

标签: java spring jsp spring-mvc


【解决方案1】:

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

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

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

                          <!--  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>

【讨论】:

    【解决方案2】:

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

    只需将 css 文件直接移动到 WebContent 下一级即可。

    【讨论】:

      猜你喜欢
      • 2017-09-04
      • 1970-01-01
      • 2015-08-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-06-20
      相关资源
      最近更新 更多