JR 精品文章 - Struts源码研究 - html-Link标签篇
AD: jr (at) javaresearch.org


首页 | 动态 | 文章 | FAQ  | 新闻 | 下载 | 代码 | 工作 | 调查 | 术语 | 站点 | 图书 | 论坛 | 帮助 | 全部  

TOP | 交流 | 软件 | 专栏 | 开源 | 译/著 | 源码 | API  | 推荐 | FTP  | 积分 | 统计 | 搜索 | Blog | 我们  
首页 » 研究文集 » 开发框架 搜索标题相关文章 搜索标题相关文章    评论此文章 发表评论     开始监控此文章 开始监控   加入收藏夹  加入收藏夹
Struts源码研究 - html-Link标签篇
tangxingbin 整理   更新:2007-08-06 22:46:03  版本: 1.0   

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




版权声明  
本篇文章对您是否有帮助?  投票:         投票结果:     15       0
作者其它文章: 作者全部文章
评论人:csyfc 发表时间: Wed Aug 15 16:18:45 CST 2007
[:)]
评论人:whw19840229 发表时间: Fri Apr 04 20:13:26 CST 2008
学习中[good]

这个文章共有 2 条评论
主题: Struts彻底实践中文问题的解决方法 上一篇文章
返回文章列表 返回〔开发框架〕
下一篇文章 主题: struts标签使用举例--logic篇


文字广告链接
        自主、快速定制基于JAVA的B/S业务系统          重量级企业在线自定义WEB报表平台
        Excel制表、零代码发布、打印、图表结合——快逸报表,免费、稳定、功能强大的java工具
        技术圈: 关于Java、dotNet、PHP、Ruby、奇客、Web2.0等更多资讯博客精选文章

关于 JR  |  版权声明  |  联系我们 

©2002-2006 JR 版权所有 沪ICP备05019622号