SpringMVC: Multi viewresolver

I created InternalResourceViewResolver for mapping URL in .java files. Now, I also want to give style my html pages with .css files. But, when The request comes with .css URL, control goes to .java files to search mapping for the given URL. So, Is it possible to have another ViewResolver particularly for all css/js files?

Here is my code for sdnext-servlet.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://www.springframework.org/schema/beans" 
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:tx="http://www.springframework.org/schema/tx"
    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/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">

    <context:property-placeholder location="classpath:resources/database.properties" />
    <context:component-scan base-package="com.visionet" />

    <tx:annotation-driven transaction-manager="hibernateTransactionManager"/>

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

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

    <bean id="dataSource"
        class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="${database.driver}" />
        <property name="url" value="${database.url}" />
        <property name="username" value="${database.user}" />
        <property name="password" value="${database.password}" />
    </bean>

    <bean id="sessionFactory"
        class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="annotatedClasses">
            <list>
                <value>com.visionet.model.School</value>
                <value>com.visionet.model.Country</value>
            </list>
        </property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">${hibernate.dialect}</prop>
                <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
                <prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>             
            </props>
        </property>
    </bean>

    <bean id="hibernateTransactionManager"
        class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory" />
    </bean>
</beans>

here comes my web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

 <servlet>
  <servlet-name>sdnext</servlet-name>
  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/config/sdnext-servlet.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
 </servlet>

 <servlet-mapping>
  <servlet-name>sdnext</servlet-name>
  <url-pattern>*.html</url-pattern>
 </servlet-mapping>

 <welcome-file-list>
  <welcome-file>index.html</welcome-file>
 </welcome-file-list>

</web-app>

Answers


You can define a resources root which does not use the resolver. In the same spot that you defined the jspViewResolver bean, add the following.

<resources mapping="/resources/**" location="/resources/" />

Create a resources directory at the same level as your WEB-INF directory, like this.

resources
     - css
     - images
     - 
WEB-INF
     - ...

You would reference your css like this, anything under /resources does not use the view resolver.

<link rel="stylesheet" href="/resources/css/styles.css" />

This is normally done in the configuration for the dispatcher servlet. Within this file you can place a <resources/> tag that denotes the directly containing the client side resources. This directory will then be excluded from being handled by the dispatcher servlet, allowing static resources such as CSS and JS to be served without interference by the dispatcher.

Here is an example of the tag in use:

<!-- Handles HTTP GET requests for /resources/ by efficiently serving
up static resources in the ${webappRoot}/resources directory -->
<mvc:resources location="/resources/" mapping="/resources/**" />

This XML snippet would be placed in your /WEB-INF/config/sdnext-servlet.xml file. It would map every request starting with /resources/ to the /resources/ directory located within the root of your web application. The documentation also provides a nice write up for how the resources tag works.


Need Your Help

Selector based html templating library for Java similar to Enlive

java html templates clojure enlive

Is anyone aware of a template library for java similar in nature to Enlive?

hibernate.validator.fail_fast Sequence

hibernate spring-mvc

My problem is that i am getting Validation error one by one, But not in sequence. So can any buddy tell me how to display error, in Sequence according to user ...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.