Struts的parameter功能
来源:互联网 发布:影子系统网络客服 编辑:程序博客网 时间:2024/04/29 21:23
struts的parameter功能(转自http://hi.baidu.com/chuntian1919/blog/item/7c12898061a753d39023d974.html)
2008-01-28 10:29 P.M.
没有struts之前,使用servlet,最常用的是doGet,doPost,service方法,如果有些经验的程序员会合理的使用这三个方法:如在用户发出get的请求时,将用户请求在doGet方法中处理,用户发出post请求时,将用户的请求用doPost请求处理,必要时加上service方法去处理那些在一个servlet中必须执行的请求,用户的请求大体也就这三类,但是如果细分,一个“编辑”,“删除”,“查看”等操作都是doGet的范围,当然也可以都写到serice方法中或doPost中处理,这样为了区分这些请求,我们通常都要在程序中加入一个判断的参数,如:operate,然后在程序中判断 if operate.equals("update")....,if operate.equals("del")....,if operate.equals("view")....等,实际上这只是个简单的逻辑,如果业务更加复杂,你可能写更多的类时operate的参数,这样就造成程序中有若干if..else if...else if ..,即便你有非常好的编码规范,整齐的缩进,这个程序也相当难维护;而用到struts时,你又可能把这些参数都写到execute方法中;那么最好的方法还是将这些逻辑分开处理,如果执行“编辑”操作的时候调用“编辑”对应的方法,执行“删除”的时候调用“删除”对应的方法...将是比较理想的结果,为了实现这个应用要求,struts引入DispatchAction,这样你在struts-config.xml文件的action元素中增加parameter属性即可实现这个功能:
例如appfuse的配置:
<action
path="/saveUser"
type="org.appfuse.webapp.action.UserAction"
name="userForm"
scope="request"
input="edit"
parameter="method"
unknown="false"
validate="false"
>
<forward
name="list"
path="/WEB-INF/pages/userList.jsp"
redirect="false"
/>
<forward
name="edit"
path="/WEB-INF/pages/userForm.jsp"
redirect="false"
/>
</action>
parameter="method"这个参数就是说,在用户提交请求时取得method参数,根据method参数调用相应的方法,如/editUser.html?method=Delete就是调用对应action中的Delete方法,这样你就可以写一个Action类处理很多的逻辑,而不是象从前那样在一个方法里面加上若干参数,或者直接建若干个action来处理。
例如appfuse的UserAction
package org.appfuse.webapp.action;
import ...
public final class UserAction extends BaseAction {
public ActionForward add(ActionMapping mapping, ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws Exception {
...
}
public ActionForward cancel(ActionMapping mapping, ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws Exception {
...
}
public ActionForward delete(ActionMapping mapping, ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws Exception {
...
}
public ActionForward edit(ActionMapping mapping, ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws Exception {
...
}
public ActionForward save(ActionMapping mapping, ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws Exception {
...
}
public ActionForward search(ActionMapping mapping, ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws Exception {
...
}
public ActionForward unspecified(ActionMapping mapping, ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws Exception {
...
}
private void sendNewUserEmail(HttpServletRequest request, UserForm userForm)
throws Exception {
...
}
}
当你没有传入method参数,或者没有符合参数的方法时,程序将执行unspecified方法;当然method只是一个逻辑名字而已,你也可以使用其他名字,如:method1,method2,go2,asdad等
例如appfuse的配置:
<action
path="/saveUser"
type="org.appfuse.webapp.action.UserAction"
name="userForm"
scope="request"
input="edit"
parameter="method"
unknown="false"
validate="false"
>
<forward
name="list"
path="/WEB-INF/pages/userList.jsp"
redirect="false"
/>
<forward
name="edit"
path="/WEB-INF/pages/userForm.jsp"
redirect="false"
/>
</action>
parameter="method"这个参数就是说,在用户提交请求时取得method参数,根据method参数调用相应的方法,如/editUser.html?method=Delete就是调用对应action中的Delete方法,这样你就可以写一个Action类处理很多的逻辑,而不是象从前那样在一个方法里面加上若干参数,或者直接建若干个action来处理。
例如appfuse的UserAction
package org.appfuse.webapp.action;
import ...
public final class UserAction extends BaseAction {
public ActionForward add(ActionMapping mapping, ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws Exception {
...
}
public ActionForward cancel(ActionMapping mapping, ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws Exception {
...
}
public ActionForward delete(ActionMapping mapping, ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws Exception {
...
}
public ActionForward edit(ActionMapping mapping, ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws Exception {
...
}
public ActionForward save(ActionMapping mapping, ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws Exception {
...
}
public ActionForward search(ActionMapping mapping, ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws Exception {
...
}
public ActionForward unspecified(ActionMapping mapping, ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws Exception {
...
}
private void sendNewUserEmail(HttpServletRequest request, UserForm userForm)
throws Exception {
...
}
}
当你没有传入method参数,或者没有符合参数的方法时,程序将执行unspecified方法;当然method只是一个逻辑名字而已,你也可以使用其他名字,如:method1,method2,go2,asdad等
- Struts的parameter功能
- struts配置文件action的parameter
- struts的校验功能
- Struts的附加功能
- struts使用bean:parameter标签获取request提交的参数
- struts-config.xml中action元素的parameter属性
- Struts的配置文件struts-config.xml中action元素的parameter属性作用
- myeclipse 打开struts 2 配置文件struts.xml报错问题的解决 undefined actionname parameter
- Struts1的struts-config.xml中action-mappings的parameter 属性详解
- Struts ActionForm的表单验证功能
- Struts文件上传功能的实现
- Struts 2的配置文件include功能
- struts.xml文件提示功能的实现
- struts拦截器的功能详解
- parameter的参数传递功能以及一个很容易被忽略的小问题
- VS 2010 测试功能学习(九) - 数据驱动的手工测试用例(@Parameter)
- struts2在struts.xml中为返回的页面配参数时出现Invalid result location value/parameter
- struts2在struts.xml中为返回的页面配参数时出现Invalid result location value/parameter
- WinCE下钩子应用(一)——WinCE 下实时捕获键盘消息
- Spring管理Dwr应用组件
- perl中use 和 require的区别
- Common Talks!(1)
- Java编程技巧:列表排序
- Struts的parameter功能
- 桌面和服务器系的部署系统
- 转换为PDF的好工具
- Common Talks!(2)
- JNA—JNI终结者
- 迷茫了....
- qtp_oracle数据库连接
- 感慨3
- 在windows xp下编译出ffmpeg.exe[转]