Spring MVC页面重定向
以下示例显示如何编写一个简单的基于Web的重定向应用程序,这个应用程序使用重定向将http请求传输到另一个页面。
- 基于Spring MVC - Hello World实例章节中代码,创建创建一个名称为 PageRedirection 项目。
- 在
com.ktao.controller包下创建一个Java类WebController。 - 在
jsp子文件夹下创建一个视图文件index.jsp,final.jsp。 - 最后一步是创建所有源和配置文件的内容并导出应用程序,如下所述。
完整的项目代码,如下所示 -

配置文件
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
dispatcher-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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <context:component-scan base-package="com.ktao.controller"/> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
控制器
WebController.java
webpackage com.ktao.controller; 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";
} }
视图
index.jsp
<%--
Created by IntelliJ IDEA.
User: ktao
Date: 17-12-2
Time: 下午10:46
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@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="redirect">
<table>
<tr>
<td><input type="submit" value="页面重定向" /></td>
</tr>
</table>
</form:form>
</body>
</html>
final.jsp
<%--
Created by IntelliJ IDEA.
User: ktao
Date: 17-12-2
Time: 下午10:46
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<html>
<head>
<title>Spring重定向页面</title>
</head>
<body>
<h2>重定向页面...</h2>
</body>
</html>
运行结果


Spring MVC页面重定向的更多相关文章
- 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页面. 即使这 ...
- Spring MVC 页面跳转时传递参数
页面仍然使用 JSP,在跳转时如果想传递参数则需要用到类 RedirectAttributes. 首先看看如何打开一个普通页面: // 登录页面(每个页面都要独立的 Action 来支持其呈现) @R ...
- 【Spring学习笔记-MVC-16】Spring MVC之重定向-解决中文乱码
概述 spring MVC框架controller间跳转,需重定向,主要有如下三种: 不带参数跳转:形如:http://localhost:8080/SpringMVCTest/test/myRedi ...
- spring mvc redirect 重定向 跳转并传递参数
在项目中做form表单功能提交时,防止用户客户端后退或者刷新时重复提交问题,需要在服务端进行重定向跳转,具体跳转方式有以下几种方式: 公用代码: @RequestMapping(value=" ...
- Spring mvc后台重定向页面,实际前端不跳转
1.ajax不支持重定向 ajax是不支持重定向的,因为ajax本身就是局部刷新,不重新加载页面的. 2.若后台出现需要重定向页面,可以设置唯一错误码 前端ajax公共调用后,凡是遇到这一类错误码,则 ...
- spring mvc页面显示图片失败
在配置文件中需要映射静态资源 <!-- 当在web.xml 中 DispatcherServlet使用 <url-pattern>/</url-pattern> 映射时, ...
随机推荐
- js 点击 返回顶部 动画
附上效果图 触发前 触发后 HTML代码: CSS代码 JS代码 由于复制文本太丑了 所以直接放的图片 但是我在评论区把js代码又复制了一边 以便你们使用
- app测试中遇到问题总结
工作总结: 1 这两天由于工作,需要进行抓包,使用了Charles,fidder,发现一个坑点: charles没有抓到返回值的时候,默认是不在列表显示请求信息的,能不能设置,我就不知道了,但是可以在 ...
- salesforce零基础学习(八十七)Apex 中Picklist类型通过Control 字段值获取Dependent List 值
注:本篇解决方案内容实现转自:http://mysalesforceescapade.blogspot.com/2015/03/getting-dependent-picklist-values-fr ...
- LDAP的用户需求
使用LDAP(ApacheDS)构建统一认证服务(SSO单点登录) 构建团队协作的体系,需要涉及很多个系统,如SVN.Jenkins.Trac.Nexus等,而一般而言每个系统均有其用户体系,当我 ...
- 19届华为实习生笔试之判断iPv6地址类型
题二: 答案: #coding=utf-8 import re,sys str = sys.stdin.readline().strip() def regex(str): result = &quo ...
- 访问器属性:setter()函数和getter()函数
1.干嘛用的? getter()函数:返回有效的值 setter()函数:调用它并传入数据,这个函数决定如何处理数据 2.具备哪些属性?如何定义? configurable(默认为true),enum ...
- tr069开源代码——cwmp移植
原创作品,转载请注明出处,严禁非法转载.如有错误,请留言! email:40879506@qq.com 声明:本系列涉及的开源程序代码学习和研究,严禁用于商业目的. 如有任何问题,欢迎和我交流.(企鹅 ...
- SQL 语句 (二) --- SELECT
1 完整句法: SELECT [ ALL | DISTINCT TOP n [] WITH TIES select_list [INTO [new_table_name] ] [FROM {table ...
- webstorm中github的配置
1.申请一个github账号,我这里的操作是已经有了账号的情况之下进行的. 打开webstorm,File-->settings,弹出settings框,输入git,得到以下界面,输入githu ...
- 【webstorm使用手册】如何安装插件
Files->Settings, 搜索"plugin",如图,