struts1.1源码研究--表单数据的填充方式
2008-01-26 16:14
417 查看
在struts(version 1.1)中,存在下面描述的一种情况:
有2个Action,UserLoginAction和LoginDetailAction 和一个MyLogin.jsp页面。
程序的流程是这样的:
请求一个MyLogin.jsp,在name输入框中输入:gordian。点击按钮提交,流程跳转到了UserLoginAction,在UserLoginAction中,
把form的name属性的值改为了'gct',并跳转到了LoginDetailAction,这时,我们发现,form中name属性的值,仍然是gordian,而不是在UserLoginAction
中修改的'gct’,就是说,在UserLoginAction中对form的操作失效了。
注:UserLoginAction和LoginDetailAction都指向同一个form
经过对Struts的源码进行跟踪发现,Struts的RequestProcessor类中的方法processPopulate()对form进行了一系列关键的操作.
对于每一次请求,struts引擎复负责生成form实例,并根据配置文件设置的scope,在request或是在session(等)保存form的实例。
当一个请求到来的时候,引擎首先根据Action,查找form的名字,再根据名字在request或者session中查找form的实例。如果,存在则直接返回。
如果不存在,则生成新的实例。
获取实例之后,引擎调用了RequestProcessor类中的方法processPopulate()。该方法,重置了form属性的值: form.reset(mapping, request);
并调用了 RequestUtils.populate(form, mapping.getPrefix(), mapping.getSuffix(),request);方法,从request中重新获取值对,
重新设置form的值。因此,form的值,又与request中的一致了。所以,在UserLoginAction对form的任何操作都是无效的。
照此推论,即使UserLoginAction和LoginDetailAction指向不同的form,但两个form有相同的属性,在UserLoginAction对相同的属性的值进行了修改,
那么,在LoginDetailAction中获取到的该属性的值还是不变的。
代码清单1:
public class UserLoginAction extends ComtopAction{
public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request,
HttpServletResponse response) throws Exception {
logger.debug("UserLoginAction");
UserForm userForm=(UserForm) form;
logger.debug(userForm);
logger.debug("在UserLoginAction userForm.hashCode "+userForm.hashCode());
userForm.setActionType("detail");
logger.debug("在UserLoginAction 设置actionType 的值");
userForm.setName("gct");
return mapping.findForward("LoginDetailAction");
}
}
代码清单2:
public class LoginDetailAction extends ComtopAction{
public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request,
HttpServletResponse response) throws Exception {
UserForm userForm=(UserForm) form;
logger.debug("LoginDetailAction");
logger.debug(userForm);
System.out.println(userForm);
logger.debug("在LoginDetailAction userForm.hashCode "+userForm.hashCode());
return mapping.findForward("LoginDetail");
}
}
代码清单3:
<html>
<script language="javascript" src="/web/system/js/setday.js"></script>
<title>just for test</title>
<script language="javascript">
<!--
//选择部门
function submitData(form){
form.loginDate.value='2007-2-11';
// form.action=form.action+'?actionType=list';
alert(form.action);
form.submit();
}
//-->
</script>
<body>
<form action="/web/product/patrol/userLoginAction.do" name="userForm" />
name:<input class=flat_u size=30 name="name" />
<input type="hidden" name="actionType" value="list" />
<input class=flat_u maxlength=20 size=30 name="loginDate" readonly>
<img style="CURSOR: hand" src="/web/images/show-calendar.gif" onClick="openCalendar('/web',userForm.loginDate)" >
<input name="button" type=button class=btn_href value= 保 存 onclick="submitData(this.form)" >
</form>
</body>
</html>
/**
* Populate the properties of the specified ActionForm instance from
* the request parameters included with this request. In addition,
* request attribute <code>Globals.CANCEL_KEY</code> will be set if
* the request was submitted with a button created by
* <code>CancelTag</code>.
*
* @param request The servlet request we are processing
* @param response The servlet response we are creating
* @param form The ActionForm instance we are populating
* @param mapping The ActionMapping we are using
*
* @exception ServletException if thrown by RequestUtils.populate()
*/
protected void processPopulate(HttpServletRequest request,
HttpServletResponse response,
ActionForm form,
ActionMapping mapping)
throws ServletException {
if (form == null) {
return;
}
// Populate the bean properties of this ActionForm instance
if (log.isDebugEnabled()) {
log.debug(" Populating bean properties from this request");
}
form.setServlet(this.servlet);
form.reset(mapping, request);
if (mapping.getMultipartClass() != null) {
request.setAttribute(Globals.MULTIPART_KEY,
mapping.getMultipartClass());
}
RequestUtils.populate(form, mapping.getPrefix(), mapping.getSuffix(),
request);
// Set the cancellation request attribute if appropriate
if ((request.getParameter(Constants.CANCEL_PROPERTY) != null) ||
(request.getParameter(Constants.CANCEL_PROPERTY_X) != null)) {
request.setAttribute(Globals.CANCEL_KEY, Boolean.TRUE);
}
}
有2个Action,UserLoginAction和LoginDetailAction 和一个MyLogin.jsp页面。
程序的流程是这样的:
请求一个MyLogin.jsp,在name输入框中输入:gordian。点击按钮提交,流程跳转到了UserLoginAction,在UserLoginAction中,
把form的name属性的值改为了'gct',并跳转到了LoginDetailAction,这时,我们发现,form中name属性的值,仍然是gordian,而不是在UserLoginAction
中修改的'gct’,就是说,在UserLoginAction中对form的操作失效了。
注:UserLoginAction和LoginDetailAction都指向同一个form
经过对Struts的源码进行跟踪发现,Struts的RequestProcessor类中的方法processPopulate()对form进行了一系列关键的操作.
对于每一次请求,struts引擎复负责生成form实例,并根据配置文件设置的scope,在request或是在session(等)保存form的实例。
当一个请求到来的时候,引擎首先根据Action,查找form的名字,再根据名字在request或者session中查找form的实例。如果,存在则直接返回。
如果不存在,则生成新的实例。
获取实例之后,引擎调用了RequestProcessor类中的方法processPopulate()。该方法,重置了form属性的值: form.reset(mapping, request);
并调用了 RequestUtils.populate(form, mapping.getPrefix(), mapping.getSuffix(),request);方法,从request中重新获取值对,
重新设置form的值。因此,form的值,又与request中的一致了。所以,在UserLoginAction对form的任何操作都是无效的。
照此推论,即使UserLoginAction和LoginDetailAction指向不同的form,但两个form有相同的属性,在UserLoginAction对相同的属性的值进行了修改,
那么,在LoginDetailAction中获取到的该属性的值还是不变的。
代码清单1:
public class UserLoginAction extends ComtopAction{
public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request,
HttpServletResponse response) throws Exception {
logger.debug("UserLoginAction");
UserForm userForm=(UserForm) form;
logger.debug(userForm);
logger.debug("在UserLoginAction userForm.hashCode "+userForm.hashCode());
userForm.setActionType("detail");
logger.debug("在UserLoginAction 设置actionType 的值");
userForm.setName("gct");
return mapping.findForward("LoginDetailAction");
}
}
代码清单2:
public class LoginDetailAction extends ComtopAction{
public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request,
HttpServletResponse response) throws Exception {
UserForm userForm=(UserForm) form;
logger.debug("LoginDetailAction");
logger.debug(userForm);
System.out.println(userForm);
logger.debug("在LoginDetailAction userForm.hashCode "+userForm.hashCode());
return mapping.findForward("LoginDetail");
}
}
代码清单3:
<html>
<script language="javascript" src="/web/system/js/setday.js"></script>
<title>just for test</title>
<script language="javascript">
<!--
//选择部门
function submitData(form){
form.loginDate.value='2007-2-11';
// form.action=form.action+'?actionType=list';
alert(form.action);
form.submit();
}
//-->
</script>
<body>
<form action="/web/product/patrol/userLoginAction.do" name="userForm" />
name:<input class=flat_u size=30 name="name" />
<input type="hidden" name="actionType" value="list" />
<input class=flat_u maxlength=20 size=30 name="loginDate" readonly>
<img style="CURSOR: hand" src="/web/images/show-calendar.gif" onClick="openCalendar('/web',userForm.loginDate)" >
<input name="button" type=button class=btn_href value= 保 存 onclick="submitData(this.form)" >
</form>
</body>
</html>
/**
* Populate the properties of the specified ActionForm instance from
* the request parameters included with this request. In addition,
* request attribute <code>Globals.CANCEL_KEY</code> will be set if
* the request was submitted with a button created by
* <code>CancelTag</code>.
*
* @param request The servlet request we are processing
* @param response The servlet response we are creating
* @param form The ActionForm instance we are populating
* @param mapping The ActionMapping we are using
*
* @exception ServletException if thrown by RequestUtils.populate()
*/
protected void processPopulate(HttpServletRequest request,
HttpServletResponse response,
ActionForm form,
ActionMapping mapping)
throws ServletException {
if (form == null) {
return;
}
// Populate the bean properties of this ActionForm instance
if (log.isDebugEnabled()) {
log.debug(" Populating bean properties from this request");
}
form.setServlet(this.servlet);
form.reset(mapping, request);
if (mapping.getMultipartClass() != null) {
request.setAttribute(Globals.MULTIPART_KEY,
mapping.getMultipartClass());
}
RequestUtils.populate(form, mapping.getPrefix(), mapping.getSuffix(),
request);
// Set the cancellation request attribute if appropriate
if ((request.getParameter(Constants.CANCEL_PROPERTY) != null) ||
(request.getParameter(Constants.CANCEL_PROPERTY_X) != null)) {
request.setAttribute(Globals.CANCEL_KEY, Boolean.TRUE);
}
}
相关文章推荐
- struts表单数据获取方式
- struts接收表单数据的3种方式
- ExtJS5.1学习笔记6——给表单填充本地数据的两种方式
- 今天在研究jquery用ajax提交form表单中得数据时,学习到了一种新的提交方式
- struts接收表单数据的方式之property
- (转)C# Winform利用POST传值方式模拟表单提交数据(Winform与网页交互)
- 控件真的很好用,突然感觉自己以前研究Discuz!NT366源码的方式很2了
- 通过编程方式在InfoPath 2010表单的下拉框修改事件中获取数据
- 客户端表单提交数据方式与服务器获取数据
- Spark 定制版:010~Spark Streaming源码解读之流数据不断接收全生命周期彻底研究和思考
- js清空表单数据的两种方式(遍历+reset)
- action里获取表单数据的三种方式
- OpenJDK源码研究笔记(五)-缓存Integer等类型的频繁使用的数据和对象,大幅度提升性能(一道经典的Java笔试题)
- 控制器读取视图表单中的数据的几种方式
- Struts源码研究 - html-Link标签篇
- angularjs表单数据提交-对象方式提交
- S2.3_Struts2_ActionGetInputFormData Action动作类接收用户输入表单数据的3种实现方式
- Struts2笔记06 封装获取表单数据方式
- JavaWeb -- Struts1 使用示例: 表单校验 防表单重复提交 表单数据封装到实体
- (Struts)ActionForm类及表单数据验证