Announcement Announcement Module
Collapse
No announcement yet.
Redirect after post (modified approach?) Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Redirect after post (modified approach?)

    Hi,
    I'm quite new to the springframwork. I read the Redirect-after port thread a while ago while I was trying to write a simple spring-web based application
    (http://forum.springframework.org/sho...ight=redirect).

    I think I understood the idea, but i was struggling with the configuration setup
    (<bean name="newPersonSuccess" class="example.RedirectAfterPostView"> ...) in the spring-servlet.xml file.

    What about the following idea:
    Instead of using a view to do store and to retreive the model in/from the session use two controllers (RedirectAfterPostController and RedirectController).

    The RedirectAfterPostController stores the model/view in the session (when a post request is detected) and redirects the processing to the RedirectController (always using the same url). It extends AbstractFormController and basically provides a functionality similar to SimpleFormController. All user supplied form controllers that use the redirect after post pattern are derived from this controller.

    The RedirectController simply extracts model and view from the session and returns it. The Controller extends the AbstractController.

    The mechanism is similar to the original approach, but the configuration is (imo) simpler. A controller using the redirect after post pattern only needs to extend the RedirectAfterPostController, no additional configuration is needed.

    Are there any disadvantages compared to the original solution?

    best regards
    matto


    Example:

    HTML Code:
    spring-servlet.xml:
    
      <bean id="redirectController" 
        	class="xxx.RedirectController">
        	<property name="modelName"><value>TheModel</value></property>
      </bean> 
      <bean id="redirectAfterPostController" 
        	class="xxx.RedirectAfterPostController"
        	abstract="true">
        	<property name="modelName"><value>TheModel</value></property>
        	<property name="redirectUrl"><value>/redirect.do</value></property>
      </bean>
    
      <!-- application spepcific controller -->
      <bean id="appFormController" 
        	class="yyy.AppFormController"
       	parent="redirectAfterPostController">
    	<property name="formView" value="..."/>
            <property name="commandName"><value>...</value></property>
            <property name="commandClass"><value>d...</value></property>
          	<property name="bindOnNewForm"><value>true</value></property> 
            <property name="successView"><value>...</value></property>	
      </bean>
    
    RedirectController.java:
    public class ZetRedirectController extends AbstractController {
    
    	private String modelName;
    	protected ModelAndView handleRequestInternal(HttpServletRequest request,
    			HttpServletResponse arg1) throws Exception {
    		HttpSession session = request.getSession();
    		Object o = session.getAttribute(getModelName());
    		session.removeAttribute(getModelName());
    		ModelAndView mv = (ModelAndView)o;
    		return mv;
    	}
    
    	...
    }
    
    RedirectAfterPostController.java:
    public abstract class RedirectAfterPostController extends AbstractFormController {
    
    	protected abstract ModelAndView onSubmit(Object cmd) throws Exception;
    
    	protected ModelAndView processFormSubmission(HttpServletRequest request,
    			HttpServletResponse response, Object cmd, BindException errors)
    			throws Exception {
    		
    		// simply taken from the simple form controller
    		ModelAndView retval = null;
    		
    		if (errors.hasErrors()) {
    			// ... (redisplay the form as implemented in the simpleFormController)
    		}
    		else {
    			ModelAndView mv =  onSubmit(cmd);
    			HttpSession session = request.getSession();
    			session.setAttribute(modelName, mv); 
    			retval = new ModelAndView();
    			retval.setView(new RedirectView(this.getRedirectUrl(), true));
    		}
    		return retval;
    	}
    	
    	...
    }
    
    AppFormController:
    public class AppFormController extends RedirectAfterPostController {
    	
    	public ModelAndView onSubmit(Object command)
                throws Exception {
            	ModelAndView mv =  new ModelAndView(getSuccessView(), "xxx", new HashMap());
            	return mv;
    	}
    }
Working...
X