Saravanan S
Saravanan S

Reputation: 1043

Spring MVC - Handling redirection

I want to redirect a page in server side (using spring), but the URL should remain the same.

For ex: if user tries http://www.example.com/page1, I want to render content of http://www.example.com/page2 in browser but the URL should still point to http://www.example.com/page1.

I tried 301, 302, 307 redirects, but all page URLs are changing to http://www.example.com/page2.

Is there anyway to achieve this?

Upvotes: 2

Views: 2174

Answers (1)

Marcel Stör
Marcel Stör

Reputation: 23535

It's a problem of terminology. What you're looking for is forward rather than redirect. If you're interested you may want to look that up e.g. here: http://www.javapractices.com/topic/TopicAction.do?Id=181.

There are at least two ways of doing this:

Traditional, RequestDispatcher can be used outside a Spring WebMVC application, too.

public class MyController extends AbstractController {

    @Override
    protected void handleRequestInternal(HttpServletRequest request, HttpServletResponse response)
    throws Exception {
        request.getRequestDispatcher("/new/path").forward(request, response);
    }
}

Spring WebMVC notation:

public class MyController extends AbstractController {

    @Override
    protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response)
    throws Exception {
        return new ModelAndView("forward:/new/path");
    }
}

Upvotes: 2

Related Questions