关于 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>
本文详细介绍了Struts2框架中的结果类型配置方法,包括dispatcher、redirect、redirectAction及chain等,并通过具体示例展示了如何配置这些结果类型来实现不同的业务需求。

240

被折叠的 条评论
为什么被折叠?



