2015-06-10 73 views
2

我想执行一个使用Spring MVC主题功能的Spring MVC应用程序。我下面这个例子https://www.youtube.com/watch?v=OiQql85qsos但不知何故,它不工作,这里是STS春季MVC主题不工作

enter image description here

项目结构

我只是有一个HomeController的

@Controller 
public class HomeController { 

@RequestMapping("/") 
public String getHomePage(){ 
    return "home"; 
} 

} 

这里是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>SpringMVCThemes</display-name> 

<servlet> 
    <servlet-name>config</servlet-name> 
    <servlet- class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
    <load-on-startup>1</load-on-startup> 
</servlet> 
<servlet-mapping> 
    <servlet-name>config</servlet-name> 
    <url-pattern>/</url-pattern> 
</servlet-mapping> 

</web-app> 

以下是config-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.xsd 
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd 
    http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd"> 

<mvc:annotation-driven></mvc:annotation-driven> 
<context:component-scan base-package="co.edureka.controllers"/> 

<mvc:resources mapping="/resources/**" location="/resources/*" /> 
<mvc:resources mapping="/images/**" location="/resources/images/" /> 
<mvc:resources mapping="/css/**" location="/resources/css/" /> 

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

<bean id="themeSource" 
    class="org.springframework.ui.context.support.ResourceBundleThemeSource"> 
     <property name="basenamePrefix" value="META-INF.theme-" /> 
</bean> 

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

<mvc:interceptors> 
<ref bean="themeChangeInterceptor"/> 
</mvc:interceptors> 

</beans> 

这里是theme-black.properties的含量(下的WebContent/META-INF)

css=resources/css/theme-black.css 

下面是主题black.css的含量(下的WebContent /资源/ CSS)

body { 
background-color: #DBF5FF; 
color: #007AAB; 
} 

下面是针对home.jsp但点击其他链接的主题也不会改变主题

home.jsp

我检查了曲奇,有一个叫做org.springframework.web.servlet.theme.CookieThemeResolver.THEME的曲奇

如何让它工作?

+0

您的配置错误...删除'DefaultAnnotationHandlerMapping'并使用''注册拦截器。 –

+0

@ M.Deinum谢谢你现在我可以在浏览器中看到org.springframework.web.servlet.theme.CookieThemeResolver.THEME cookie,但它仍然没有采取主题。 –

+0

我真的不明白你的应用程序结构,你显然是用maven做一些事情,但是这个结构是基于eclipse的?如果您使用的是maven,则属性文件位于错误的位置,如果您未使用maven,它应该位于src/main/resources中,它可以位于任何目录中。你的tsp里有''标签吗? –

回答

1

首先关闭所有的拦截器设置是有缺陷的。您正在使用<mvc:annotation-driven />,因此应使用<mvc:interceptors />来注册您的拦截器。您应该删除DefaultAnnotationHandlerMapping bean。

<mvc:interceptors> 
    <ref bean="themeChangeInterceptor"/> 
</mvc:interceptors> 

下才能够使用选定的主题在你的JSP,你必须使用theme标签从Spring标签库。

<%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%> 

然后在您的标题中包含要使用的样式表。

<head> 
    <link rel="stylesheet" href="<spring:theme code='styleSheet'/>" type="text/css"/> 
</head>