当前页面: 开发资料首页 → Java 专题 → Struts中DispatchAction类的使用
Struts中DispatchAction类的使用
摘要: Struts中DispatchAction类的使用
</td>
</tr>
<tr>
<td height="35" valign="top" class="ArticleTeitle">
<table width="676" border="0">
<tr>
<td width="396"> 今天刚刚看了DispatchAction觉得这个东西有点意思,所以就写点东西,通过它的名字我想应该可以明白它的作用了,用于分发的Action,主要的好处是把一些功能类似的Action放到一个Action中,通过传入的不同参数来觉得执行哪个操作.
DispatchAction类是一个抽象类,它实现了父类(Action)的execute()方法,所以它的子类就不用来实现这个方法了,只需要专注与实际操作的方法,
1.首先要一个DispatchAction的子类,它含有一些方法,login,logout,method1,
package examples;
package examples;
import javax.servlet.http.*;
import org.apache.struts.action.*;
import org.apache.struts.actions.*; </td>
<td width="270" align="center">
</td>
</tr>
</table>
public class AccountAction extends DispatchAction {
public ActionForward login(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws Exception {
// 進行一些Login的邏輯
return mapping.findForward("success");
}
public ActionForward logout(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws Exception {
// 進行一些Logout的邏輯
return mapping.findForward("success1");
}
public ActionForward method1(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws Exception {
// 進行一些method1的邏輯
return mapping.findForward("success");
}
}
一定要注意在DispatchAction中你想执行的操作,都必须要有统一的参数(ActionMapping mapping, ActionForm form,HttpServletRequest request, HttpServletResponse response),是一个规定.
2.接下来是配置struts-config.xml
name="logonForm"
scope="request"
parameter="action"
input="/pages/dispatch1.jsp">
name="logonForm"
scope="request"
parameter="action"
input="/pages/dispatch1.jsp">
这里需要注意的就是parameter属性的值,因为这个值要和页面传来的参数对应.
3.再来看看JSP页 pages/dispatch1.jsp
<%@ taglib uri="/tags/struts-html" prefix="html" %>
login
logout
这里要注意几点,首先 ?后面的KEY要和struts-config.xml中的parameter相同,还有它的VALUE要是你在action的一个方法名字,这里方法名为login, 那么在程序运行时就是调用login的操作,如果是logout,那程序就调用logout的操作.
function TempSave(ElementID)
{
CommentsPersistDiv.setAttribute("CommentContent",document.getElementById(ElementID).value);
CommentsPersistDiv.save("CommentXMLStore");
}
function Restore(ElementID)
{
CommentsPersistDiv.load("CommentXMLStore");
document.getElementById(ElementID).value=CommentsPersistDiv.getAttribute("CommentContent");
}
</td>
</tr>
<tr>
↑返回目录
前一篇: 你的第一个struts应用程序
后一篇: struts中使用tiles组件