1

I have some controllers which have nothing in them, i am just setting the viewname to model object and returning it . In the absence of these methods, does SpringMVC do the mapping directly through the tile definition? If yes, can you please tell how to do that.

Thanks in Advance..

user1169474
  • 191
  • 1
  • 1
  • 6

1 Answers1

0

You can use a combination of org.springframework.web.servlet.handler.SimpleUrlHandlerMapping & org.springframework.web.servlet.mvc.UrlFilenameViewController:

  • SimpleUrlHandlerMapping gives the ability to route an URL directly to a controller,
  • UrlFilenameViewController transform the received URL directy into a view name (e.g. URL: /test.htmlView name: test).

Hereunder, the required Spring configuration:

    <?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:util="http://www.springframework.org/schema/util"
        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.2.xsd
            http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd
            http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
            http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.2.xsd">

        <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <property name="viewClass" value="org.springframework.web.servlet.view.tiles3.TilesView" />
            <property name="prefix" value="/WEB-INF/tiles/" />
            <property name="suffix" value=".tiles" />
            <property name="order" value="1" />
        </bean>

        <!-- For direct mapping between URL (i.e. index.html ←→ index) and the JSP 
            to render -->
        <bean id="urlFilenameViewController" class="org.springframework.web.servlet.mvc.UrlFilenameViewController" />

        <bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
            <property name="order" value="1" />
            <property name="mappings">
                <util:map>
                    <entry key="/test.html" value-ref="urlFilenameViewController" />
                </util:map>
            </property>
        </bean>
    </beans>

Be careful of the <mcv:default-servlet-handler /> tag in your Spring configuration, it can make the above configuration failing (not returning null and default handler priority set to Interger.MAX_VALUE as explained in the Spring documentation).

Doc Davluz
  • 4,154
  • 5
  • 30
  • 32