层层递进Struts1(七)详解DispatchAction

        通过前面几篇博客,不知道大家有没有发现这个问题,虽然现在可以灵活控制跳转了,但是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");
	}
	
}


UserActionForm

        封装表单中的数据,需要注意的是,不用为每个表单 建立一个ActionForm,多个表单可以使用一个进行封装。

 

package com.tgb.drp.web.forms;

import java.util.Date;

import org.apache.struts.action.ActionForm;

/**
 * 用户管理ActionForm
 *
 */
public class UserActionForm extends ActionForm {
	
	//用户代码
	private String userId;
	
	//用户名称
	private String userName;
	
	//密码
	private String password;
	
	//联系电话
	private String contactTel;
	
	//email
	private String email;
	
	//创建日期
	private Date createDate;
	
	//接收界面中的选中标记的集合
	private String[] selectFlag;
	
	public String getContactTel() {
		return contactTel;
	}

	public void setContactTel(String contactTel) {
		this.contactTel = contactTel;
	}

	public Date getCreateDate() {
		return createDate;
	}

	public void setCreateDate(Date createDate) {
		this.createDate = createDate;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getUserId() {
		return userId;
	}

	public void setUserId(String userId) {
		this.userId = userId;
	}

	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public String[] getSelectFlag() {
		return selectFlag;
	}

	public void setSelectFlag(String[] selectFlag) {
		this.selectFlag = selectFlag;
	}
}

 

 

struts-config

 

        配置Action和ActionForm,区别于以前的是需要添加parameter属性。

 

    <form-beans>
		<form-bean name="userForm" type="com.tgb.drp.web.forms.UserActionForm"/>
    </form-beans>
    
    <action-mappings>
    	<action path="/user/user_maint"
    			type="com.tgb.drp.web.actions.UserAction"
    			name="userForm"
    			scope="request"
    			parameter="command"
    	>
    		<forward name="list_success" path="/user/user_list.jsp"/>
    		<forward name="del_success" path="/user/user_maint.do" redirect="true"/>
    		<forward name="add_success" path="/user/user_maint.do" redirect="true"/>
    		<forward name="modify_success" path="/user/user_maint.do" redirect="true"/>
    		<forward name="find_success" path="/user/user_modify.jsp"/>
    		 
    	</action>
		<action path="/user/show_add"
				forward="/user/user_input.jsp"
		></action>

    </action-mappings>

 

访问接口

 

<body>
	<a href="user/user_maint.do" title="请点击访问用户管理系统">用户管理系统</a>
</body>

 

    如上所示,它是根据command属性的值确定的。

执行流程

    把断点主要设置在DispatchAction中,可以看到,执行流程如下:

            

代码分析

    在DispatchAction的execute函数中,有如下代码:

	// Prevent recursive calls
	if ("execute".equals(name) || "perform".equals(name)){
		String message =
			messages.getMessage("dispatch.recursive", mapping.getPath());

		log.error(message);
		throw new ServletException(message);
	}

    所以在给parameter设置值时,不能为execute或perform。

 

    在DispatchAction的dispatchMethod中,有如下代码:

 

        if (name == null) {
            return this.unspecified(mapping, form, request, response);
        }


    即如果parameter值为空,则执行unspecified函数,可以在子类中实现这个函数,作为无参数值的跳转。

 

总结

    总而言之,DispatchAction的出现减少了Action的数量,将多个Action融合到一起,虽然有显而易见的好处,但是同样会有问题,这样的耦合性的提高,必然伴随着扩展性的降低,所以用还是不用,还要根据具体情况而定。

    更多相关博客,请至《层层递进Struts1(八)之总结》

 

posted on 2013-08-02 22:51  you Richer  阅读(332)  评论(0编辑  收藏  举报