Saturday, 26 December 2015

Spring redirect using mvc:redirect-view-controller tag


This is an extension of my previous post on 
So if you have directly landed here I would highly recommend to read it first for setup.  In this post we will essentially see how can we redirect one URL to other using mvc:redirect-view-controller tag configuration.

Spring redirect using mvc:redirect-view-controller tag

Basic setup remains the same as that of previous post

NOTE : For this Spring 4.1+ is required! So change your ivy dependency accordingly.

        <dependency org="org.springframework" name="spring-webmvc"
            <exclude org="javax.servlet" name="javax.servlet-api" />
            <exclude org="javax.servlet.jsp" name="jsp-api" />
            <exclude org="javax.el" name="javax.el-api" />

Now only change that you need to do in your spring configuration is as follows - 

    <mvc:redirect-view-controller redirect-url="/TestWebProject/newtest" path="/oldtest" status-code="308" keep-query-params="true"/>
    <mvc:view-controller path="/newtest" view-name="test"/>

All it says is if you get a URL "/oldtest" redirect it to new URL "/newtest" and the 2nd line states if you get url "/newtest" then render view with logical name test which will again render the same JSP test.jsp as per the InternalResourceViewResolver.

Notice the redirect with 308 response status.

Alternate way to redirect is using

<mvc:view-controller path="/oldtest" view-name="redirect:newtest"/>
 <mvc:view-controller path="/newtest" view-name="test"/>

Again notices the 302 response code.

NOTE : It is not desirable to use version in xsd declaration. Spring will use highest version in the project's dependencies if version not given.

No comments:

Post a comment

t> UA-39527780-1 back to top