Announcement Announcement Module
Collapse
No announcement yet.
Redirects Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Redirects

    Is there a builtin mechanism to redirect in Spring?

    At the moment I have the following:

    Code:
    <bean name="/shop/addItemToCart.do" class="presentation.web.AddItemToCartController">
      <property name="webStore"><ref bean="webStore"/></property>
      <property name="redirectURL"><value>./viewCart.do</value></property>
    </bean>
    	
    <bean name="/shop/removeItemFromCart.do" class="presentation.web.RemoveItemFromCartController">
      <property name="redirectURL"><value>./viewCart.do</value></property>
    </bean>
    	
    <bean name="/shop/updateCartQuantities.do" class="presentation.web.UpdateCartQuantitiesController">
      <property name="redirectURL"><value>./viewCart.do</value></property>
    </bean>
    
    <bean name="/shop/viewCart.do" class="presentation.web.ViewCartController"/>
    Code:
    package presentation.web;
    
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    import org.springframework.web.servlet.ModelAndView;
    import org.springframework.web.servlet.mvc.Controller;
    import org.springframework.web.util.WebUtils;
    
    import domain.WebStoreFacade;
    import domain.PrimaryKey;
    import domain.Cart;
    import domain.Item;
    
    /**
     * @author <a href="mailto&#58;[email protected]">Cameron Zemek</a>
     */
    public class AddItemToCartController implements Controller &#123;
        private WebStoreFacade webstore;
        
        public void setWebStore&#40;WebStoreFacade webstore&#41; &#123;
            this.webstore = webstore;
        &#125;
        
        private String redirectURL;
        
        public void setRedirectURL&#40;String url&#41; &#123;
            this.redirectURL = url;
        &#125;    
        
        public ModelAndView handleRequest&#40;
                HttpServletRequest request,
                HttpServletResponse response&#41; throws Exception &#123;
            Cart cart = &#40;Cart&#41; WebUtils.getOrCreateSessionAttribute&#40;
                    request.getSession&#40;&#41;, "sessionCart", Cart.class&#41;;
            
            // Get item
            int itemId = Integer.parseInt&#40;request.getParameter&#40;"itemId"&#41;&#41;;
            Item item = webstore.getItem&#40;new PrimaryKey&#40;itemId&#41;&#41;;
            
            // Add to cart
            cart.addItem&#40;item&#41;;
            
            response.sendRedirect&#40;redirectURL&#41;;
            return null;
        &#125;
    &#125;

  • #2
    There is spring RedirectView. But I had some problems using it, dont't remember exactly. So i returned null and redirected by myself.

    Comment


    • #3
      The RedirectView view is the view you need. You can specify whether or not the view should redirect relative to the context or not and it's a view object, just as any other view in the Spring MVC framework. I always recommend to stay away from returning null from your controller as much as possible but instead return ModelAndView objects. Using hte view resolver infrastructure you can easily change the view to a RedirectView.

      Have a look at the ResourceBundleViewResolver, it holds bean definitions (views) in a properties file.

      Code:
      return new ModelAndView&#40;"redirectView"&#41;;
      Code:
      redirectView.class=org.springframework.web.servlet.view.RedirectView
      redirectView.contextRelative=true # defaults to false

      Comment


      • #4
        Could you provide an example, especially on how I should change the code that I posted?

        Comment


        • #5
          Java might be:
          Code:
             return new RedirectView&#40;redirectURL, true&#41;;
          Instread of:
          Code:
              response.sendRedirect&#40;redirectURL&#41;;
              return null;

          Comment


          • #6
            The simplest approach would be to change your code to use an instance of org.springframework.web.servlet.view.RedirectView. The following code should clear things up.

            Code:
            public ModelAndView handleRequest&#40;
                        HttpServletRequest request,
                        HttpServletResponse response&#41; throws Exception &#123;
                    Cart cart = &#40;Cart&#41; WebUtils.getOrCreateSessionAttribute&#40;
                            request.getSession&#40;&#41;, "sessionCart", Cart.class&#41;;
                   
                    // Get item
                    int itemId = Integer.parseInt&#40;request.getParameter&#40;"itemId"&#41;&#41;;
                    Item item = webstore.getItem&#40;new PrimaryKey&#40;itemId&#41;&#41;;
                   
                    // Add to cart
                    cart.addItem&#40;item&#41;;
                   
                    return new ModelAndView&#40;new RedirectView&#40;redirectURL&#41;&#41;;
                    //response.sendRedirect&#40;redirectURL&#41;;
                    //return null;
                &#125;
            I would still like some easy way of declaring the redirect from in the XML. Something as easy as the struts forward redirect attribute would be excellent...

            Code:
            <forward name="foo" path="/bah/thankyou.do" redirect="true"/>

            Comment


            • #7
              Keep in mind though, that those redirects will do exactly that. So they will return a http code to the browser enforcing you to redirect.

              When I needed that feature it didnt keep the already saved return messages to the redirect and so I decided that this wasnt good enough. Therefor I started my own spring based web workflow project. Its available at http://www.competities.com/springworkflow. With this system you can chain multiple controllers so you can redirect to another page (and then yet to another page) without doing a browser redirect (and thus without loosing state). Also the system has all the possible actions in one xml file and there is a special tag available to put the actions on the page. This tag will know on which page the user is and look up in the xml if it should make the action available (either as a link or a submit button). This way you can easily change the navigation of your site without having to change all of your jsp's.

              Comment


              • #8
                An other alternative is to extend the AbstractWizardFormController provided by the spring framework. It enables you to set up a list of pages in the xml configuration file and then it provides a list of tools, like error validation, navigation logic etc. By default a bean is passed to each page, but you can also pass other objects through the referenceData method depending on the next page that will be shown to the user. You can take a look at the PetStore example.

                Comment


                • #9
                  Hello,

                  Where is the PetStore example website? I came across it some time ago and
                  now been searching all over the internet. Googling but can't seem to find the real one.

                  Please, paste the website here.

                  Thanking you.

                  eve

                  Comment


                  • #10
                    Originally posted by evepokua View Post
                    Hello,

                    Where is the PetStore example website? I came across it some time ago and
                    now been searching all over the internet. Googling but can't seem to find the real one.

                    Please, paste the website here.

                    Thanking you.

                    eve
                    If you mean the jPetStore, you shall find it under the samples directory of the 2.5 distribution.

                    Comment


                    • #11
                      What web layer are you using?

                      Comment


                      • #12
                        Hi,

                        I have found the JPetStore. But the wedsite I am still looking for.
                        It shows step by step how to set it up. I am not sure if it's the same as
                        the Jpetstore with spring.

                        Thanking you.

                        eve

                        Comment


                        • #13
                          Originally posted by SIqbal View Post
                          What web layer are you using?
                          Web layer.

                          I m using glassfish v2 application server,
                          Spring webflow 2.0.3
                          string 2.0.6
                          EJB3.

                          I have a persistence.xml.

                          I am able to retrieve and input data into my database without spring.

                          I have got spring and webflow working. What I want to do is similar
                          to the petstore. But the petStore is a little too much for me.

                          But I would like to do is use the database with spring.

                          eve

                          Comment


                          • #14
                            string 2.0.6? or Struts

                            OK search for startsoft on google code

                            Comment

                            Working...
                            X