1、Action的编写
(1)Action本身是一个POJO的类
/**
* Action类的编写方式一:Action本身是POJO的类
* @author jt
*
*/
public class StrutsDemo1 {
public String execute() {
System.out.println("StrutsDemo1执行了...");
return null;
}
}
(2)Action实现一个Action的接口
/**
* Action的编写方式二:Action实现一个Action接口
* @author jt
*
*/
public class StrutsDemo2 implements Action{
@Override
/**
* Action的接口中提供了五个常量(逻辑视图名称)
* * SUCCESS :成功.
* * NONE :不跳转
* * ERROR :跳转到错误页面
* * INPUT :表单校验
* * LOGIN :跳转到登录页面
*/
public String execute() throws Exception {
System.out.println("StrutsDemo2执行了...");
return NONE;
}
}
(3)Action继承一个ActionSupport的类(推荐)
/**
* Action的编写的方式三:Action继承一个ActionSupport类
* @author jt
*
*/
public class StrutsDemo3 extends ActionSupport{
@Override
public String execute() throws Exception {
System.out.println("StrutsDemo3执行了...");
return NONE;
}
}
2、Action的访问
现在已经可以访问到Action,但是一次请求对应了一个Action的,需要一个模块的多次请求添加到一个Action中。
(1)通过method进行配置(不推荐):
页面:
<h1>客户管理</h1>
<h3><a href="${ pageContext.request.contextPath }/addCustomer.action">添加客户</a></h3>
<h3><a href="${ pageContext.request.contextPath }/findCustomer.action">查询客户</a></h3>
<h3><a href="${ pageContext.request.contextPath }/updateCustomer.action">修改客户</a></h3>
<h3><a href="${ pageContext.request.contextPath }/deleteCustomer.action">删除客户</a></h3>
编写Action
public class CustomerAction extends ActionSupport{
public String add(){
System.out.println("保存客户...");
return NONE;
}
public String find(){
System.out.println("查询客户...");
return NONE;
}
public String update(){
System.out.println("修改客户...");
return NONE;
}
public String delete(){
System.out.println("删除客户...");
return NONE;
}
}
配置Action
<!-- 配置package包 -->
<package name="demo3" extends="struts-default"namespace="/">
<!-- 配置action -->
<action name="addCustomer" class="com.lwt.struts2.action3.CustomerAction"method="add"/>
<action name="findCustomer" class="com.lwt.struts2.action3.CustomerAction"method="find"/>
<action name="updateCustomer" class="com.lwt.struts2.action3.CustomerAction"method="update"/>
<action name="deleteCustomer" class="com.lwt.struts2.action3.CustomerAction"method="delete"/>
</package>
(2)通配符的方式配置(推荐)
页面
<h1>商品管理</h1>
<h3><a href="${ pageContext.request.contextPath }/product_add.action">添加客户</a></h3>
<h3><a href="${ pageContext.request.contextPath }/product_find.action">查询客户</a></h3>
<h3><a href="${ pageContext.request.contextPath }/product_update.action">修改客户</a></h3>
<h3><a href="${ pageContext.request.contextPath }/product_delete.action">删除客户</a></h3>
编写Action
public class ProductAction extends ActionSupport{
public String add(){
System.out.println("保存商品...");
return NONE;
}
public String find(){
System.out.println("查询商品...");
return NONE;
}
public String update(){
System.out.println("修改商品...");
return NONE;
}
public String delete(){
System.out.println("删除商品...");
return NONE;
}
}
配置Action:
<!-- 通配符的方式的配置 -->
<action name="product_*" class="com.lwt.struts2.action3.ProductAction"method="{1}"/>
(3)动态方法访问:
编写Action
public class OrderAction extends ActionSupport{
public String add(){
System.out.println("保存订单...");
return NONE;
}
public String find(){
System.out.println("查询订单...");
return NONE;
}
public String update(){
System.out.println("修改订单...");
return NONE;
}
public String delete(){
System.out.println("删除订单...");
return NONE;
}
}
配置Action
<!-- 动态方法的访问 -->
<action name="order"class="com.lwt.struts2.action3.OrderAction"/>
开启动态方法访问:
<!-- 动态方法访问的常量 -->
<constant name="struts.enable.DynamicMethodInvocation"value="true"/>
页面:
<h1>订单管理</h1>
<h3><a href="${ pageContext.request.contextPath }/order!add.action">添加订单</a></h3>
<h3><a href="${ pageContext.request.contextPath }/order!find.action">查询订单</a></h3>
<h3><a href="${ pageContext.request.contextPath }/order!update.action">修改订单</a></h3>
<h3><a href="${ pageContext.request.contextPath }/order!delete.action">删除订单</a></h3>