如果一个表单有多个提交,比如加、减、乘、除,那么解决方案有两个(我知道的),第一呢是继承DispatchAction,第二个就是继承LookupDispatchAction。这里我们只说后者
1、需要有一个属性文件,这里我们假设为message.properties
2、在message.properties中加入如下代码。等号前为我们起的中间变量,等号后为jsp页面的value值
button.add=ADD
button.sub=SUB
button.mul=MUL
button.div=DIV
3、jsp页面代码如下:
<input type="submit" name="method" value="ADD"/>
<input type="submit" name="method" value="SUB"/>
<input type="submit" name="method" value="MUL"/>
<input type="submit" name="method" value="DIV"/>
4、在struts-config.xml 中需要配置parameter
<action path="" type="" parameter="method" name=""/>
5、最后我们实现Action中未实现的方法
protected Map getKeyMethodMap() {
Map map = new HashMap();
map.put("button.add", "add");
map.put("button.sub", "sub");
map.put("button.mul", "mul");
map.put("button.div", "div");
return map;
}
put方法的第一个参数为我们起的中间变量,第二个参数即为我们要访问的方法
6、执行顺序,以除为例:jsp(value="DIV")----message.properties(button.div)----**Action(div方法)