Announcement Announcement Module
No announcement yet.
Forwarding to another controller? Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Forwarding to another controller?

    Im struggling to understand the exact use of Controllers for forwarding in Spring. I have worked through the simple tutorial ok but it doesnt cover the next stage, we cant use Webflow because we need to use an Offcial release.

    If I give an example could somebody please explain the correct controller to use, particular in reference to forwarding to another controller rathe than returning a view and model.

    I start off witha pseudo login.jsp
    This then calls the Spring Dispatcher
    This dispatches it to a Controller (Im not sure what type)
    This should then validate the login
    if the login is ok I want then forward to another controller which retrieve some data and then will return a model and a view (the data and a jsp to render it)
    If the login fails redisplay the login.jsp.

  • #2

    in your case, you do not need to forward to another Controller. Making this could be done by something like:

    return new ModelAndView(new RedirectView(...))
    However, simple validation can be done by creating an additional class that inherits Validator. By setting this validator for your Controller in the servlet definition, all you need is done automatically.

    <bean id="myValidator" class="" />
    	<bean name="/" class="my.package.myAction">
    		<property name="formView"><value>login</value></property>
    		<property name="commandName"><value>whatEverYouWant</value></property>
    		<property name="validator"><ref bean="myValidator" /></property>
    So, when validation fails, it automatically returns to the formView; otherwise, it continues normally.

    Hope this helps.

    Kind regards,


    • #3
      Originally posted by simtin

      in your case, you do not need to forward to another Controller. Making this could be done by something like:

      return new ModelAndView&#40;new RedirectView&#40;...&#41;&#41;
      Would this not then go to a jsp view for rendering. At this point I am not ready to display anything, I need to call a controller to retrieve the model. I needs to be another controller because it is dealing with stuff that is nothing to do with login and may be used in othe rplaces.

      Maybe RedirectDirect view can be used to go straight to this second controller, I dont know, but the name suggests it goes back to the client it would be more efficient to just foward onto another controller which would then return the view.

      Sorry,if ive completely misunderstood you.


      • #4
        I think you want InternalResourceView.

        You can use "redirect:" and "forward:" as prefixed when returning the view name, or use RedirectView or InternalResourceView respectively to redirect or forward.

        I tend to use "redirect:" more than "forward:" so that if the user hits refresh, the browser then doesn't do another post.