1,先写一个CommonAction类
public class CommonAction extends DispatchAction {
private static ApplicationContext context = null;
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request,
HttpServletResponse response) throws Exception {
// Identify the request parameter containing the method name
String parameter = mapping.getParameter();
if (parameter == null) {
String message = messages.getMessage("dispatch.handler", mapping
.getPath());
log.error(message);
response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
message);
return (null);
}
// Identify the method name to be dispatched to.
// dispatchMethod() will call unspecified() if name is null
String name = request.getParameter(parameter);
// to do : check the authorization...
// Invoke the named method, and return the result
return dispatchMethod(mapping, form, request, response, name);
}
}
该类的主要方法是dispatchMethod负责调用Action类的name方法
2.现在具体写一个类,该类包含save和list方法
public class ListAllLeavesAction extends CommonAction {
public ActionForward list(
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response) throws Exception {
LybanForm listForm = (LybanForm)form;
Session session = HibernateUtil.currentSession();
Criteria c = session.createCriteria(Ly.class);
listForm.setFields(c.list());
return mapping.findForward("list");
}
public ActionForward save(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
LybanForm addLybanForm = (LybanForm) form;
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date now = new Date();
Session session = HibernateUtil.currentSession();
Transaction t = session.beginTransaction();
Ly ly = new Ly();
ly.setMail(addLybanForm.getMail());
ly.setMessages(addLybanForm.getMessages());
ly.setName(addLybanForm.getName());
ly.setTitle(addLybanForm.getTitle());
ly.setTime(sdf.format(now));
session.save(ly);
t.commit();
HibernateUtil.closeSession();
return mapping.findForward("success");
}
}
3 .现在看一下页面是如何调用ListAllLeavesAction类的save方法
<body>
<html:form action="/jsp/lyb/manageLyban" method="post">
<table width="90%" border="1" align="center" bordercolor="#CCCCCC">
<tr>
<input type="hidden" name="method" value="save"/>//该隐藏域的method属性的值就是ListAllLeavesAction类对应的save方法的名字
<td width="100" align="right" class="tdright">
留言标题<span class="red">*</span>: </td>
<td width="185" class="TDTEXT2">
<input name="title" type="text">
</td>
<td width="98" align="right" class="tdright">
<div align="right">留言人:</div>
</td>
<td width="279" class="TDTEXT2"><input name="name" type="text"> </td>
</tr>
<tr>
<td align="right" class="tdright">
<div align="right">
留言内容<span class="red">*</span>: </div> </td>
<td width="185" class="TDTEXT2"><input name="messages" type="text"></td>
<td align="right" class="tdright">
<div align="right">
留言人Email:</div> </td>
<td width="279" class="TDTEXT2"><input name="mail" type="text"></TD>
</tr>
<tr align="center">
<td height="33" colspan="5">
<input type="button" value="确定" onClick="submit();" />
</td>
</tr>
</table>
</html:form>
</body>
</html>
这样就会调用ListAllLeavesAction类里面的save方法
4.Struts配置文件的配置
<struts-config>
<form-beans>
<form-bean name="addLybForm" type="com.dragonsoft.lyb.struts.form.LybanForm" />
</form-beans>
<action-mappings>
<action attribute="addLybForm" parameter="method" input="/jsp/addLyban.jsp" name="addLybForm" path="/jsp/lyb/manageLyban" scope="request" type="com.dragonsoft.lyb.struts.action.ListAllLeavesAction">
<forward name="success" path="/jsp/lyb/manageLyban.do?method=list"/>
<forward name="list" path="/jsp/listAllLeaves.jsp" />
</action>
</action-mappings>
</struts-config>
注意的是parameter的值要跟页面隐藏域method的名字一样
这是一个简单的例子,想必大家也看的懂,我就不需要再罗嗦了。
public class CommonAction extends DispatchAction {
private static ApplicationContext context = null;
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request,
HttpServletResponse response) throws Exception {
// Identify the request parameter containing the method name
String parameter = mapping.getParameter();
if (parameter == null) {
String message = messages.getMessage("dispatch.handler", mapping
.getPath());
log.error(message);
response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
message);
return (null);
}
// Identify the method name to be dispatched to.
// dispatchMethod() will call unspecified() if name is null
String name = request.getParameter(parameter);
// to do : check the authorization...
// Invoke the named method, and return the result
return dispatchMethod(mapping, form, request, response, name);
}
}
该类的主要方法是dispatchMethod负责调用Action类的name方法
2.现在具体写一个类,该类包含save和list方法
public class ListAllLeavesAction extends CommonAction {
public ActionForward list(
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response) throws Exception {
LybanForm listForm = (LybanForm)form;
Session session = HibernateUtil.currentSession();
Criteria c = session.createCriteria(Ly.class);
listForm.setFields(c.list());
return mapping.findForward("list");
}
public ActionForward save(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
LybanForm addLybanForm = (LybanForm) form;
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date now = new Date();
Session session = HibernateUtil.currentSession();
Transaction t = session.beginTransaction();
Ly ly = new Ly();
ly.setMail(addLybanForm.getMail());
ly.setMessages(addLybanForm.getMessages());
ly.setName(addLybanForm.getName());
ly.setTitle(addLybanForm.getTitle());
ly.setTime(sdf.format(now));
session.save(ly);
t.commit();
HibernateUtil.closeSession();
return mapping.findForward("success");
}
}
3 .现在看一下页面是如何调用ListAllLeavesAction类的save方法
<body>
<html:form action="/jsp/lyb/manageLyban" method="post">
<table width="90%" border="1" align="center" bordercolor="#CCCCCC">
<tr>
<input type="hidden" name="method" value="save"/>//该隐藏域的method属性的值就是ListAllLeavesAction类对应的save方法的名字
<td width="100" align="right" class="tdright">
留言标题<span class="red">*</span>: </td>
<td width="185" class="TDTEXT2">
<input name="title" type="text">
</td>
<td width="98" align="right" class="tdright">
<div align="right">留言人:</div>
</td>
<td width="279" class="TDTEXT2"><input name="name" type="text"> </td>
</tr>
<tr>
<td align="right" class="tdright">
<div align="right">
留言内容<span class="red">*</span>: </div> </td>
<td width="185" class="TDTEXT2"><input name="messages" type="text"></td>
<td align="right" class="tdright">
<div align="right">
留言人Email:</div> </td>
<td width="279" class="TDTEXT2"><input name="mail" type="text"></TD>
</tr>
<tr align="center">
<td height="33" colspan="5">
<input type="button" value="确定" onClick="submit();" />
</td>
</tr>
</table>
</html:form>
</body>
</html>
这样就会调用ListAllLeavesAction类里面的save方法
4.Struts配置文件的配置
<struts-config>
<form-beans>
<form-bean name="addLybForm" type="com.dragonsoft.lyb.struts.form.LybanForm" />
</form-beans>
<action-mappings>
<action attribute="addLybForm" parameter="method" input="/jsp/addLyban.jsp" name="addLybForm" path="/jsp/lyb/manageLyban" scope="request" type="com.dragonsoft.lyb.struts.action.ListAllLeavesAction">
<forward name="success" path="/jsp/lyb/manageLyban.do?method=list"/>
<forward name="list" path="/jsp/listAllLeaves.jsp" />
</action>
</action-mappings>
</struts-config>
注意的是parameter的值要跟页面隐藏域method的名字一样
这是一个简单的例子,想必大家也看的懂,我就不需要再罗嗦了。