关于 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中struts_xml的Action配置详解
Struts2属性文件详解 struts.configuration 该属性指定加载Struts 2配置文件的配置文件管理器.该属性的默认值是org.apache.Struts2.config.DefaultConfiguration, 这是Struts 2默认的配置文件管理器.如果需要实现...
struts2 配置文件详解 ,配置struts2 action和strtus1是不一样的,需要特殊处理一下
从 Struts1 升级到 Struts2: 1、Struts1 里使用 ActionServlet 作为控制器;... Struts2 中任何一个 POJO 都可以是一个 Action 类. 5、Struts2 在页面里使用 OGNL 来显示各种对象模型, 可以不再使用 EL 和 JSTL
Struts2和Struts1的常见区别 二. Struts2入门 3. 快速配置Struts2环境 4. 开发一个Struts2简单实例 5. struts.xml文件 三. Struts2深入开发 6. 常用<Action>标签配置和使用 7. 常用标签配置和使用 8....
Struts 2的处理流程与Action的管理方式 为应用指定多个配置文件 动态方法调用和使用通配符定义action 请求参数接收 自定义类型转换器 全局类型转换器 访问或添加几个属性 文件上传 多文件上传 自定义拦截...
本篇文章主要介绍了Struts 2 配置Action详解,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
struts2配置详解,完全从零开始教你配置
struts面试题详解,Struts1框架以ActionServlet作为核心控制器,整个应用由客户端请求驱动。
struts.action.extension The URL extension to use to determine if the request is meant for a Struts action 用URL扩展名来确定是否这个请求是被用作Struts action,其实也就是设置 action的后缀,例如login....
详解Struts2中Action访问Servlet API的几种方法 在通常的web开发中Request和Response对象比较常见,但在Struts2框架中由于Action能与JSP页面进行数据交互,所以通常都不会用到这两个对象。如果想在Struts2程序中用到...
主要介绍了Struts2的配置 struts.xml Action详解,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
namespace :对应与项目名称后面的"/"(例如Struts2_0100_Introduction后面的"/") (http://localhost:8080/Struts2_0100_Introduction/) 四、 标签 是用来解决重名的问题,例如当系统的前台和后台都有一个action...
十二、总结 本教程对struts2的基本知识进行了一些说明,关于struts2的更多详细内容应参看struts2的官方文档及提供的app实例。 下面对struts2的基本执行流程作一简要说明,此流程说明可以结合官方提供的struts2结构图...
本章学习目标 struts2 的概念和作用 struts2 的HelloWorld 简单分析struts2 的运行流程 ... struts.xml 配置文件详解 struts2 常量文件修改 Action 动作类的三种写法 Action 动作类的三种访问方式
主要介绍了Struts2之Action接收请求参数和拦截器详解,非常具有实用价值,需要的朋友可以参考下
"http://jakarta.apache.org/struts/dtds/struts-config_1_2.dtd"> <struts-config> <action-mappings> <action path="" attribute="" type="" name="" scope="" validate="" input="" / >...
struts1.x_action属性详解
主要介绍了struts1之ActionServlet详解,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧