王朝网络
分享
 
 
 

Struts源码研究-logic-Iterator标签篇

王朝other·作者佚名  2008-05-31
宽屏版  字体: |||超大  

Struts里的Html:Cancel标签是在Form中经常运用的一个标签,主要功能就是cancel当前Form,一般写法如下:

===========================

<html:cancel

<bean:message key="createuser.cancelbutton"/

</html:cancel

===========================

这个标签将生成如下的HTML代码:

<input type="submit" name="org.apache.struts.taglib.html.CANCEL" value="返回" onclick="bCancel=true;"

bCancel=true是一段javascript,bCancel是在使用Struts的Validator时,Struts自动为我们加的一段Javascript代码里的一个变量

这段Javascript简略摘要如下:

===========================

<script type="text/javascript" language="Javascript1.1"

<!-- Begin

var bCancel = false;

function validateCreateUserForm(form) {

if (bCancel)

return true;

else

return validateMaxLength(form) && validateRequired(form) && validateMinLength(form);

}

===========================

由上可以看到,这个bCancel=true时,Javascript将自动将表单提交(return true),也就是说,假如我们在后台Action的代码里没有对这个Cancel动作写特定代码的话,这个Cancel标签产生的效果和submit按钮产生的动作完全一致!!(因为这个按钮的type也等于submit)这一点需要非常的注重!所以,一般来说,我们在Action类的execute方法里面会加上如下的一段代码来处理这个Cancel动作:

===========================

// Was this transaction cancelled?

if (isCancelled(request)) {

return (mapping.findForward("createusersUCcess"));

}

===========================

有了以上的代码,Cancel动作就有了相应的处理代码,转到相关的页面了。本来事情已经解决,但本着对Struts源码研究的精神,我们还需要对以上代码研究一下

OK,让我们来看一下isCancelled这个方法在什么地方被定义了,内容是什么?

首先发现,这个方法被定义在Action类里面,代码如下:

===========================

/**

* <pReturns <codetrue</code if the current form's cancel button was

* pressed. This method will check if the <codeGlobals.CANCEL_KEY</code

* request attribute has been set, which normally occurs if the cancel

* button generated by <strongCancelTag</strong was pressed by the user

* in the current request. If <codetrue</code, validation performed

* by an <strongActionForm</strong's <codevalidate()</code method

* will have been skipped by the controller servlet.</p

*

* @param request The servlet request we are processing

* @see org.apache.struts.taglib.html.CancelTag

*/

protected boolean isCancelled(HttpServletRequest request) {

return (request.getAttribute(Globals.CANCEL_KEY) != null);

}

===========================

原来是在request对象中查找Globals.CANCEL_KEY这个key值是否绑定了一个对象,假如是,那么就代表按下Cancel按钮后,

Struts会在request对象中绑定一个对象,并以这个key值来命名那Struts是在什么地方绑定了这个对象呢?很自然的,让我们从头找起从ActionServlet的process方法开始找起,历经多次方法调用,终于找到了根源,原来是在RequestProcessor.java中,代码如下:

===========================

/**

* <pProcess an <codeHttpServletRequest</code and create the

* corresponding <codeHttpServletResponse</code.</p

*

* @param request The servlet request we are processing

* @param response The servlet response we are creating

*

* @exception IOException if an input/output error occurs

* @exception ServletException if a processing exception occurs

*/

public void process(HttpServletRequest request,

HttpServletResponse response)

throws IOException, ServletException {

//省略代码若干

// Process any ActionForm bean related to this request

ActionForm form = processActionForm(request, response, mapping);

//答案就在这个processPopulate方法中

processPopulate(request, response, form, mapping);

if (!processValidate(request, response, form, mapping)) {

return;

}

/**

* Populate the properties of the specified ActionForm instance from

* the request parameters included with this request. In addition,

* request attribute <codeGlobals.CANCEL_KEY</code will be set if

* the request was submitted with a button created by

* <codeCancelTag</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);

}

}

===========================

OK,看最后几行代码,Struts从request中取得Constants.CANCEL_PROPERTY这个参数,假如这个参数不为空,那么他就将TRUE这个对象以Globals.CANCEL_KEY为key值,放到了request对象中至于这个Constants.CANCEL_PROPERTY这个值是什么,现在都可以猜到了,显然就是html:Cancel这个标签生成的HTML代码中,Cancel这个按钮的名称嘛!查了一下,果然是:

<input type="submit" name="org.apache.struts.taglib.html.CANCEL" value="返回" onclick="bCancel=true;"

而Constants.CANCEL_PROPERTY这个值就是org.apache.struts.taglib.html.CANCEL

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
>>返回首页<<
推荐阅读
 
 
频道精选
 
静静地坐在废墟上,四周的荒凉一望无际,忽然觉得,凄凉也很美
© 2005- 王朝网络 版权所有