JSP,Spring MVC 页面重定向

问题描述 投票:0回答:3

需要您帮助解决以下错误。

我正在尝试使用 Spring MVC 从 index.jsp 重定向到第二页。但是得到这个错误。

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

我的控制器类是,

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";
    }
}

控制器只是尝试将用户请求重定向到第二页。

我的 web.xml 是

<!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 如下

<?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 为

<?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 如下。

<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>
spring jsp tomcat6
3个回答
2
投票

return "bravo";
不会重定向(我希望当你说重定向时你认为有 HTTP 302)。它将尝试查找相对于您当前路径的视图(如果成功则重现 HTTP 200)。

对于重定向你应该使用

return "redirect:bravo"

但是首先你的 jsp 文件有错误。

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

应该改为

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

0
投票

除了上面的回答,还需要去掉

@ResponseBody
注解,让Spring把返回值识别为一个
View
的名字,而不是一个应该直接返回给客户端的
String


0
投票

将以下代码添加到 index.jsp 中,它将重定向到需要在控制器类中捕获的 /redirect。

JSP:

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

弹簧控制器:

 @RequestMapping(value = "/redirect", method = RequestMethod.GET)
 public void doGet() throws IOException { 
 // add logic here
 }
© www.soinside.com 2019 - 2024. All rights reserved.