views:

256

answers:

1

I studying springmvc,When I use @RequestMapping(value="/helloWorld", headers = "content-type=text/*"),I input http://localhost:8080/SpringMVC_10100/helloWorld ,I found console is

WARN org.springframework.web.servlet.PageNotFound - No matching handler method found for servlet request: path '/helloWorld', method 'GET', parameters map[[empty]]

The code is:

@Controller
public class HelloWordController {
private Logger logger = LoggerFactory.getLogger(HelloWordController.class);

@RequestMapping(value="/helloWorld", headers = "content-type=text/*")
public ModelAndView helloWorld() {
    logger.debug("jin ru le");
    logger.info("The helloWorld() method is use");
    ModelAndView view = new ModelAndView();
    view.setViewName("/helloworld");
    return view;
}

}

web.xml is

 <servlet>
<description>This is Spring MVC DispatcherServlet</description>
<servlet-name>SpringMVC DispatchServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
  <description>SpringContext</description>
  <param-name>contextConfigLocation</param-name>
  <param-value>classpath*:springmvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>

 <servlet-mapping>
<servlet-name>SpringMVC DispatchServlet</servlet-name>
<url-pattern>/</url-pattern>

WHy?

+1  A: 

Its most likely the case that /helloworld is not inside the path configured for your dispatcher servlet

e.g. If i have a servlet configured like so:

  <servlet>
    <servlet-name>BMA</servlet-name>
    <servlet-class>
       org.springframework.web.servlet.DispatcherServlet
    </servlet-class>
    <load-on-startup>2</load-on-startup>
  </servlet>

  <servlet-mapping>
    <servlet-name>BMA</servlet-name>
    <url-pattern>/bma/*</url-pattern>
  </servlet-mapping>

And i have a controller configured like so:

@RequestMapping(value = "/planner/plan/{planId}/delete", method = RequestMethod.GET)
public ModelAndView deletePlanConfirm(HttpServletRequest request,  
       @PathVariable("planId")   Long planId)   {}

Then the request in browsder would be:

http://localhost:8080/bma/planner/plan/1223/delete

Edit: Also if you have content-type header narrowing on your handler, make sure that content-type haeder is sent in your request.

naikus
@naikus:I want to try @RequestMapping in headers parameter,but is not success
EdwardLau
@EdwardLau Thats because the Content-Type header is missing in the request, When you do a get from a browser, the browser does not send a content-type header, If done via ajax, you can set the header explicitly. I've edited my answer
naikus