Ranish
Ranish

Reputation: 36

JSP, Spring MVC page redirect

need your help on following error.

I am trying redirect to a second page from index.jsp using Spring MVC. But getting this error.

HTTP Status 404 - /redirect.do
description The requested resource is not available.

My controller class is,

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class MainInvController {

    @RequestMapping(value="/redirect")// , method = RequestMethod.GET
    @ResponseBody
    public String redirect() {
        System.out.println("redirect");
        return "bravo";
    }
}

Controller just try to redirect the user request to second page.

And my web.xml is

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
    <display-name>Sample Inventory App</display-name>

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath*:META-INF/spring/spring-master.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener> 

    <servlet>
        <servlet-name>invsample</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfiguration</param-name>
            <param-value>WEB-INF/invsample-servlet.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>invsample</servlet-name>
        <url-pattern>*.do</url-pattern>
    </servlet-mapping>

    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>     
</web-app>

invsample-servlet.xml as follows.

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc" 
       xsi:schemaLocation="http://www.springframework.org/schema/beans 
                           http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
                           http://www.springframework.org/schema/context 
                           http://www.springframework.org/schema/context/spring-context-4.0.xsd
                           http://www.springframework.org/schema/mvc
                           http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">

    <mvc:annotation-driven/>

    <context:component-scan base-package="com.sample">
        <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>


    <bean id="viewResolver"
        class="org.springframework.web.servlet.view.UrlBasedViewResolver">
        <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
        <property name="prefix" value="" />
        <property name="suffix" value=".jsp" />
    </bean>

</beans>

spring-master.xml as

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc" 
       xsi:schemaLocation="http://www.springframework.org/schema/beans 
                           http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
                           http://www.springframework.org/schema/context 
                           http://www.springframework.org/schema/context/spring-context-4.0.xsd">



    <context:property-placeholder location="classpath*:META-INF/spring/*.properties"/>

    <!-- Load everything except @Controllers -->
    <context:component-scan base-package="com.sample">
        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>

</beans>

index.jsp is given below.

<html>
<body>
<h2>Hello World!</h2>
<h2>Spring Page Redirection</h2>
<p>Click below button to redirect the result to new page</p>
<form method="GET" action="/redirect.do">
<table>
    <tr>
    <td>
    <input type="submit" value="Redirect Page"/>
    </td>
    </tr>
</table>  
</form>
</body>
</html>

Upvotes: 0

Views: 3378

Answers (3)

Du-Lacoste
Du-Lacoste

Reputation: 12757

Add the below code to the index.jsp and it will do the redirection to /redirect which need to be captured in the controller class.

JSP:

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<body>
<c:redirect url="/redirect"/>
</body>
</html>

Spring Controller:

 @RequestMapping(value = "/redirect", method = RequestMethod.GET)
 public void doGet() throws IOException { 
 // add logic here
 }

Upvotes: 0

Josh Ghiloni
Josh Ghiloni

Reputation: 1300

In addition to the answer above, you need to remove the @ResponseBody annotation, so that Spring recognizes the return value as the name of a View, not a String that should be returned directly to the client.

Upvotes: 0

mommcilo
mommcilo

Reputation: 946

return "bravo"; will not redirect(I hope when you said redirect you thought to have HTTP 302). It will try to find view relative to your current path(and reproduce HTTP 200 if success).

For redirect you should use

return "redirect:bravo"

But first you have an error in your jsp file.

<form method="GET" action="/redirect.do">

should be changed to

<form method="GET" action="/redirect">

Upvotes: 2

Related Questions