Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring cannot resolve view within servlet

Tags:

java

spring

I've added a new method/mapping to one of my servlets:

@RequestMapping(value = "/user/prefs/order", method = RequestMethod.POST)
public void updateUsersPrefs(@RequestBody Map<String, ArrayList> body, HttpServletRequest request) {
    ...
}

but when I send a request to this url I get a 500 Internal Server Error, with the following error message:

javax.servlet.ServletException: Could not resolve view with name 'user/prefs/order' in servlet with name 'appfinder'
    org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1029)
    org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:817)
    org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:719)
    org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:644)
    org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:560)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:710)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:803)

I cannot for the life of me see why this is being reported. Can anyone help with this? I there is anymore information that I could provide, please let me know.

Thanks!

like image 222
MeanwhileInHell Avatar asked May 17 '11 15:05

MeanwhileInHell


2 Answers

Spring treats @RequestMapping methods with void return type in the following manner:

void - if the method handles the response itself (by writing the response content directly, declaring an argument of type ServletResponse / HttpServletResponse for that purpose) or if the view name is supposed to be implicitly determined through a RequestToViewNameTranslator (not declaring a response argument in the handler method signature).

Therefore since there is no HttpServletResponse parameter to this method, Spring assumes that you would like the view name to be determined through a RequestToViewNameTranslator.

If you do not specify a particular RequestToViewNameTranslator to use in your context, then the default implementation kicks in which will:

simply transforms the URI of the incoming request into a view name.

If you don't want the URI of the incoming request to be used as the view name, you have a few options:

  1. Configure a custom RequestToViewNameTranslator with the behavior you would like
  2. Add a HttpServletResponse parameter to this method if you would like to write to the response directly rather than View resolution take place.
  3. Change the return type of this method to String, View, orModelAndView` to be able to specify the view or view name within the method.
like image 195
matt b Avatar answered Sep 16 '22 16:09

matt b


I had this problem, and the reason was I was using tiles framework and did not mention the view name in tiles-def.xml. After configuring tiles-def.xml had the problem solved.

like image 35
user2712789 Avatar answered Sep 20 '22 16:09

user2712789