SpringMVC 专题
您的位置:java > SpringMVC专题 > Spring MVC页面重定向
Spring MVC页面重定向
作者:--    发布时间:2019-11-19

以下示例显示如何编写一个简单的基于web的重定向应用程序,这个应用程序使用重定向将http请求传输到另一个页面。首先使用eclipse ide,并按照以下步骤使用spring web framework开发基于动态表单的web应用程序:

  1. 基于spring mvc - hello world实例章节中代码,创建创建一个名称为 pageredirection 项目。

  2. com.h3.springmvc 包下创建一个java类webcontroller

  3. jsp子文件夹下创建一个视图文件index.jspfinal.jsp
  4. 最后一步是创建所有源和配置文件的内容并导出应用程序,如下所述。

完整的项目代码,如下所示 -

webcontroller.java 文件中的代码如下所示 -

package com.h3.springmvc;

import org.springframework.stereotype.controller;
import org.springframework.web.bind.annotation.requestmapping;
import org.springframework.web.bind.annotation.requestmethod;

@controller
public class webcontroller {

   @requestmapping(value = "/index", method = requestmethod.get)
   public string index() {
       return "index";
   }

   @requestmapping(value = "/redirect", method = requestmethod.get)
   public string redirect() {

      return "redirect:finalpage";
   }

   @requestmapping(value = "/finalpage", method = requestmethod.get)
   public string finalpage() {

      return "final";
   }
}

下面是spring视图文件index.jsp的内容。这是一个登陆页面,这个页面将发送访问重定向方法的请求,将重定向这个请求到另一个服务方法,最后将显示final.jsp页面的内容。

index.jsp 文件中的代码如下所示 -

<%@ page contenttype="text/html; charset=utf-8"%>
<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<html>
<head>
<title>spring mvc页面重定向</title>
</head>
<body>
    <h2>spring mvc页面重定向</h2>
    <p>点击下面的按钮将结果重定向到新页面</p>
    <form:form method="get" action="/pageredirection/redirect">
        <table>
            <tr>
                <td><input type="submit" value="页面重定向" /></td>
            </tr>
        </table>
    </form:form>
</body>
</html>

final.jsp 文件中的代码如下所示 -

<%@ page contenttype="text/html; charset=utf-8"%>
<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<html>
<head>
<title>spring重定向页面</title>
</head>
<body>
    <h2>重定向页面...</h2>
</body>
</html>

完成创建源和配置文件后,导出应用程序。 右键单击应用程序,并使用导出> war文件选项,并将 pageredirection.war 文件保存在tomcat的webapps文件夹中。或者直接右键选择“run as -> run on server”。

启动tomcat服务器,并确保您能够使用标准浏览器从webapps文件夹访问其他网页。现在尝试访问 url => http://localhost:8080/helloweb/index ,如果spring web应用程序没有问题,那么应该看到以下结果:

现在点击“重定向页面”按钮提交表单并获得最终重定向页面。如果spring web应用程序没有问题,那么应该看到以下结果:


网站声明:
本站部分内容来自网络,如您发现本站内容
侵害到您的利益,请联系本站管理员处理。
联系站长
373515719@qq.com
关于本站:
编程参考手册