struts1的基本知识点—DispachAction
dispachaction是动态的指定action。也就是说在view层的不同的请求和url参数被actionservlet所截获,
并在struts-config.xml中根据请求得到参数不同来指定(调用)action中不同的方法,根据方法的返回值来跳转相应的页面。
这样可以很好的解决action膨胀的问题。以前我们继承struts中的action只处理一种请求,也就是说不同的请求会有相应的action类,这样action类就会越来越多,就会照成action膨胀。用dispachaction会根据请求的不同来指定调用哪个方法,这样可以有效的解决action膨胀的问题。
如何使用dispachaction:
1.写一个useraction继承dispachaction
写一个adduser方法添加用户
[java]
//添加user
public actionforward adduser(actionmapping mapping, actionform arg1,
httpservletrequest arg2, httpservletresponse arg3) throws exception {
new userservice().saveuser(new user());
return mapping.findforward("ok");
}
//添加user
public actionforward adduser(actionmapping mapping, actionform arg1,
httpservletrequest arg2, httpservletresponse arg3) throws exception {
new userservice().saveuser(new user());
return mapping.findforward("ok");
}
写一个deleteuser方法删除用户
[java]
//删除user
public actionforward deleteuser(actionmapping mapping, actionform arg1,
httpservletrequest arg2, httpservletresponse arg3) throws exception {
return mapping.findforward("delok");
}
//删除user
public actionforward deleteuser(actionmapping mapping, actionform arg1,
httpservletrequest arg2, httpservletresponse arg3) throws exception {
return mapping.findforward("delok");
}
2.在页面上
[html]
<span style="font-size: 18px"><form action="user.do?command=adduser" method="post">
username:<input type="text" name="username"><br>
password:<input type="password" name="password">
<input type="submit" value="提交">
</form>
<a href="user.do?id=1&command=deleteuser">删除id为1的用户</a></span>
<form action="user.do?command=adduser" method="post">
username:<input type="text" name="username"><br>
password:<input type="password" name="password">
<input type="submit" value="提交">
</form>
<a href="user.do?id=1&command=deleteuser">删除id为1的用户</a>
3.在struts-config.xml配置
[html]
action-mappings>
<action path="/user"
parameter="command"
type="com.jxau.action.useraction">
<forward name="ok" path="/add_ok."></forward>
<forward name="delok" path="/del_ok.jsp"></forward>
</action>
</action-mappings>
<action-mappings>
<action path="/user"
parameter="command"
type="com.jxau.action.useraction">
<forward name="ok" path="/add_ok.jsp"></forward>
<forward name="delok" path="/del_ok.jsp"></forward>
</action>
</action-mappings>
总结:用dispatchaction可以动态的控制action,根据一个模板一个action的原则来管理一个业务逻辑。有效的防止action膨胀的问题
下一篇: asp学习日记