Struts2 part 2:Action的编写方式

  

Action的编写方式:

1、POJO对象

public class RequestAction {
	public String execute() {

		return "success";
	}
}

 2、实现Action接口

public class RequestAction2 implements Action {

	@Override
	public String execute() throws Exception {
		return SUCCESS;
	}
}

 接口中定义了5种逻辑视图名称

public static final String SUCCESS = "success";  // 数据处理成功 (成功页面)
public static final String NONE = "none";  // 页面不跳转  return null; 效果一样
public static final String ERROR = "error";  // 数据处理发送错误 (错误页面)
public static final String INPUT = "input"; // 用户输入数据有误,通常用于表单数据校验 (输入页面)
public static final String LOGIN = "login"; // 主要权限认证 (登陆页面)

3、继承ActionSupport类

public class RequestAction3 extends ActionSupport {

	@Override
	public String execute() throws Exception {
		return SUCCESS;
	}
}

 这种方式更为常用,ActionSupport实现了表单校验、错误信息设置、读取国际化信息 三个功能

public class ActionSupport implements Action, Validateable, ValidationAware, TextProvider, LocaleProvider, Serializable {}

 Action中的方法调用

1、通过mehtod属性配置

链接 

	<a href="${pageContext.request.contextPath }/demo03/regist.action">注册</a>

编写Action类,采用继承ActionSupport这种方式

public class RegistAction extends ActionSupport {
	@Override
	public String execute() throws Exception {
		System.out.println("RegistAction--execute()");
		return NONE;
	}

	public String regist() {
		System.out.println("RegistAction--regist()");
		return NONE;
	}
}

 struts配置

<package name="demo03" namespace="/demo03" extends="struts-default">
		<action name="regist" class="demo03.RegistAction"  method="regist">
		</action>
	</package>
 如果不配置method属性,将默认执行execute方法,如果配置就执行regist方法,通过这种方式可以将多个业务方法封装到一个Action中,但配置文件并没有减少,要在struts.xml中配置多个<action>

 2、使用通配符

链接

	<a href="${pageContext.request.contextPath }/demo03/customer_add.action">新增</a>
	<a href="${pageContext.request.contextPath }/demo03/customer_upd.action">修改</a>
	<a href="${pageContext.request.contextPath }/demo03/customer_del.action">删除</a>

 action类

public class CustomerAction extends ActionSupport {
	public String add() throws Exception {
		System.out.println("CustomerAction--add()");
		return NONE;
	}

	public String upd() throws Exception {
		System.out.println("CustomerAction--upd()");
		return NONE;
	}

	public String del() throws Exception {
		System.out.println("CustomerAction--del()");
		return NONE;
	}
}

 配置

<package name="demo03" namespace="/demo03" extends="struts-default">
		<action name="customer_*" class="demo03.CustomerAction" method="{1}">
		</action>
	</package>

 通过customer_*这种方式简化配置,在method中使用{数字}来调用action中的方法,这是action的命名就必须规范

 使用两个通配符

//jsp
<a href="${pageContext.request.contextPath }/demo03/AAAAction_add.action">删除</a>
//action
public class AAAAction extends ActionSupport {
	public String add() {
		return NONE;
	}
}
//struts配置
<action name="*Action_*" class="demo03.{1}Action" method="{2}">
		</action>

 3、动态方法调用

在struts.xml配置常量,开启动态方法调用

<constant name="struts.enable.DynamicMethodInvocation" value="true" />

 页面链接,使用"!方法名称"的方式调用action中方法

	<a href="${pageContext.request.contextPath }/demo03/customer!add.action">新增</a>
	<a href="${pageContext.request.contextPath }/demo03/customer!upd.action">修改</a>
	<a href="${pageContext.request.contextPath }/demo03/customer!del.action">删除</a>

 action类

public class ProductAction extends ActionSupport {
	public String add() throws Exception {
		System.out.println("CustomerAction--add()");
		return NONE;
	}

	public String upd() throws Exception {
		System.out.println("CustomerAction--upd()");
		return NONE;
	}

	public String del() throws Exception {
		System.out.println("CustomerAction--del()");
		return NONE;
	}
}

 配置,不用配置method,也不用通配符,这种方式结合注解,struts可以近乎于零配置

	<action name="product" class="demo03.ProductAction">

猜你喜欢

转载自mvplee.iteye.com/blog/2240224