Struts2_Action中result的各种转发类型

47 篇文章 0 订阅

Action中result的各种转发类型:

	<action name="helloworld" class="cn.itcast.action.HelloWorldAction"
		method="execute">
		<result name="success">/WEB-INF/page/hello.jsp</result>
	</action>
	
	result配置类似于struts2中的forward,但struts2中提供了多种结果类型,常用的类型有:dispatche(默认值)
	、redirect、redirectAction、plainText。
	
	在result中还可以使用${属性名}表达式访问action中的属性,表达式里的属性名对应action中的属性。如下:
	<result type="redired">/view.jsp?id=${id}</result>
	
	下面是redirectAction结果类型的例子,如果重定向的action中同一个包下:
	<result type="redirectAction">helloworld</result>
	如果重定向的action在别的命名空间下:
	<result type="redirectAction">
		<param name="actionName">helloworld</param>
		<param name="namespace">/test</param>
	</result>
	plaintext:显示原始文件内容,例如:当我们需要原样显示jsp文件源代码的时候,我们可以使用此类型。
	<!-- plainText定各显示网页源代码 -->
	<action name="plainText">
		<result type="plainText">
			<param name="location">/index.jsp</param>
			<param name="charSet">UTF-8</param><!-- 指定读取文件的编码 -->
		</result>
	</action>

struts.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
	"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
	"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
	<package name="bese" extends="struts-default">
		<!-- 全局action视图 -->
		<global-results>
			<result name="message">/WEB-INF/page/msessage.jsp</result>
		</global-results>
	</package>

	<package name="itcast" namespace="/test" extends="bese">

		<action name="list" class="cn.itcast.action.HelloWorldAction"
			method="execute">
			<result name="success" type="redirect">/employeeAdd.jsp?username=${username}
			</result>
		</action>

		<action name="message" class="cn.itcast.action.HelloWorldAction"
			method="add">
		</action>

		<action name="addUI">
			<result>/WEB-INF/page/employeeAdd.jsp</result>
		</action>

		<!-- redirect重定向 -->
		<action name="redirect">
			<result type="redirect">/employeeAdd.jsp</result>
		</action>

		<!-- redirectAction重定向(重定向action) -->
		<!-- redirectAction重定向到包 -->
		<action name="redirectAction">
			<!-- <result type="redirectAction">list</result> -->
			<result type="redirectAction">
				<param name="actionName">xxx</param>
				<param name="namespace">/department</param>
			</result>
		</action>

		<!-- plainText定各显示网页源代码 -->
		<action name="plainText">
			<result type="plainText">
				<param name="location">/index.jsp</param>
				<param name="charSet">UTF-8</param><!-- 指定读取文件的编码 -->
			</result>
		</action>
	</package>

	<package name="other" namespace="/department" extends="bese">
		<action name="message" class="cn.itcast.action.HelloWorldAction"
			method="add">
		</action>

		<action name="xxx">
			<result>/WEB-INF/page/hello.jsp</result>
		</action>
	</package>
</struts>
HelloWorldAction.java

package cn.itcast.action;

import java.net.URLEncoder;

public class HelloWorldAction {
	private String msg;
	private String username;

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getMessage() {
		return msg;
	}

	public String execute() throws Exception {
		this.username = URLEncoder.encode("中国人民万岁", "utf-8");
		msg = "我的第一个struts2应用";
		return "success";
	}

	public String add() {
		return "message";
	}
}

jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>

<title>My JSP 'msessage.jsp' starting page</title>

<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">


</head>

<body>
	结果
	<br/>
</body>
</html>

<%@ page language="java" import="java.util.*,java.net.URLDecoder"
	pageEncoding="utf-8"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'employeeAdd.jsp' starting page</title>

<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
</head>

<body>
	<%=URLDecoder.decode(new String(request.getParameter("username").getBytes("ISO8859-1"),"utf-8"), "utf-8")%>
	<form action="/xxx">
		姓名:<input type="text" name="xxxx" />
	</form>
</body>
</html>

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'employeeAdd.jsp' starting page</title>

<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
</head>

<body>
	<form action="/xxx">
		姓名:<input type="text" name="xxxx" />
	</form>
</body>
</html>


<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'index.jsp' starting page</title>
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->
  </head>
  
  <body>
    	<%=new Date() %>中国
  </body>
</html>



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值