[基础]项目中Struts的配置

 

DispatchAction用于分发的Action,主要的好处是把一些功能类似的Action放到一个Action中,通过传入的不同参数来觉得执行哪个操作. 

是一个抽象类,它实现了父类(Action)的execute()方法,所以它的子类就不用来实现这个方法了,只需要专注与实际操作的方法


DispatchAction就是在struts-config中用parameter参数配置一个表单字段名,这个字段的值就是最终替代execute被调用的方法. 例如parameter="method"而request.getParameter("method")="save",其中"save"就是MethodName。struts的请求将根据parameter被分发到"save"或者"edit"或者什么。但是有一点,save()或者edit()等方法的声明和execute必须一模一样。 

即:

<action path="/admin/user" name="userForm" scope="request" parameter="method" validate="false">

    <forward name="list" path="/admin/userList.jsp"/>

</action>

    其中parameter="method" 设置了用来指定响应方法名的url参数名为method,即/admin/user.do?method=list 将调用UserAction的public ActionForward list(....) 函数。   

    public ActionForward unspecified(....) 函数可以指定不带method方法时的默认方法

也就是说  当你使用 DispatchAction 定义action的时候,如果没有指定method 方法,那么action  自动匹配到  unspecified  这个函数。

 

 

 

public class UpdateBookingOrderAction extends BaseDispathAction {
	@Override
	public String query(ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws SystemException {
			
	}
	@Override
	public String update(ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws SystemException {
	
	}
}

 

 

public abstract class BaseDispathAction extends DispatchAction {
	private static Log log = LogFactory.getLog(BaseDispathAction.class);

	@Override
	protected ActionForward unspecified(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws SystemException {
		ActionForward actionForward = this.doInit("proccess", mapping, form, request, response);
		return actionForward;
	}

	public ActionForward query(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws SystemException {
		ActionForward actionForward = this.doInit("query", mapping, form, request, response);
		return actionForward;
	}

	public ActionForward add(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws SystemException {
		ActionForward actionForward = this.doInit("add", mapping, form, request, response);
		return actionForward;
	}

	public ActionForward read(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws SystemException {
		ActionForward actionForward = this.doInit("read", mapping, form, request, response);
		return actionForward;
	}

	public ActionForward doInit(String type, ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws SystemException {
		HttpSession session = request.getSession();
		String id = session.getId();
		String remoteAddr = request.getRemoteAddr();
		String clientInfo = remoteAddr + "(" + id + ")";
		String strGotoPage = "success";
		try {
			if (type.equalsIgnoreCase("add")) {
				session.setAttribute("querystatus", "Add");
				strGotoPage = add(form, request, response);
			} else if (type.equalsIgnoreCase("update")) {
				session.setAttribute("querystatus", "Edit");
				strGotoPage = update(form, request, response);
			} else if (type.equalsIgnoreCase("addnew")) {
				session.setAttribute("querystatus", "Add");
				strGotoPage = addnew(form, request, response);
			} else if (type.equalsIgnoreCase("delete")) {
				session.setAttribute("querystatus", "Delete");
				strGotoPage = delete(form, request, response);
			} else if (type.equalsIgnoreCase("query")) {
				session.setAttribute("querystatus", "Query");
				strGotoPage = query(form, request, response);
			} else if (type.equalsIgnoreCase("read")) {
				session.setAttribute("querystatus", "Edit");
				strGotoPage = read(form, request, response);
			} else {// process
				session.setAttribute("querystatus", "Query");
				strGotoPage = process(form, request, response);
			}			
		} catch (Exception e) {
			log.error(e);
			e.printStackTrace();
			throw new SystemException(e.getMessage());
		}
		ActionForward actionForward = mapping.findForward(strGotoPage);
		if (null != actionForward) {
			log.warn(clientInfo + "====> " + actionForward.getName() + ":" + actionForward.getPath());
		}
		return actionForward;
	}

	public abstract String process(ActionForm form, HttpServletRequest request, HttpServletResponse response) throws SystemException;
	public abstract String query(ActionForm form, HttpServletRequest request, HttpServletResponse response) throws SystemException;
	public abstract String add(ActionForm form, HttpServletRequest request, HttpServletResponse response) throws SystemException;
	public abstract String addnew(ActionForm form, HttpServletRequest request, HttpServletResponse response) throws SystemException;
	public abstract String read(ActionForm form, HttpServletRequest request, HttpServletResponse response) throws SystemException;
	public abstract String update(ActionForm form, HttpServletRequest request, HttpServletResponse response) throws SystemException;
	public abstract String delete(ActionForm form, HttpServletRequest request, HttpServletResponse response) throws SystemException;
}
 

JSP调用:

 

document.forms[0].action = "../updateBooking.do?method=updateStatus;
document.forms[0].submit();

 

或者:

public class ZonesAction extends BaseAction {
	
	@Override
	public String process(ActionForm form, HttpServletRequest request) throws Exception {

	}
}

public abstract class BaseAction extends Action {

	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws Exception {
		HttpSession session = request.getSession();
		String id = session.getId();
		String remoteAddr = request.getRemoteAddr();
		String clientInfo = remoteAddr + "(" + id + ")";
		log = LogFactory.getLog(this.getClass());
		String strGotoPage;
		try {
			strGotoPage = process(form,request);
		} catch (Exception e) {
			log.error(e);
			e.printStackTrace();
			throw e;
		}
		ActionForward actionForward = mapping.findForward(strGotoPage);
		log.warn(clientInfo +"====> "+ actionForward.getName() + ":" + actionForward.getPath());
		return actionForward;
	}
	
	public abstract String process(ActionForm form, HttpServletRequest request)throws Exception;
	
}
 

 

DispatchAction, LookupDispatchAction, MappingDispatchAction深入分析

http://qmug.iteye.com/blog/215456

 

 

 

 

 

 

 

 

你可能感兴趣的:(struts)