您的位置:首页 > 编程语言 > Java开发

Struts框架之转发和重定向

2015-07-07 13:27 555 查看

转发

转发操作是服务端的行为,在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{

@Override
public 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
b59f
>
</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"));
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息