===================">
 
网站首页
JSP空间
动态资讯
开源项目
技术文档
资源下载
J2EE资源
客户论坛
在线支付
 
  技术文档>>JAVA>>新手入门>>基础入门>查看文档  
  struts源码研究 - logic-iterator标签篇     
  文章作者:未知  文章来源:水木森林  
  查看:75次  录入:管理员--2007-11-17  
 
  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
 
 
上一篇: struts源码研究 - action-input属性篇    下一篇: struts源码研究 - html-link标签篇
  相关文档
java代码编程规范 11-17
为rmi实现类jini的发现机制 11-17
tomcat下配置mysql数据库连接池 11-16
java多线程编程基础之线程对象 11-17
java socket编程(一) socket传输模式 11-16
j2ee应用中与oracle数据库的连接 11-16
java学习笔记 线程实例:一个钟表的实现 11-17
jxta概念介绍(翻译) 11-17
深入java中文问题及最优解决方法-下 11-16
struts开发指南之mvc架构 11-16
java新手留意:java编程三十条规则 11-16
jsp中连接sql2000数据库的问题总结 11-17
资源访问的错误方法 11-17
高级:编写多线程java应用程序常见问题 01-03
j2ee到底是什么? 11-17
java基础:date和calendar类的使用方法 11-16
用jsp定制标签创建丰富的超连接(二) 11-17
构造java函数 11-17
static和final修饰类属性变量及初始化 11-17
java新手必备:java基础之关键字 11-16
返回首页 | 关于我们 | J网章程 | JSP空间合租 | 客服中心 | 免责声明 | 常见问题 | 参观机房
本站主机空间代理至厦门市华众网络科技有限公司
《中华人民共和国增值电信业务经营许可证》
编号:闽B2-20050079
@2005-2008福建JSP技术网 版权所有 闽ICP备05000928号
技术电话:13616026886
邮箱:admin@fjjsp.com 站长QQ,点击这里给我发消息