通过前面几篇博客,不知道大家有没有发现这个问题,虽然现在可以灵活控制跳转了,但是Action的数量 还是比较多,如何既能保证跳转灵活,还能减少Action的数量?这就是我们这篇博客所说的DispatchAction, 如其名,可以理解为“分发式Action”,使用它可以避免为每个Action创建一个类。
我们先来看一下 实例。
UserAction
DispatchAction继承的是Action,它的特点就是把以前的多个Action合并为 一个,当多个Action关联较大时,可以像这样放在一起,减少Action类的同时,也降低了维护的难度。
package com.tgb.drp.web.actions; 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.tgb.drp.manager.UserManager; import com.tgb.drp.model.User; import com.tgb.drp.web.forms.UserActionForm; public class UserAction extends DispatchAction { @Override protected ActionForward unspecified(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { //调用业务逻辑操作 List userList = UserManager.getInstance().findAllUserList(); request.setAttribute("userlist", userList); return mapping.findForward("list_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("del_success"); } /** * 用户添加 * @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(user, uaf); user.setCreateDate(new Date()); //调用业务逻辑操作 UserManager.getInstance().addUser(user); return mapping.findForward("add_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); //调用业务逻辑操作 UserManager.getInstance().modifyUser(user); return mapping.findForward("modify_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"); } }
以上是小编为您精心准备的的内容,在的博客、问答、公众号、人物、课程等栏目也有的相关内容,欢迎继续使用右上角搜索按钮进行搜索exception
, struts1
, request
, response
, import
, form action
, dispatchaction
, mapping
, param
, com exception
Request.Param
初中层层递进语文答案、层层递进、层层递进初中历史答案、形容层层递进的成语、层层递进 英文,以便于您获取更多的相关知识。