Spring MVC-页面重定向示例(转载实践)
以下内容翻译自:https://www.tutorialspoint.com/springmvc/springmvc_page_redirection.htm
说明:示例基于Spring MVC 4.1.6。
以下示例显示如何编写一个简单的基于Web的应用程序,该应用程序利用重定向将http请求传输到另一个页面。首先,让我们使用Eclipse IDE,并按照以下步骤使用Spring Web Framework开发基于动态窗体的Web应用程序:
步骤 | 描述 |
---|---|
1 | 创建一个名为HelloWeb的项目,在一个包com.tutorialspoint下,如Spring MVC - Hello World Example章节所述。 |
2 | 在com.tutorialspoint包下创建一个Java类WebController。 |
3 | 在jsp子文件夹下创建一个视图文件index.jsp,final.jsp。 |
4 | 最后一步是创建所有源和配置文件的内容并导出应用程序,如下所述。 |
WebController.java
package com.tutorialspoint; 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
<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<html>
<head>
<title>Spring Page Redirection</title>
</head>
<body>
<h2>Spring Page Redirection</h2>
<p>Click below button to redirect the result to new page</p>
<form:form method="GET" action="/HelloWeb/redirect">
<table>
<tr>
<td>
<input type="submit" value="Redirect Page"/>
</td>
</tr>
</table>
</form:form>
</body>
</html>
final.jsp
<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<html>
<head>
<title>Spring Page Redirection</title>
</head>
<body> <h2>Redirected Page</h2> </body>
</html>
完成创建源和配置文件后,导出应用程序。右键单击应用程序并使用Export > WAR File选项,并将您的HelloWeb.war文件保存在Tomcat的webapps文件夹中。
现在启动您的Tomcat服务器,并确保您可以使用标准浏览器从webapps文件夹访问其他网页。现在尝试URL http://localhost:8080/HelloWeb/index,如果您的Spring Web应用程序的一切都很好,您应该会看到以下结果:
现在点击“重定向页面”按钮提交表单并获得最终重定向页面。如果您的Spring Web应用程序的一切都很好,您应该会看到以下结果:
Maven示例:
https://github.com/easonjim/5_java_example/tree/master/springmvc/tutorialspoint/test2
Spring MVC-页面重定向示例(转载实践)的更多相关文章
- Spring MVC页面重定向
以下示例显示如何编写一个简单的基于Web的重定向应用程序,这个应用程序使用重定向将http请求传输到另一个页面. 基于Spring MVC - Hello World实例章节中代码,创建创建一个名称为 ...
- spring mvc: 页面重定向调整
我的项目名称是hello, 在src/main/java目录下面建了一个chapter2目录 有三个配置文件: web.xml, chapter2-servlet.xml, applicationCo ...
- Spring MVC页面重定向实例
以下内容引用自http://wiki.jikexueyuan.com/project/spring/mvc-framework/spring-page-redirection-example.html ...
- MVC页面重定向'页面跳转
MVC页面重定向,主要有以下几种形式: 1.Response.Redirect();方法 using System; using System.Collections.Generic; using S ...
- spring MVC页面的重定向
如图,一个jsp页面跳转到下一个jsp页面通常需要上一个页面发出带有参数得请求,我们都知道spring MVC是不能直接跳页面的. 需要配置视图解析器,通过返回视图名再跳转到相应得JSP页面. 即使这 ...
- 搭建Spring4+Spring MVC web工程的最佳实践
Spring是个非常非常非常优秀的java框架,主要是用它的IOC容器帮我们依赖注入和管理一些程序中的Bean组件,实现低耦合关联,最终提高系统可扩展性和可维护性,用它来辅助我们构建web工程将会感觉 ...
- Spring MVC - Hello World示例
以下示例演示如何使用Spring MVC框架编写一个简单的基于Web的Hello World应用程序.首先使用Eclipse IDE,并按照以下步骤使用Spring Web Framework开发一个 ...
- Spring MVC 入门教程示例 (一)
今天和大家分享下 Spring MVC 入门教程 首先还是从 HelloWorld web 工程开始 -------------------------- 1.首先创建一个Maven Web工程 ...
- Spring MVC 页面跳转时传递参数
页面仍然使用 JSP,在跳转时如果想传递参数则需要用到类 RedirectAttributes. 首先看看如何打开一个普通页面: // 登录页面(每个页面都要独立的 Action 来支持其呈现) @R ...
随机推荐
- Codeforces--626B--Cards(模拟)
Cards Time Limit: 2000MS Memory Limit: 262144KB 64bit IO Format: %I64d & %I64u Submit Sta ...
- centos安装lamp步骤还可以
1. 用yum安装Apache,Mysql,PHP. 1.1安装Apache yum install httpd httpd-devel 安装完成后,用/etc/init.d/httpd start ...
- hdu3652B-number(数位dp)
B-number Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Su ...
- python-day2 切片,格式化输出,函数
1.切片:取元素 格式;变量名[M:N:K] M 表示开始元素索引值, N 表示结束元素索引值(不包含索引值本身) K 表示步长,隔几个切一次 例子:a='hello python' p ...
- HDU3085 Nightmare Ⅱ
题目: Last night, little erriyue had a horrible nightmare. He dreamed that he and his girl friend were ...
- Winform 异步调用2 时间
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...
- JavaScript alert()函数的使用方法
这里向大家简单介绍一下JavaScript alert()函数的使用,alert--弹出消息对话框,并且alert消息对话框通常用于一些对用户的提示信息. JavaScript alert()函数 a ...
- C#接入第三方支付一些小问题
13年第一次接入支付宝的时候,支付宝的api还不是很好用,费了些劲才完成,本月再次接入的时候发现已经很好用了,接入过程非常顺畅,只出现了一个小问题,我的金额默认是保留了4位小数,支付宝api只接受最多 ...
- Beta冲刺-星期五
这个作业属于哪个课程 <课程的链接> 这个作业要求在哪里 <作业要求的链接> 团队名称 Three cobblers 这个作业的目标 完成项目最后的冲刺 ...
- JavaScript 继承代码中,B.prototype = new A(); 的含义是什么?[转自知乎]
假设有如下代码: function A() {this.name = "A"} function B() {this.name = "B"} A.prototy ...