Page 1 of 2 12 LastLast
Results 1 to 10 of 14

Thread: Redirects

  1. #1
    Join Date
    Sep 2004
    Posts
    9

    Default 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;grom@capsicumcorp.com">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. #2
    Join Date
    Aug 2004
    Posts
    14

    Default

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

  3. #3
    Join Date
    Aug 2004
    Location
    Amsterdam, Netherlands
    Posts
    450

    Default

    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

  4. #4
    Join Date
    Sep 2004
    Posts
    9

    Default

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

  5. #5
    Join Date
    Aug 2004
    Location
    Auburn, AL, USA.
    Posts
    106

    Default

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

  6. #6
    Join Date
    Sep 2004
    Location
    Melbourne, Australia
    Posts
    54

    Default

    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"/>

  7. #7
    Join Date
    Aug 2004
    Posts
    29

    Default

    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.

  8. #8
    Join Date
    Aug 2004
    Location
    Paris
    Posts
    43

    Default

    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.

  9. #9
    Join Date
    Nov 2009
    Posts
    14

    Question

    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

  10. #10

    Default

    Quote 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.
    [URL="http://vicina.info"] 新闻,社区新闻,分类广告

Similar Threads

  1. Redirects and Spring Web Flow
    By Scott Battaglia in forum Web Flow
    Replies: 11
    Last Post: Oct 27th, 2005, 07:56 PM
  2. after login redirects incorrectly
    By ryan.tyer in forum Security
    Replies: 1
    Last Post: Oct 10th, 2005, 05:16 PM
  3. Redirects In Java base flow
    By curtney in forum Web Flow
    Replies: 6
    Last Post: Aug 28th, 2005, 02:39 AM
  4. Replies: 2
    Last Post: Jul 5th, 2005, 07:29 AM
  5. Replies: 0
    Last Post: Jan 25th, 2005, 01:14 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •