我已经尝试解决这个问题好几天了,我也无法在论坛中找到任何类似问题的帖子。当我在 web.xml 文件中将 servlet 映射 url-pattern 作为 *.html 时,我的 Spring Theme 运行良好。但是,如果我将 web.xml 文件中的 url-pattern 更改为正斜杠,以便默认 servlet 处理所有请求并且 URL 看起来像 REST URL,那么我使用得到 NO MAPPING 错误,这会导致 not能够显示网页的 CSS 设置。我习惯得到的 NO MAPPING 警告如下:
WARN http-apr-8080-exec-6 (DispatcherServlet.java:947) - No mapping found for HTTP request with URI [/MyApp/themes/default.css] in DispatcherServlet with name 'spring'.
但是通过添加到我的 Spring servlet 配置文件来解决映射问题:
<mvc:default-servlet-handler />
出现了另一个问题:现在,在启动应用程序并在任何操作之前,在任何 REST 操作之前单击主题选项的使用效果很好。显示正确的主题一切正常。事实上,如果我通过普通控制器(没有 REST)并进行非 REST 操作,一切都会继续正常工作,我可以在主题之间切换。 但是,通过 REST 并单击主题时,即使它返回与普通控制器返回的视图名称相同的视图名称,我在浏览器中收到以下错误,并且奇怪的是没有报告控制台(调试)消息:
HTTP Status 405 - Request method 'GET' not supported
type Status report
message Request method 'GET' not supported
description The specified HTTP method is not allowed for the requested resource (Request method 'GET' not supported).
Apache Tomcat/7.0.29
这是我更新的 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_2_5.xsd" version="2.5">
<display-name>LiveAppSpring</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/root-context.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<servlet>
<servlet-name>liveAppSpring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>liveAppSpring</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
我更新的Spring servlet 配置是:
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
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">
<!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->
<!-- Enables the Spring MVC @Controller programming model -->
<mvc:annotation-driven />
<mvc:default-servlet-handler />
<mvc:interceptors>
<beans:bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
<beans:property name="paramName" value="lang" />
</beans:bean>
<beans:bean class="org.springframework.web.servlet.theme.ThemeChangeInterceptor">
<beans:property name="paramName" value="theme" />
</beans:bean>
</mvc:interceptors>
<!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
<mvc:resources mapping="/resources/**" location="/resources/" />
<!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->
<!--
<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<beans:property name="prefix" value="/WEB-INF/views/" />
<beans:property name="suffix" value=".jsp" />
</beans:bean>
View Classes:
<beans:property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
<beans:property name="viewClass" value="org.springframework.web.servlet.view.tiles2.TilesView" />
-->
<!--
<beans:bean id="viewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<beans:property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
<beans:property name="prefix" value="/WEB-INF/views/" />
<beans:property name="suffix" value=".jsp" />
</beans:bean>
-->
<!-- This attribute could also be added to viewResolver bean below...
<beans:property name="requestContextAttribute" value="requestContext"/>
-->
<beans:bean id="viewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<beans:property name="viewClass" value="org.springframework.web.servlet.view.tiles2.TilesView" />
</beans:bean>
<beans:bean id="tilesConfigurer" class="org.springframework.web.servlet.view.tiles2.TilesConfigurer">
<beans:property name="definitions">
<beans:list>
<beans:value>/WEB-INF/tiles.xml</beans:value>
</beans:list>
</beans:property>
</beans:bean>
<!-- Application Message Bundle -->
<beans:bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<beans:property name="basename" value="classpath:messages" />
<beans:property name="defaultEncoding" value="UTF-8"/>
</beans:bean>
<beans:bean id="localeResolver" class="org.springframework.web.servlet.i18n.CookieLocaleResolver">
<beans:property name="defaultLocale" value="en"/>
</beans:bean>
<!-- Theme setup -->
<beans:bean id="themeSource" class="org.springframework.ui.context.support.ResourceBundleThemeSource">
<beans:property name="basenamePrefix" value="theme-" />
</beans:bean>
<beans:bean id="themeResolver" class="org.springframework.web.servlet.theme.CookieThemeResolver">
<beans:property name="defaultThemeName" value="default" />
</beans:bean>
<!--
The 2 bean declarations below are NOT compatible with 3.0.5 as it conflicts with the mvc tag library (as defined in header).
Work around is to have the mvc:interceptors tag declared as done near the top of this configuration file.
-->
<!--
<beans:bean id="localeChangeInterceptor" class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
<beans:property name="paramName" value="lang" />
</beans:bean>
-->
<!--
<beans:bean id="handlerMapping" class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
<beans:property name="interceptors">
<beans:ref bean="localeChangeInterceptor" />
</beans:property>
</beans:bean>
-->
<context:component-scan base-package="com.liveapp.spring" />
</beans:beans>
最后,我的主题属性文件位于
LiveAppSpring\src\main\resources
具有以下模式的所有主题属性文件的内容:
css=themes/default.css
为了安全起见,我在所有 3 个目录中都复制了我的 css 文件:
LiveAppSpring\src\main\webapp\resources
LiveAppSpring\src\main\webapp\resources\themes
LiveAppSpring\src\main\webapp\themes
任何方向将不胜感激。