Struts2之Action详解2

 

关于 Struts2 请求的扩展名问题

 

1). org.apache.struts2 包下的 default.properties 中配置了 Struts2 应用个的一些常量

2). struts.action.extension 定义了当前 Struts2 应用可以接受的请求的扩展名.

3). 可以在 struts.xml 文件中以常量配置的方式修改 default.properties 所配置的常量.

<constant name="struts.action.extension" value="action,do,"></constant>

 

ActionSupport

 

1). ActionSupport 是默认的 Action 类: 若某个 action 节点没有配置 class 属性, 则 ActionSupport 即为

待执行的 Action 类. 而 execute 方法即为要默认执行的 action 方法

<action name="testActionSupport">

<result>/testActionSupport.jsp</result>

</action>

 

等同于

 

<action name="testActionSupport"

class="com.opensymphony.xwork2.ActionSupport"

method="execute">

<result>/testActionSupport.jsp</result>

</action>

 

2). 在手工完成字段验证, 显示错误消息, 国际化等情况下, 推荐继承 ActionSupport. 

 

 

result

 

1). result 是 action 节点的子节点

2). result 代表 action 方法执行后, 可能去的一个目的地

3). 一个 action 节点可以配置多个 result 子节点. 

4). result 的 name 属性值对应着 action 方法可能有的一个返回值. 

 

<result name="index">/index.jsp</result>

 

5). result 一共有 2 个属性, 还有一个是 type: 表示结果的响应类型

6). result 的 type 属性值在 struts-default 包的 result-types 节点的 name 属性中定义.

         常用的有 

    > dispatcher(默认的): 转发. 同 Servlet 中的转发. 

    > redirect: 重定向

    > redirectAction: 重定向到一个 Action

    注意: 通过 redirect 的响应类型也可以便捷的实现 redirectAction 的功能!

   

    <result name="index" type="redirectAction">

<param name="actionName">testAction</param>

<param name="namespace">/atguigu</param>

</result>

 

OR

 

<result name="index" type="redirect">/atguigu/testAction.do</result>

   

    > chain: 转发到一个 Action

    注意: 不能通过 type=dispatcher 的方式转发到一个 Action

   

         只能是:

         

    <result name="test" type="chain">

<param name="actionName">testAction</param>

<param name="namespace">/atguigu</param>

</result>

 

不能是:

 

<result name="test">/atguigu/testAction.do</result>

 

示例

 

1. 添加 JSP页面

 

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!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>result的type属性</title>
</head>
<body>
    
    <a href="resultType.action?type=dispatcher">默认dispatcher</a><br/><br/>
    
    <a href="resultType.action?type=redirect">redirect: 重定向</a><br/><br/>
    
    <a href="resultType.action?type=redirectAction">redirectAction重定向到一个 Action</a><br/><br/>
    
    <a href="resultType.action?type=redirect-redirectAction">redirect实现redirectAction</a><br/><br/>
    
    <a href="resultType.action?type=chain">chain:转发到一个 Action</a><br/><br/>
    
    <a href="resultType.action?type=dispatcher-chain">不能通过 type=dispatcher的方式转发到一个 Action</a><br/><br/>
    
</body>
</html>

 

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!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>result的type属性</title>
</head>
<body>
     
    <h1>获取请求数据</h1>
    
    actionName: ${requestScope.actionName }<br/>
    newActionName: ${requestScope.newActionName }<br/>
    
    <hr/>
    <a href="/org.rabbitx.web.struts2.action/resultType/index.jsp" style="float:right;">返回首页</a>
</body>
</html>

 

 2. 添加Action

 

package org.rabbitx.web.struts2.result.action;

import java.util.Map;

import org.apache.struts2.interceptor.ParameterAware;
import org.apache.struts2.interceptor.RequestAware;

import com.opensymphony.xwork2.ActionSupport;

public class ResultTypeAction extends ActionSupport implements ParameterAware,RequestAware{

	private static final long serialVersionUID = 1L;

	private Map<String, String[]> parameters;
	
	private Map<String, Object> request;
	
	@Override
	public String execute() throws Exception {
		
		request.put("actionName", "ResultTypeAction");
		
		return parameters.get("type")[0];
	}

	@Override
	public void setParameters(Map<String, String[]> parameters) {
		this.parameters = parameters;
	}
	
	@Override
	public void setRequest(Map<String, Object> request) {
		this.request = request;
	}

}

 

 

package org.rabbitx.web.struts2.result.action;

import java.util.Map;

import org.apache.struts2.interceptor.RequestAware;

public class RedirectTestAction  implements RequestAware{

	private Map<String, Object> request;
	
	public String execute()
	{
		request.put("newActionName", "RedirectTestAction");
		
		System.out.println("--------RedirectTestAction--------");
		
		return "dispatcher";
	}
	
	@Override
	public void setRequest(Map<String, Object> request) {
		this.request = request;
	}
}

 

 3. 配置Action

 

	<package name="result-type" extends="struts-default">
	    
	    <action name="resultType" class="org.rabbitx.web.struts2.result.action.ResultTypeAction">
	    
	        <result name="dispatcher" type="dispatcher">/resultType/viewData.jsp</result>
	        
	        <result name="redirect" type="redirect">/resultType/viewData.jsp</result>
	        
	        <result name="redirectAction" type="redirectAction">
                <param name="actionName">redirectTest</param>
                <param name="namespace">/</param>
            </result>
            
	        <result name="redirect-redirectAction" type="redirect">redirectTest.action</result>
	        
	        <result name="chain" type="chain">
                <param name="actionName">redirectTest</param>
                <param name="namespace">/</param>
            </result>
	        
	        <result name="dispatcher-chain" type="dispatcher">redirectTest.action</result>
	        
	    </action>
	    
	    <action name="redirectTest" class="org.rabbitx.web.struts2.result.action.RedirectTestAction">
	        <result name="dispatcher" type="dispatcher">/resultType/viewData.jsp</result>
	    </action>
	</package>

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值