Trouble getting mapping to work
I'm using Spring 3.0.5. None of my annotated controllers are getting recognized. I have XML for my application …
<?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:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframewor开发者_JAVA百科k.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<mvc:resources mapping="/static/**" location="/static/"/>
<mvc:annotation-driven/>
<context:component-scan base-package="com.myco.systems.leadsmonitor"/>
My static assets are getting picked up fine. But I'm getting a 404 when I try and reach an annotated controller …
package com.myco.systems.leadsmonitor.web.controller;
@Controller
public class HomeController {
…
@RequestMapping("/")
public void index(Model model) {
model.addAttribute("latestLeadTime", MonitorStatistics.getInstance().getLatestLeadCreationTime());
model.addAttribute("lastDBCheck", MonitorStatistics.getInstance().getLastDBCheck());
}
What else do I need to do to get my controllers picked up by Spring? Thanks, - Dave
I think the reason you are getting a 404 is because the view is not being resolved in the contoller method. By default a void return type is mapped using to a view name based on the URI, in your case since the path is "/", it probably gets resolved to "", which does not get mapped to any logical view.
Can you try a couple of things:
- Start in a debug mode and see if your request is actually coming to the RequestMapping method
- If it is, can you explicitly return a view name from your method and see if this view name is getting resolved to a correct view(assuming that you have a view resolver registered)
You should check both URL mappings in web.xml and view resolution configuration in your spring configuration.
One good resource is Spring By Example.org
see: http://www.springbyexample.org/examples/simple-spring-mvc-form-annotation-config-webapp.html
Another one are the tutorials available in SpringSource ToolSuite (STS), where they walk you through creating a Spring MVC web application. Those tutorials usually include the UrlRewriteFilter from tuckey.org
e.g. servlet maps to /app, then add this servlet filter
<!-- Enables clean URLs with JSP views e.g. /welcome instead of /app/welcome -->
<filter>
<filter-name>UrlRewriteFilter</filter-name>
<filter-class>org.tuckey.web.filters.urlrewrite.UrlRewriteFilter</filter-class>
<!-- <init-param>-->
<!-- <param-name>logLevel</param-name>-->
<!-- <param-value>DEBUG</param-value>-->
<!-- </init-param>-->
</filter>
精彩评论