动态网站制作指南 [  QQ表情  ]
[ 投票调查 ]
[ 企业邮箱 ]
[ 网站空间 ]
网络编程 | 站长之家 | 网页制作 | 图形图象 | 操作系统 | 冲浪宝典 | 软件教学 | 网络办公 | 邮件系统 | 网络安全 | 认证考试 | 系统进程
ASP源码 | .Net源码 | PHP源码 | JSP源码 | JAVA源码 | CGI源码 | VB源码 | C++源码 | Delphi源码 | PB源码 | VF源码 | 汇编 | 服务器
Firefox | IE | Maxthon | 迅雷 | 电驴 | BitComet | FlashGet | QQ | QQ空间 | Vista | 输入法 | Ghost | Word | Excel | wps | Powerpoint
asp | .net | php | jsp | Sql | c# | Ajax | xml | Dreamweaver | FrontPages | Javascript | css | photoshop | fireworks | Flash | Cad | Discuz!
当前位置 > 网站建设学院 > 网络编程 > Java教程
Tag:注入,存储过程,分页,安全,优化,xmlhttp,fso,jmail,application,session,防盗链,stream,无组件,组件,md5,乱码,缓存,加密,验证码,算法,cookies,ubb,正则表达式,水印,索引,日志,压缩,base64,url重写,上传,控件,Web.config,JDBC,函数,内存,PDF,迁移,结构,破解,编译,配置,进程,分词,IIS,Apache,Tomcat,phpmyadmin,Gzip,触发器,socket
网络编程:ASP教程,ASP.NET教程,PHP教程,JSP教程,C#教程,数据库,XML教程,Ajax,Java,Perl,Shell,VB教程,Delphi,C/C++教程,软件工程,J2EE/J2ME,移动开发
文章搜索服务
邮件订阅
输入你的邮件地址,
你将不会错过任何关于:
[ Java教程 ]的信息

本月文章推荐
.java中按字节截取String字符串..
.Java中鲜为人知的缺点(上).
.使用消息驱动Beans(1).
.java 跳转语句.
.使用MIDP2.0开发游戏(2)使用Spri.
.Struts实时生成Excel文件下载.
.JBuilder2005+JBOSS+Oracle9i环境.
.Java Servlet 编程及应用之六.
.WhatisAspectJ.
.区分Tomcat与Web服务器、应用服务.
..NET使用WMI获得硬盘的信息.
.使用 Translator 模式构建更好的.
..NET2005提供的Code Snippets看代.
.RMI远程方法调用讲解教程.
.Weblogic上配置Hibernate为JNDI.
.[JAVA100例]038、操作Excel文件.
.小小调查通.
.j2ee13种核心技术.
.创建Web应用和Struts框架的配置文.
.Java Web Start开发指南(二).

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

发表日期:2008-1-5 |



  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源码研究 - html-Link标签篇 人气:462
下一篇:struts中文问题和国际化问题的终极解决方案 人气:513
浏览全部Java的内容 Dreamweaver插件下载 常用网页广告代码全集
  最新网站源码 最新软件下载
2008-9-6 Movie34电影搜索引擎 v3.0
2008-9-6 wap2.0仿帝国建站喜用 v2.0
2008-9-6 免费人才招聘网 宽屏版 v3.01
2008-9-6 喜喔喔视频采集程序 v1.0 beta
2008-9-6 ASP客户管理系统
2008-9-6 主流驿站中秋祝福程序
2008-9-6 php实现msn协议的类
2008-9-5 Coppermine Photo Gallery v1.4.
2008-9-5 清松网络日记本 v2.4
2008-8-23 Mini WinMount V0.4
2008-8-23 Vista优化大师3.11正式版
2008-8-23 Wine 1.13
2008-8-23 KlipFolio 5.0 Build 5899-80
2008-8-23 Windows Sysinternals Desktops
2008-8-23 OneTap Movies1.2破解版
2008-8-23 AnnotaterPDF阅读1.1.503 破解版
2008-8-23 SoundMeter分贝测量仪 v1.0汉化破
2008-8-23 iDrum音乐节拍1.0破解版
  发表评论
姓 名: 验证码:
内 容:
站长工具:网站收录查询 | Google PR查询 | ALEXA排名查询 | CSS在线编辑器 | 广告代码 | Html转换js | js/vbs加密 | md5加密 | 进制转换
实用工具:汉字翻译拼音 | 符号对照表 | 个税计算 | 经典小工具 | 汉字简繁转换 | 普通单位换算 | 公制单位换算 | 生辰老黄历 | 国内电话区号 国家代码与域名缩写 | 文字加密解密 | 健康查询 | 万年历 | 汉字横竖排版 | 手机号码查询 | 计算器 | ip搜索
业务联系 | 广告刊登 | 频道合作 | 投稿荐稿 | 联系方式 | 加入收藏 | RSS订阅
Copyright © 2000-2008 www.knowsky.com All rights reserved | 网络实名:动态网站制作指南 | 沪ICP备05001343号