高级Action

 1、DispatchAction

当一个action要处理两个操作时,如以下情况

<html:form action="/buy.do?method=add" method="post">
   请您选择书本 : <html:text property="book"/><html:errors property="book"/><br/>
   <html:submit value="添加到购物车"/>
  </html:form>
  <hr>
  以下是您选的熟本<br>
  <logic:present name="cart">
   <logic:iterate id="book" name="cart">
   <bean:write name="book"/><html:link action="/buy.do?method=delete" paramId="BOOK" paramName="book">删除</html:link><br>
   </logic:iterate>
  </logic:present>

我们都是用buy这个action来处理的,action继承了DispatchAction,为了让页面识别action中的add()方法和delete()方法,我们需要在struts.xml中配置<action  parameter="method"></action>,这种方法耦合性高。需要页面识别action中的方法。

2、MappingDispatchAction

这种方法可以降低耦合性,指向多个结构不同的ActionForm,以上jsp可以如下

<html:form action="/add.do" method="post">
请您选择书本 : <html:text property="book"/><html:errors property="book"/><br/><html:submit value="添加到购物车"/>
</html:form>
<hr>
以下是您选的熟本<br>
<logic:present name="cart">
<logic:iterate id="book" name="cart">
<bean:write name="book"/><html:link action="/delete.do" paramId="BOOK" paramName="book">删除</html:link><br>
</logic:iterate>
</logic:present>

在struts.xml中设置如下<action path="/add" parameter="add"></action>

<action path="delete" parameter="delete"></action>

3、LookupDispatchAction

这种情况一般是同一个表单可以提交到多个不同的按钮,这种方法耦合性低,比较麻烦,需要用到资源文件。举例:jsp如下

<html:form action="/lr">
   account : <html:text property="account"/><br/>
   password : <html:text property="password"/><br/>
   <html:submit property="ope"><bean:message key="info.tag.login"/></html:submit>
   <html:submit property="ope"><bean:message key="info.tag.reg"/></html:submit>
 </html:form>

我们在action中需要重写getKeyMethodMap()

protected Map getKeyMethodMap() {//用一个map保存资源文件和方法名的映射
  // TODO Auto-generated method stub
  Map map = new HashMap();
  map.put("info.tag.login","login");
  map.put("info.tag.reg","reg");
  return map;
 }

login  reg用来选择方法。在struts.xml中需要作如下配置

<action parameter="ope"></action>

原文地址:https://www.cnblogs.com/wyhong/p/2393473.html