Struts2中Action的编写&访问

1、Action的写法

1.1 Action类是POJO类

/**
 * Action的编写方式:Action类是一个POJO的类
 * POJO(Plain Ordinary Java Object)简单的Java对象,实际就是普通JavaBeans
 * @author xu
 *
 */
public class ActionDemo1 {

	public String execute(){
		System.out.println("ActionDemo1执行了...");
		return null;
	}
}

1.2 Action类实现一个Action的接口

/**
 * Action的编写方式二:实现一个Action的接口
 * * 实现接口的这种方式:提供了五个常量(五个逻辑视图的名称)
 * 		* SUCCESS	:成功
 * 		* ERROR		:失败
 * 		* LOGIN		:登录出错页面跳转
 * 		* INPUT		:表单校验的时候出错
 * 		* NONE		:不跳转
 * @author xu
 *
 */
public class ActionDemo2 implements Action{

	@Override
	public String execute() throws Exception {
		System.out.println("ActionDemo2执行了...");
		return NONE;
	}

}

1.3 Action类继承ActionSupport类

/**
 * Action的编写方式三:Action类继承ActionSupport类
 * * 推荐使用继承ActionSupport方式
 * 		* ActionSupport中提供了数据校验、国际化等一系列操作的方法。
 * @author xu
 *
 */
public class ActionDemo3 extends ActionSupport{

	@Override
	public String execute() throws Exception {
		System.out.println("ActionDemo3执行了...");
		return NONE;
	}
}

2、Action的访问

2.1 通过method设置

页面编写

<h1>Action的访问</h1>
<h3>通过method方式</h3>
<a href="${ pageContext.request.contextPath }/userFind.action">查询用户</a><br/>
<a href="${ pageContext.request.contextPath }/userUpdate.action">修改用户</a><br/>
<a href="${ pageContext.request.contextPath }/userDelete.action">删除用户</a><br/>
<a href="${ pageContext.request.contextPath }/userSave.action">保存用户</a><br/>

JAVA代码编写

public class UserAction extends ActionSupport{

	public String find(){
		System.out.println("查询用户...");
		return NONE;
	}
	public String update(){
		System.out.println("修改用户...");
		return NONE;
	}
	public String delete(){
		System.out.println("删除用户...");
		return NONE;
	}
	public String save(){
		System.out.println("保存用户...");
		return NONE;
	}
}

Struts.xml的配置

<package name="demo3" extends="struts-default" namespace="/">
    <action name="useFind" class="com.ithema.struts.demo3.UserAction" method="find"></action>
    <action name="useUpdate" class="com.ithema.struts.demo3.UserAction" method="update"></action>
    <action name="useDelete" class="com.ithema.struts.demo3.UserAction" method="delete"></action>
</package>

2.2 通过通配符的方式进行配置

页面编写

<h3>通过通配符的方式</h3>
<a href="${ pageContext.request.contextPath }/product_find.action">查询商品</a><br/>
<a href="${ pageContext.request.contextPath }/product_update.action">修改商品</a><br/>
<a href="${ pageContext.request.contextPath }/product_delete.action">删除商品</a><br/>
<a href="${ pageContext.request.contextPath }/product_save.action">保存商品</a><br/>

JAVA代码编写

public class ProductAction extends ActionSupport {

	public String find(){
		System.out.println("查询商品...");
		return NONE;
	}
	public String update(){
		System.out.println("修改商品...");
		return NONE;
	}
	public String delete(){
		System.out.println("删除商品...");
		return NONE;
	}
	public String save(){
		System.out.println("保存商品...");
		return NONE;
	}
}

Struts.xml的配置

<!-- 通配符的方式 -->
<action name="product_*" class="com.itheima.struts.demo3.ProductAction" method="{1}"></action>

2.3 动态方法访问

页面编写

<h3>通过动态方法访问的方式</h3>
<a href="${ pageContext.request.contextPath }/customer!find.action">查询客户</a><br/>
<a href="${ pageContext.request.contextPath }/customer!update.action">修改客户</a><br/>
<a href="${ pageContext.request.contextPath }/customer!delete.action">删除客户</a><br/>
<a href="${ pageContext.request.contextPath }/customer!save.action">保存客户</a><br/>

JAVA代码编写

public class UserAction extends ActionSupport{

	public String find(){
		System.out.println("查询用户...");
		return NONE;
	}
	public String update(){
		System.out.println("修改用户...");
		return NONE;
	}
	public String delete(){
		System.out.println("删除用户...");
		return NONE;
	}
	public String save(){
		System.out.println("保存用户...");
		return NONE;
	}
}

Struts.xml的配置

<!-- 动态访问在Struts2中一般是被关闭的,想要使用动态访问,需要开启-->
<!-- 开启动态方法访问 -->
<constant name="struts.enable.DynamicMethodInvocation" value="true"/>
<!-- 动态方法访问的方式 -->
<action name="customer" class="com.itheima.struts.demo3.CustomerAction"></action>

猜你喜欢

转载自blog.csdn.net/qq_43642812/article/details/84377403