【Struts2】04 Action 分析
Action的三种编写方式:
方式一,创建普通的Java类
package cn.dzz.action; /** * @author Echo42 * @file Struts2 * @create 2020年08月29日13:44 */ public class HelloAction { /** * servlet 默认执行service方法 * struts2 默认执行execute方法 * @return */ public String execute(){ return "ok"; } public String add(){ return "aaa"; } }
方式二,创建Java类,实现Action接口
package cn.dzz.action; import com.opensymphony.xwork2.Action; /** * @author Echo42 * @file Struts2 * @create 2020年08月29日15:55 */ public class ActionMode02 implements Action { public String execute() throws Exception { return null; } }
查看Action接口的源码
package com.opensymphony.xwork2; public interface Action { String SUCCESS = "success"; String NONE = "none"; String ERROR = "error"; String INPUT = "input"; String LOGIN = "login"; String execute() throws Exception; }
一些常用的返回值,可以使用这个接口的常量代替:

方式三,创建Java类,继承ActionSupport类
package cn.dzz.action; import com.opensymphony.xwork2.ActionSupport; /** * @author Echo42 * @file Struts2 * @create 2020年08月29日16:00 */ public class ActionMode3 extends ActionSupport { @Override public String execute() throws Exception { return super.execute(); } }
Action的方法访问:
三种实现方式:
1、使用action标签的method属性
package cn.dzz.action; import com.opensymphony.xwork2.ActionSupport; /** * @author Echo42 * @file Struts2 * @create 2020年08月29日16:00 */ public class ActionMode3 extends ActionSupport { @Override public String execute() throws Exception { return super.execute(); } public String method1(){ System.out.println("- - - method1 - - -"); return NONE; } public String method2(){ System.out.println("- - - method2 - - -"); return NONE; } }
struts.xml配置:
<action name="m1" class="cn.dzz.action.ActionMode3" method="method1" /> <action name="m2" class="cn.dzz.action.ActionMode3" method="method2" />
2、通配符方式实现
<!-- 该Action可以访问的地址是: m_add, m_alt, m_get, m_del 等等 例如:http://localhost:8080/m_add 这样 而method的名称使用的OGNL表达式{1}匹配name属性的第一个*通配符 意思是,我的action中有一个名为method01的方法, 如果访问地址是http://localhost:8080/m_method01 就可以被访问到 --> <action name="m_*" class="cn.dzz.action.ActionMode3" method="{1}" />
3、动态访问实现 不使用
Action的数据携带的API:
public String method03(){ HttpServletRequest httpServletRequest = ServletActionContext.getRequest(); HttpServletResponse httpServletResponse = ServletActionContext.getResponse(); ServletContext servletContext = ServletActionContext.getServletContext(); ActionContext actionContext = ServletActionContext.getActionContext(httpServletRequest); ActionMapping actionMapping = ServletActionContext.getActionMapping(); ValueStack valueStack = ServletActionContext.getValueStack(httpServletRequest); PageContext pageContext = ServletActionContext.getPageContext(); return NONE; }
结果页面配置:
场景一,存在多个Action都返回同一个页面位置,方法的返回值一样:
全局返回结果配置:
所谓的全局是受限于Package标签中,多个Action共用的Result标签
<global-results> <result name="method-return-String">dispatcher-Url-Path</result> </global-results>
另外,action标签不需要再配置result标签
<action name="aaa" class="cn.dzz.action.ActionMode02" method="sss" />
但是方法不要搞错:
public String sss() { return "method-return-String"; }
局部结果页面配置:
就是一个Action对应自己的一个Result标签
<action name="insert" class="cn.dzz.action.HelloAction" method="add"> <result name="aaa" >/haha.jsp</result> </action>
如果既配置了全局,又配置了局部,Struts2会以局部配置为准


浙公网安备 33010602011771号