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类里面,代码如下:
===========================
/**
* <p>returns <code>true</code> if the current form's cancel button was
* pressed. this method will check if the <code>globals.cancel_key</code>
* request attribute has been set, which normally occurs if the cancel
* button generated by <strong>canceltag</strong> was pressed by the user
* in the current request. if <code>true</code>, validation performed
* by an <strong>actionform</strong>'s <code>validate()</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中,代码如下:
===========================
/**
* <p>process an <code>httpservletrequest</code> and create the
* corresponding <code>httpservletresponse</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 <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);
}
}
===========================
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
闽公网安备 35060202000074号