模块的实现写的差不多了,可是 每个页面的活动都要一个Action,3个对象的增删改查 就有14个Action。终于今天组长发飙了,要改成DispatchAction。现上网查查怎么用。。找到个本博客中的文章放入收藏了。
http://www.blogjava.net/jxhkwhy/archive/2007/01/21/95177.html
把增删改查都放到一个Action中,然后在XML中为每个方法配一个
< action
attribute = "addUserForm"
input = "/addUser.jsp"
name = "addUserForm"
parameter="method"
path = "/addUser"
scope = "request"
type="com.why.struts.action.UserAction" >
</ action >
其实如果使用的Form(name=“xxxForm”)是一样的话,几个方法配一个<action>也行。用多个的时候,注意path不要写成一样的了。
注意parameter的值,作为JSP跳转的do的参数就可以了。基本上配置方面 和一般<action>区别就是含有同一个type地址和parameter
<html:link href="addUser.do?method=addUser">Add User</html:link>
参数值为Action里的方法名,如这个 对应为 Action中的
addUser (ActionMapping mapping,ActionForm form, HttpServletRequest request,HttpServletResponse response)
execute就不用在写了。虽然不知道写了会怎么样。
下面是我自己的struts-config.xml配置:
实现offer的增,删,改,查。其中查和删的配置写在了一起。所以是 3个Action,注意其 path,type ,和parameter属性。调用时用parameter的值区分。这里还可以看到定向到offerSearch的URL的使用。
这里的name虽然相同,但是分开的原因是 我需要input这个属性作为异常的转回页面。查询和删除都不用name 而且异常提示页面相同,所以合并。
<action
input="/service/offerSearch.jsp"
parameter="operation"
path="/offer"
scope="request"
type="com.neusoft.struts.service.action.OfferAction">
<forward name="success" path="/offer.do?operation=offerSearch&offername="/>
<forward name="show" path="/service/offerSearch.jsp" />
</action>
<action
attribute="OfferForm"
input="/service/offerNew.jsp"
parameter="operation"
name="OfferForm"
path="/offerAdd"
scope="request"
type="com.neusoft.struts.service.action.OfferAction">
<forward name="success" path="/offer.do?operation=offerSearch&offername="/>
</action>
<action
attribute="OfferForm"
name="OfferForm"
parameter="operation"
path="/offerUpdate"
input="/service/offerUpdate.jsp"
scope="request"
type ="com.neusoft.struts.service.action.OfferAction" >
<forward name="success" path="/offer.do?operation=offerSearch&offername="/>
</action>
对应的DispatchAction :
public class OfferAction extends DispatchAction {
public ActionForward offerSearch(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) throws Exception {
String offername = changeStr(request.getParameter("offername"));
request.setAttribute("offername", offername);
HttpSession session = request.getSession();
IPageList pageList;
if(request.getParameter("page")==null){ //来自查询表单的请求
pageList = new PageList(1,OfferManager.getInstance().getOfferList(offername));
session.setAttribute("list", pageList.getResultList());
request.setAttribute("pageList", pageList);
}
else{ //来自分页链接的请求
int page = Integer.parseInt(request.getParameter("page"));
pageList = new PageList(page,OfferManager.getInstance().getOfferList(offername));
session.setAttribute("list", pageList.getResultList());
request.setAttribute("pageList", pageList);
}
return mapping.findForward("show");
}
public ActionForward offerAdd(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) throws Exception {
OfferForm offer = (OfferForm) form;
OfferManager.getInstance().addOffer(offer);
return mapping.findForward("success");
}
public ActionForward offerUpdate(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) throws Exception {
OfferForm offer = (OfferForm) form;
OfferManager.getInstance().offerUpdate(offer);
return mapping.findForward("success");
}
public ActionForward offerDelete(ActionMapping mapping,ActionForm Form,
HttpServletRequest request,HttpServletResponse response)throws Exception{
String id = request.getParameter("offerId");
if(id!=null){
OfferManager.getInstance().offerDelete(Integer.parseInt(id));
}
return mapping.findForward("success");
}
}