Struts框架之转发和重定向
转发
转发操作是服务端的行为,在Servlet中使用request.getRequestDispatcher("jsp").forward(request,response)
。在Struts中需要在配置文件中对result的属性type定义为dispatcher。但是在struts-default.xml中,如果result标签的type属性默认为dispatcher。使用了default=true。转发操作由于是服务器端的行为,所以是一次请求,因此可以在request中传递值。
<result-types><result-type name="chain" class="com.opensymphony.xwork2.ActionChainResult"/><result-type name="dispatcher" class="org.apache.struts2.dispatcher.ServletDispatcherResult" default="true"/><result-type name="freemarker" class="org.apache.struts2.views.freemarker.FreemarkerResult"/><result-type name="httpheader" class="org.apache.struts2.dispatcher.HttpHeaderResult"/><result-type name="redirect" class="org.apache.struts2.dispatcher.ServletRedirectResult"/><result-type name="redirectAction" class="org.apache.struts2.dispatcher.ServletActionRedirectResult"/><result-type name="stream" class="org.apache.struts2.dispatcher.StreamResult"/><result-type name="velocity" class="org.apache.struts2.dispatcher.VelocityResult"/><result-type name="xslt" class="org.apache.struts2.views.xslt.XSLTResult"/><result-type name="plainText" class="org.apache.struts2.dispatcher.PlainTextResult" /><result-type name="postback" class="org.apache.struts2.dispatcher.PostbackResult" /></result-types>
新建ActionOperatorService.java类,此类主要进行转发和重定向的处理。
package com.struts.service;import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;/*** @TODO 本文中进行转发和重定向的操作* @author Administrator* @date 2015年7月7日 下午1:41:04* @version 1.0*/
@SuppressWarnings("serial")
public class ActionOperatorService extends ActionSupport{@Overridepublic String execute(){ActionContext.getContext().put("request_dispatcher", "转发操作的request请求");return Action.SUCCESS;}
}
配置文件修改如下:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd">
<struts><package name="action" extends="struts-default"><action name="dispatcher" class="com.struts.service.ActionOperatorService"><!-- 转发操作dispatcher操作 --><!-- <result name="success">/actionOperator.jsp</result> --><result name="success" type="dispatcher">/actionOperator.jsp</result></action></package>
</struts>
actionOperator.jsp页面使用EL表达式:
<%@ page language="java" contentType="text/html; charset=UTF-8"pageEncoding="UTF-8"%><%@page isELIgnored="false" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>转发和重定向操作</title>
</head>
<body><!--获取转发操作中request -->转发操作中的request:${request.request_dispatcher }<br/>
</body>
</html>
重定向
重定向是发生在客户端的行为。在Servlet中使用response.sendRedirect("jsp")
。重定向由于是客户端的行为,每次服务器响应完毕后,都需要客户端重新发送一次请求,因此对于request范围内的值时无法通过重定向传递的。
重定向到jsp
在struts中实现重定向有两种方式:一是重定向到jsp,一种是重定向到Action。其中重定向到jsp页面需要在result中定义type=redirect,重定向到Action需要在result中定义为type=redirectAction。
配置文件如下:
<action name="redirect" class="com.struts.service.ActionOperatorService"><!-- 重定向操作redirect操作 --><result name="success" type="redirect">/actionOperator.jsp</result></action>
重定向到Action(无参数)
在配置文件中配置result的属性type=redirectAction。重定向到Action是从一个Action转到另外一个Action。代码如下:
<action name="redirectAction" class="com.struts.service.ActionOperatorService"><!-- 重定向操作redirectAction操作 --><!-- <result name="success" type="redirectAction">ognl</result> --><result name="success" type="redirectAction"><param name="actionName">ognl</param><param name="namespace">/</param></result></action>
重定向到Action(带参数)
在重定向到Action不带参数中,使用了标签<param/>
,因此可以使用此标签进行参数的传递。代码如下:
<!-- 重定向操作redirectAction操作传递参数 --><result name="success" type="redirectAction"><param name="actionName">ognl</param><param name="namespace">/</param><!-- <param name="redirectActionParam">redirectActionParam</param> --><param name="redirectActionParam">${message}</param></result>
其中${message}中的message为ActionOperatorService中的成员变量。另外在ognl.action中接受参数需要定义成员变量或者使用request获取传递的参数。由于传递的参数为中文,因此在后台中输出为乱码,需要使用进行转码。
System.out.println(new String(redirectActionParam.getBytes("ISO-8859-1"),"UTF-8"));
System.out.println(new String(ServletActionContext.getRequest().getParameter("redirectActionParam").getBytes("ISO-8859-1"),"UTF-8"));
Struts框架之转发和重定向相关推荐
- SpringMVC框架 学习DAY_02 : 接收请求参数/向模板页面转发数据/重定向与转发 /Session
1. 接收客户端提交的请求参数 1.1. 使用HttpServletRequest接收请求参数 在处理请求的方法的参数列表中,添加HttpServletRequest类型的参数,在处理请求的过程中,调 ...
- 转发与重定向的使用详解
原文地址:http://www.2cto.com/kf/201107/97118.html 区别一: 重定向时浏览器上的网址改变 转发是浏览器上的网址不变 区别二: 重定向实际上产生了两次请求 转发只 ...
- springMVC3.0(文件上传,@RequestMapping加参数,@SessionAttributes,@ModelAttribute,转发,重定向,数值获取,传参,ajax,拦截器)
1.项目包结构如下: 2. spring配置文件springMVC.xml修改如下: <?xml version="1.0" encoding="UTF ...
- 【drp 12】再识转发和重定向:SpringMVC无法跳转页面
最近再使用SpringMVC进行页面跳转的时候,不知道发生了什么,始终都无法正确跳转.后来问题解决了,发现是对于转发和重定向没有能很好的理解,以此写篇博客,权当做积累了! 声明:本博客的所有代码,均为 ...
- 【Servlet】请求转发、重定向、Cookie使用之三天自动登陆
三天自动登陆项目源码:GitHub地址-LoginSystem JDBC 手写SORM框架 手写Servlet 开发环境:Eclipse 一.请求乱码问题解决 // 设置请求.响应编码格式req.se ...
- java struts 框架_java中struts 框架的实现
该文章主要简单粗暴的实现了struts的请求转发功能. 其他的功能后续会慢慢补上. 最近在学习javassist的内容,看到一篇文章 大家一起写mvc 主要简单的描述了mvc的工作流程,同时实现了 ...
- 转发和重定向简介及与之相关的(URL)参数(parameter)、属性(attribute)问题探讨
转发和重定向简介及与之相关的(URL)参数(parameter).属性(attribute)问题探讨 蜀中孤鹰 2019-07-15 15:55:49 101 收藏 最后发布:2019-07-15 1 ...
- 【JavaLearn】#(23)JSP相关语法、HTTP协议、Servlet介绍、Servlet生命周期、请求和响应、相对路径、转发和重定向
1. JSP简单内容 1.1 JavaEE JavaEE 包含JSP JavaEE是一个开发分布式企业级应用的规范和标准.JavaEE包含之前学过的所有内容(JavaSE) 真正开发中,很少使用Jav ...
- 创建线程的几种方式?JSP的九大内置对象及作用分别是什么?servlet的生命周期及常用方法?转发和重定向区别?ajax书写方式及内部主要参数都有哪些
文章目录 1 , 创建线程的几种方式? 问题扩展: 2 ,JSP的九大内置对象及作用分别是什么? (1) 问题分析: (2) 核心答案讲解: 1.request对象 2.response对象 3.se ...
最新文章
- this.$modal.confirm 自定义按钮关闭_Excel迫使人类基因重命名?用VBA给科学家们支一招!—— 怎样快速定制工具栏按钮...
- vtune mysql user_intel vtune 介绍、安装和使用
- python交互式编程客户端_【python】UDP网络编程:实现服务端与客户端的交互、简单的AI智能模式...
- JAVA入门级教学之(while循环语句)
- 调用拷贝构造函数的场景
- Maven分布式工程-分页查询(一)
- python列表常用方法_python 列表常用方法
- power automate desktop获取股票网页数据
- Zigbee之旅:第一个CC2430程序LED-IAR相关设置
- EXCEL VBA窗口快捷键冲突查找原因
- Mac老是自动关掉屏幕怎么办?
- iOS下载APP之后直接跳转到信任界面
- SEER数据库中肿瘤发病率计算并绘制发病率趋势图
- Leetcode 1833 雪糕的最大数量
- 7. 常见网络攻击欺骗手段与防护
- 多商户商城系统功能拆解07讲-平台端商品管理
- 聊聊WPF中字体的设置
- linux怎么杀僵尸进程
- 工业CT检测技术及工业CT基本组成
- 硅谷之谜(读书笔记)
热门文章
- 60万英语老师,在辛勤工作着,培养着一批又一批聋哑学生······
- pytorchOCR之DBnet(多类别文本检测1)
- Rosin-移动端页面开发调试Fiddler插件
- 实战maven私有仓库三部曲之一:搭建和使用
- 金蝶计算机快捷键,金蝶软件中主要的快捷键?
- log4j日志写入redis扩展实现(log4j-redis-appender)
- 湖北申报!2022年湖北省创新型产业集群申报条件、申报程序和考核评估
- python3从头开始学-数据类型-字典 05
- Windows环境搭建WeGame饥荒专用服务器(详细版)
- 光模块的传输距离如何得出?