Announcement Announcement Module
Collapse
No announcement yet.
@RequestMapping, return from action does not map to jsp Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • @RequestMapping, return from action does not map to jsp

    Hi,

    I am building a small app following samples found in the spring distribution.
    I have this in my DispatcherServlet config file:
    Code:
    <context:annotation-config/>
    <context:component-scan base-package="my.packages.controllers"/>
    <bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"/>
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"	p:prefix="/WEB-INF/jsp/" p:suffix=".jsp"/>
    I chose to have all jsp files related to a same model in a dedicated directory:
    for example, I have a WEB-INF/jsp/country/ directory containing a list.jsp, show.jsp, newForm.jsp, ...

    I have a controller class that begins like this:

    Code:
    &#64;Controller
    &#64;RequestMapping("country/*")
    &#64;SessionAttributes("country")
    public class CountryController {
    Everything works fine except when I explicitly return from a controller method.
    Here is an excerpt of my processSubmit()

    Code:
    &#64;RequestMapping(method=RequestMethod.POST)
    public String processSubmit(&#64;ModelAttribute("country") Country country, BindingResult result, SessionStatus status) {
        if (result.hasErrors()) {
            return "country/newForm";
        } else {
            // saving data
            status.setComplete();
            return "redirect:list.html";
        }
    }
    Why do I have to put the directory ("country/") that contains the view in the if clause?
    When I do not return explicitly a view name, the views are correctly loaded from the "country/" directory.

    Why do I have to NOT put the directory ("country/") but put the ".html" extention (defined in my web.xml) in the else clause?
    Because it is a redirect? If this is a leaky abstraction then I'm not sure we can still speak about abstraction.

  • #2
    The redirect is a client side redirect which, if not prefixed with a /, is being treated as being inside the current path. In the other case it is a forward to a certain view and the whole view name is passed to the ViewResolver. Now what you need to return depends on your ViewResolver. In your case it is a InternalResourceViewResolver which only adds something in front and in the back. As you have decided to put it in sub directories you need to append the directory.

    You can switch to a ResourceBundleViewResolver (or xml based one), or use a BeanNameViewResolver or use the default RequestToViewNameTranslator to resolve your JSP (that last one uses a convention).

    If this is a leaky abstraction then I'm not sure we can still speak about abstraction.
    Before starting complaining maybe first understand what is happening inside the framework and how all the components work together.

    Comment


    • #3
      Thanks for your reply.
      And sorry for the satirical part. But you are right: I like complaining and I do not know about all that is happening inside the framework.
      The thing is I feel that I need to know too much about it just to be able to do so little.

      Comment


      • #4
        The thing is I feel that I need to know too much about it just to be able to do so little.
        Hmm try writing your own framework.

        Spring is flexible and with flexibility comes that you need to know what class does what.

        Basically it are a few classes (actually interfaces that do the work). The javadocs of the DispatcherServlet is quite clear about the process. That gives you also an overview of what you can change.

        You just need to understand the process/flow of things (not the internals) and just select the right class (or implement your own based on the interfaces).

        Comment

        Working...
        X