今天使用LookupDispatchAction时,由于编写的类继承LookupDispatchAction,同时也覆盖其execute()方法,结果导致自己编写的逻辑函数不起作用,把覆盖execute()方法的代码去掉即可实现同名按钮提交分别处理的效果,下面是代码:
<%@ page contentType="text/html; charset=GBK" %>
<%@ include file="taglib.jsp" %>
<html>
<head>
<title>
jsp7
</title>
</head>
<body bgcolor="#ffffff">
<html:form action="/processCheckoutAction.do">
<html:text property="abc">
</html:text>
<html:submit property="action"><bean:message
key="welcome.user"/></html:submit>
<html:submit property="action"><bean:message
key="goodbye.user"/></html:submit>
</html:form>
</body>
</html>

public class ProcessCheckoutAction extends LookupDispatchAction ...{

protected Map getKeyMethodMap()...{
Map map=new HashMap();
map.put("welcome.user","saveorder");
map.put("goodbye.user","checkout");
return map;
}
public ActionForward saveorder(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,

HttpServletResponse response)...{
System.out.println("saveorder");
return (mapping.findForward("success1"));
}
public ActionForward checkout(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,

HttpServletResponse response)...{
System.out.println("checkout");
return (mapping.findForward("success2"));
}
}
<action name="untitled2ActionForm" parameter="action"
path="/processCheckoutAction" scope="request"
type="test.ProcessCheckoutAction" validate="true">
<forward name="success1" path="/jsp5.jsp" />
<forward name="success2" path="/jsp6.jsp" />
</action>
JSP文件
<%@ page contentType="text/html; charset=GBK" %>
<%@ include file="taglib.jsp" %>
<html>
<head>
<title>
jsp7
</title>
</head>
<body bgcolor="#ffffff">
<html:form action="/processCheckoutAction.do">
<html:text property="abc">
</html:text>
<html:submit property="action"><bean:message
key="welcome.user"/></html:submit>
<html:submit property="action"><bean:message
key="goodbye.user"/></html:submit>
</html:form>
</body>
</html>

扩展的LookupDispatchAction

public class ProcessCheckoutAction extends LookupDispatchAction ...{

protected Map getKeyMethodMap()...{
Map map=new HashMap();
map.put("welcome.user","saveorder");
map.put("goodbye.user","checkout");
return map;
}
public ActionForward saveorder(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)...{
System.out.println("saveorder");
return (mapping.findForward("success1"));
}
public ActionForward checkout(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)...{
System.out.println("checkout");
return (mapping.findForward("success2"));
}
}
Struts-config.xml相应的配置
<action name="untitled2ActionForm" parameter="action"
path="/processCheckoutAction" scope="request"
type="test.ProcessCheckoutAction" validate="true">
<forward name="success1" path="/jsp5.jsp" />
<forward name="success2" path="/jsp6.jsp" />
</action>
本文介绍如何使用 Struts2 中的 LookupDispatchAction 实现不同按钮触发不同方法的功能。通过具体示例展示了 JSP 页面设置及 Action 类编写方法,并配置了 Struts-config.xml 文件。
169

被折叠的 条评论
为什么被折叠?



