引用
在采用标准的Action时每个单独的操作都要写一个Action的类,这样会使我们的Action类膨胀起来,为了解决这个问题,可以使用Struts给我们提供的DispatchAction。
DispatchAction的用法:
1.首先我们要先建一个Action类,并继承Struts的DispatchAction
package com.yx.zzg.action;
import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;
import com.yx.zzg.form.UserActionForm;
import com.yx.zzg.manager.UserManager;
import com.yx.zzg.model.User;
/**
* 用户CRUD
*
* @author Administrator
*
*/
public class UserAction extends DispatchAction {
/**
* 覆写Struts的unspecified方法,当传递的参数值不存在时,会执行到此方法
*/
@Override
protected ActionForward unspecified(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
System.out.println("-----------UserAction.unspecified()-----------");
// 调用业务逻辑操作
List userList = UserManager.getInstance().findAllUserList();
request.setAttribute("userlist", userList);
return mapping.findForward("list_success");
// ActionForward af = new ActionForward();
// af.setPath("/user/usermaint.do?command=list");
// af.setRedirect(true);
// return af;
}
/**
* 用户添加
*
* @param mapping
* @param form
* @param request
* @param response
* @return
* @throws Exception
*/
public ActionForward add(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
// 获取从页面表单中提交过来的值
UserActionForm uaf = (UserActionForm) form;
User user = new User();
//此方法是用来属性的拷贝(常用)
//用法BeanUtils.copyProperties(目标对象, 源对象);
BeanUtils.copyProperties(user, uaf);
user.setCreateDate(new Date());
// 调用业务逻辑操作
UserManager.getInstance().addUser(user);
return mapping.findForward("success");
}
/**
* 用户删除
*
* @param mapping
* @param form
* @param request
* @param response
* @return
* @throws Exception
*/
public ActionForward del(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
// 获取从页面表单中提交过来的值
UserActionForm uaf = (UserActionForm) form;
// 取得需要删除的userId的集合
String[] userIdList = uaf.getSelectFlag();
// 调用业务逻辑操作
UserManager.getInstance().deleteUsers(userIdList);
return mapping.findForward("success");
}
/**
* 根据ID查询用户
*
* @param mapping
* @param form
* @param request
* @param response
* @return
* @throws Exception
*/
public ActionForward find(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
// 获取从页面表单中提交过来的值
UserActionForm uaf = (UserActionForm) form;
String userId = uaf.getUserId();
// 调用业务逻辑操作
User user = UserManager.getInstance().findUserById(userId);
// 将user对象从Action传递到JSP页面
request.setAttribute("user", user);
return mapping.findForward("find_success");
}
/**
* 修改用户
*
* @param mapping
* @param form
* @param request
* @param response
* @return
* @throws Exception
*/
public ActionForward modify(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
// 获取从页面表单中提交过来的值
UserActionForm uaf = (UserActionForm) form;
User user = new User();
BeanUtils.copyProperties(user, uaf);
user.setCreateDate(new Date());
// 调用业务逻辑操作
UserManager.getInstance().modifyUser(user);
return mapping.findForward("success");
}
}
2.配置struts-config.xml文件
<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE struts-config PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 1.3//EN"
"http://struts.apache.org/dtds/struts-config_1_3.dtd">
<struts-config>
<!-- 定义ActionForm -->
<form-beans>
<form-bean name="userForm" type="com.yx.zzg.form.UserActionForm"></form-bean>
</form-beans>
<!-- 定于一个全局的转向 -->
<global-forwards>
<forward name="success" path="/user/usermaint.do" redirect="true" />
</global-forwards>
<!-- 定义Action -->
<action-mappings>
<!-- parameter定义参数名称为command,参数名称的值跟的是方法名称 -->
<action path="/user/usermaint" type="com.yx.zzg.action.UserAction"
name="userForm" scope="request" parameter="command">
<forward name="find_success" path="/user/user_modify.jsp" />
<forward name="list_success" path="/user/user_list.jsp" />
</action>
</action-mappings>
</struts-config>
注意:使用DispatchAction必须注意以下几个方面
引用
1.如果在Action中覆写execute方法,必须显示的用super调用父类的execute方法,否则DispatchAction就会失效。
2.在配置Action时parameter的属性值不能为execute或perform,否则会抛出异常,参见struts源码。
3.如果我们在访问一个parameter为null或不存在的parameter时,就会抛出异常,我们可以在Action里覆写struts的unspecified方法,并在这个方法里做处理。