Spring MVC Themes Not working -
i trying execute spring mvc application uses spring mvc theme feature. following example https://www.youtube.com/watch?v=oiqql85qsos somehow not working, here project structure in sts
i have homecontroller
@controller public class homecontroller { @requestmapping("/") public string gethomepage(){ return "home"; } }
here 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>
below 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>
here content of theme-black.properties (under webcontent/meta-inf)
css=resources/css/theme-black.css
below content of theme-black.css (under webcontent/resources/css)
body { background-color: #dbf5ff; color: #007aab; }
here home.jsp on clicking on other themes link not change themes
i checked cookies , there cookie name org.springframework.web.servlet.theme.cookiethemeresolver.theme
how make work ?
first off interceptor setup flawed. using <mvc:annotation-driven />
, such should use <mvc:interceptors />
register interceptors. should remove defaultannotationhandlermapping
bean.
<mvc:interceptors> <ref bean="themechangeinterceptor"/> </mvc:interceptors>
next able use selected theme in jsp have use theme
tag spring tag library.
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>
then in header include stylesheet want use.
<head> <link rel="stylesheet" href="<spring:theme code='stylesheet'/>" type="text/css"/> </head>
Comments
Post a Comment