一 、Action 的编写(3种写法)
1 、Action类是POJO的类
package czm.struts2.testdemo2;
/*** Action编写方式一:Action类是一个POJO(Plain Ordinary Java Object 简单的Java对象)类* @author ASUS**/
public class TestAction2 {public String execute(){System.out.println("TestAction2执行了.....");return null;}
}
2 、Action类实现一个Action接口
package czm.struts2.testdemo2;import com.opensymphony.xwork2.Action;/*** Action编写方式二:实现一个Action接口* * 实现接口的这种方式:提供了五个常量(五个逻辑视图的名称)* * SUCCESS :成功* * ERROR :失败* * LOGIN :登录出错页面跳转* * INPUT :表单校验的时候出错* * NONE :不跳转* @author ASUS**/public class TestAction3 implements Action{@Overridepublic String execute() throws Exception {System.out.println("TestAction3执行了.....");return null;}}
3 、Action类 继承 ActionSupport类 (推荐使用)
package czm.struts2.testdemo2;import com.opensymphony.xwork2.ActionSupport;/*** Action的编写方式三:Action类 继承 ActionSupport类* 推荐使用继承ActionSupport方式* * ActionSupport中提供了数据校验、国际化等一系列操作的方法。* @author ASUS**/
public class TestAction4 extends ActionSupport{@Overridepublic String execute() throws Exception {System.out.println("TestAction4执行了.....");return NONE;}
}
编写xml 配置文件
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN""http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<package name="name" extends="struts-default" namespace="/"> <!-- 此处的name属性值为自定义 --><action name="test2" class="czm.struts2.testdemo2.TestAction2"> </action><action name="test3" class="czm.struts2.testdemo2.TestAction3"> </action><action name="test4" class="czm.struts2.testdemo2.TestAction4"> </action>
</package></struts>
二 、Action 访问的方法(3种方式)
准备工作 :新建一个JSP页面 testdemo02.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>Insert title here</title>
</head>
<body>
<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/><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/><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/>
</body>
</html>
第一种方式 :通过 method 的方式
1 、新建一个 UserAction 类
package czm.struts2.testdemo3;import com.opensymphony.xwork2.ActionSupport;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;}
}
2 、编写xml配置文件
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN""http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<package name="name2" extends="struts-default" namespace="/"> <!-- 此处的name属性值为自定义 --><action name="userFind" class="czm.struts2.testdemo3.UserAction" method="find"> </action><action name="userUpdate" class="czm.struts2.testdemo3.UserAction" method="update"> </action><action name="userDelete" class="czm.struts2.testdemo3.UserAction" method="delete"> </action><action name="userSave" class="czm.struts2.testdemo3.UserAction" method="save"> </action></package></struts>
3 、访问 JSP 页面进行测试
4 、点击链接,查看控制台输出
第二种方式:通过 通配符 的方式进行配置(常用)
上面准备工作 已建立一个JSP页面 testdemo02.jsp
1 、新建一个 ProductAction 类
package czm.struts2.testdemo3;import com.opensymphony.xwork2.ActionSupport;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;}
}
2 、编写xml配置文件
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN""http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<package name="name2" extends="struts-default" namespace="/"> <!-- 此处的name属性值为自定义 --><!-- 通配符的方式 --><action name="product_*" class="czm.struts2.testdemo3.ProductAction" method="{1}"></action></package></struts>
3 、访问 JSP 页面进行测试
4 、点击链接,查看控制台输出
通配符 方法配置 备注说明:
第三种方式:通过 动态方法 访问
上面准备工作 已建立一个JSP页面 testdemo02.jsp
1 、新建一个 CustomerAction 类
package czm.struts2.testdemo3;import com.opensymphony.xwork2.ActionSupport;public class CustomerAction extends ActionSupport {public String find(){System.out.println("查询客户...");return NONE;}public String delete(){System.out.println("删除客户...");return NONE;}public String update(){System.out.println("修改客户...");return NONE;}public String save(){System.out.println("保存客户...");return NONE;}
}
2 、编写xml配置文件
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN""http://struts.apache.org/dtds/struts-2.3.dtd">
<struts><!-- 开启动态方法访问 --><constant name="struts.enable.DynamicMethodInvocation" value="true"/><package name="name2" extends="struts-default" namespace="/"> <!-- 此处的name属性值为自定义 --><!-- 动态方法访问 的方式 --><action name="customer" class="czm.struts2.testdemo3.CustomerAction"></action><!-- name属性值对应 JSP页面链接中 !号前面的名称 --></package></struts>
3 、访问 JSP 页面进行测试
4 、点击链接,查看控制台输出
动态方法访问 备注说明 :
<action>配置中name属性值对应 JSP页面链接中 !号前面的名称