mvc:view-controller causes PageNotFound in Spring Tiles2
I have a webapp based on Spring 3.0.6 which works fine on Tomcat 7.0.
The web.xml
defines the dispatcher as following:
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
The dispatcher defines the view resolver in the usual way:
<bean id="tilesViewResolver"
class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.tiles2.TilesView" />
</bean>
<bean id="tilesConfigurer"
class="org.springframework.web.servlet.view.tiles2.TilesConfigurer">
<property name="definitions">
开发者_开发技巧 <list>
<value>/WEB-INF/tiles-def.xml</value>
</list>
</property>
</bean>
I have a controller annotated with @RequestMapping("/home")
and a "home"
view defined in tiles-def.xml
. When I point my browser to the /myapp/home.html
, the Tiles page is opened.
If I add <mvc:resources mapping="/resources/**" location="/resources/" />
or <mvc:view-controller path="/" view-name="home.html"/>
to my dispatcher xml file, pointing the browser to /myapp/home.html
results in a 404. The log says:
21:34:22,128 WARN PageNotFound:947 – No mapping found for HTTP request with URI [/myapp/home.html] in DispatcherServlet with name 'dispatcher'
What am I doing wrong?
Thanks a lot
The problem in my application was due to the automatic view name resolution. My annotated method in my @Controller
returned void
, and the framework tried to guess the tiles view name using the request path.
I modified my annotated method as following, returning a String
:
@RequestMapping(value="/page", method = RequestMethod.GET)
public String showForm(HttpServletRequest request, Model model) {
// TO BUSINESS LOGIC
// return tiles view name as configured in 'tiles-def.xml'
return "my_tiles_view_name";
}
With this change, everything works fine.
精彩评论