0

I am trying use a PRG (Post-Redirect-Get) pattern in one of my Spring MVC controller. The controller collects user data from an HTML form, does some processing, stores data in DB and then shows a JSP page. After saving data a redirect should happen and then the JSP page should be displayed.

I also tried to prepend "redirect:" in front of the VIEW_NAME but I get 404 then.

Please guide.

CartPageController.java

@Controller
@RequestMapping("/cartPageController.do")
public class CartPageController {

    private static final Logger LOG = Logger.getLogger(CartPageController.class);
    private static final String VIEW_NAME = "cart";

    @Autowired
    protected MasterDao masterDao;

    @RequestMapping(method = {RequestMethod.GET, RequestMethod.POST})
    public ModelAndView processRequest(HttpServletRequest request, HttpServletResponse response) {
        LOG.debug("Into the CartPageController...");

        HttpSession session = request.getSession();
        ModelAndView mav = new ModelAndView();

        //create Cart object and store it in session
        Cart cart = null;
        if (session.getAttribute("cart") != null) {
            cart = (Cart) session.getAttribute("cart");
        } else {
            cart = createCart();
            session.setAttribute("cart", cart);
        }
        LOG.debug("cart = " + cart);

        //determine the cart operation
        String btnAddToCart = GenericUtils.nullToEmptyString(request.getParameter("btnAddToCart"));
        String removeProduct = GenericUtils.nullToEmptyString(request.getParameter("removeProduct"));
        String updateProduct = GenericUtils.nullToEmptyString(request.getParameter("updateProduct"));
        LOG.debug("btnAddToCart = " + btnAddToCart);
        LOG.debug("removeProduct = " + removeProduct);
        LOG.debug("updateProduct = " + updateProduct);
        if (btnAddToCart.length() > 0) {
            addToCart(request, cart);
        } else if (removeProduct.length() > 0) {
            removeProduct(request, cart);
        } else if (updateProduct.length() > 0) {
            updateCart(request, cart);
        }

        //TODO: Should use PRG pattern here
        //TRIED TO APPEND "redirect:" here but does not work, gives me 404 
        mav.setViewName(VIEW_NAME);
        return mav;
    }

    //some more code below here
}
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Nital
  • 5,784
  • 26
  • 103
  • 195
  • possible duplicate of [Redirect to dynamic URL in Spring MVC](http://stackoverflow.com/questions/9311940/redirect-to-dynamic-url-in-spring-mvc) – Mudassar Aug 05 '15 at 17:06

4 Answers4

0

you can try

return new ModelAndView(new RedirectView("/page"));

BoraKoc
  • 1
  • 3
0

You can't just prepend "redirect:" to your view name which results in redirect:cart and then obviously 404, what you have to do for redirect is to specify the redirect path for e.g.:

redirect:/cart.htm

This should explain it more in details.

Paulius Matulionis
  • 23,085
  • 22
  • 103
  • 143
0

You should redirect to url not a view name. as Spring doc says:

While the use of RedirectView works fine, if the controller itself creates the RedirectView, there is no avoiding the fact that the controller is aware that a redirection is happening. This is really suboptimal and couples things too tightly. The controller should not really care about how the response gets handled. In general it should operate only in terms of view names that have been injected into it.

The special redirect: prefix allows you to accomplish this. If a view name is returned that has the prefix redirect:, the UrlBasedViewResolver (and all subclasses) will recognize this as a special indication that a redirect is needed. The rest of the view name will be treated as the redirect URL.

Ali Dehghani
  • 46,221
  • 15
  • 164
  • 151
0

You can do this way redirect:cart.do and have requestmapping in controller which will return cart view. Hope this will work.

Raj Pandit
  • 11
  • 1
  • 6