0

I am trying to provide some homepage with theme multiple support by changing the content of the h3 tag in the AdmissionForm.jsp file but I am facing the problem after clicking the Red or Green link that I am getting the following warning:

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'

some code from the AdmissionForm.jsp file

<%@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;
}

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

TheBook
  • 1,698
  • 1
  • 16
  • 32
  • Presumably your question is "Why do I get a 404 on /FirstSpringMVCProject/stylegreen.css?" Why do you think you shouldn't receive a 404? What part of your Spring MVC application do you think serves that path? – Sotirios Delimanolis Jun 22 '16 at 17:03
  • Please post your web.xml, I feel you are missing some configurations there. – Azim Jun 22 '16 at 17:12
  • @Azim: I have added it. – TheBook Jun 22 '16 at 17:26
  • @SotiriosDelimanolis: I am not getting 404 error I added another screen shot – TheBook Jun 22 '16 at 17:27
  • you ll need smth to serve your resources(css,js,images). check this [link](http://www.mkyong.com/spring-mvc/spring-mvc-how-to-include-js-or-css-files-in-a-jsp-page/) and this [answer](http://stackoverflow.com/questions/26276603/how-to-include-js-and-css-in-jsp-with-spring-mvc) – AntJavaDev Jun 22 '16 at 17:41
  • You have to specify the contextClass, please add the snippet into your web.xml contextClass org.springframework.web.context.support.AnnotationConfigWebApplicationContext – Azim Jun 22 '16 at 17:46
  • @AntJavaDev: Your comment worked for me. Please add it as answer and I will accept it. – TheBook Jun 22 '16 at 18:02

2 Answers2

1

Resources should not live under WEB-INF, because a servlet container must never directly serve anything from there. As you mapped the dispatcher servlet to / and not /*, resources outside WEB-INF can directly be served by tomcat.

Just move css file one level up directly under WebContent.

Serge Ballesta
  • 143,923
  • 11
  • 122
  • 252
0

Based on your stacktrace, there is not any controller mapped to serve your resources. you ll need to add the mvc:resources tag in order to configure a handler for static resources.

As a suggestion , dont keep the static resources in the WEB-INF folder , but create a subfolder under the WebContent directory and name it smth like resources or static-resources , because later on you might want to add a whole new servlet to process the resources requests.

Also i am adding an interceptor , in case you want to keep the static resources in browser's cache. Simply add it in your <mvc:interceptors> section.

                          <!--  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>
AntJavaDev
  • 1,204
  • 1
  • 18
  • 24