您现在的位置: 捷凌网安 >> 服务器 >> WEB开 发 >> JSP >> 正文
在JSP开发中模拟.NET WebForm

作者:佚名 责任编辑:左决 点击数: 更新时间:2008-2-20 0:27:01

  WebForm是事件驱动的,控件状态可以在http请求之间自动保持,并且使用后置代码很好地实现了页面外观与页面逻辑控制的分离,一改以往html,服务器段代码、javaScript混杂在一起的web开发方式。stucts提供了大量的定制标签,由tag、form、bean、action及配置文件构建了一个优秀的MVC模式的web开发方式。但相比较其WebForm来,窃以为stucts更为复杂,需要协同工作的元素较多,解决问题的效果不如WebForm显著(仅是个人看法)。

  在现实开发中,常常需要在某个页面中处理很多Form控件,且要处理这个页面可能引发的多个事件,在事件触发后,又请求同一个页面,又需要在请求之间保持状态,在页面中处理所有这些,真实不胜其烦。受到WebForm启发,我在用JSP进行开发时,借鉴了了其一些思想。本质上我们就是想让页面显示代码与页面控制代码分离,要作到这一点并不困难,有很多办法。

  可以为页面定义一个“页面处理器(PageHandler)”,它类似WebForm的后置代码,它的接口基本是下面这个样子:

  public class PageHandler
{
 protected HttpServletRequest request;
 protected HttpServletResponse response;
 protected JspWrITer out;
 protected PageContext pageContext;
 protected HttpSession session = null;
 protected ServletContext application = null;
 protected ServletConfig config = null;
 protected String event_action = null; //页面事件
 protected String event_params = null; //页面参数
 //取得操作页面的基本组件
 public PageHandler(PageContext page)
 {
  this.pageContext = page;
  this.request = (HttpServletRequest) pageContext.getRequest();
  this.response = (HttpServletResponse) pageContext.getResponse();
  this.pageContext = page;
  out = pageContext.getOut();
  application = pageContext.getServletContext();
  config = pageContext.getServletConfig();
  session = pageContext.getSession();
  try{
   request.setCharacterEncoding("gb2312");//设定页面编码
  }
  catch(Exception e)
  {
   e.printStackTrace();
  }
 }
 //初始化页面的参数,具体的页面处理器类可以重写这
 //个方法进行页面初始化
 protected void onLoad() throws Exception
 {
 }
 //根据页面指定的事件进行处理
 private final void eventBind() throws Exception
 {
  //event_action从从页面的名为event_action的hidden字段取得,它意为事件的称,
  //当此事件触发时,他会寻找在"页面处理器类中"与event_action同名的方法加
  // 以调用。
  if (event_action != null && !event_action.equals(Format.Empty))
  {
   event_params = request.getParameter("parameters"); //事件参数参数,从页面
   //的名为parameters的hidden字段取得
   if (paramTypes[0] == null)
   {
    paramTypes[0] = Class.forName("java.lang.String");
   }
   Object paramValues[] = new Object[1];
   paramValues[0] = event_params;
   Method method = null;
   try
   {
    method = this.getClass().getDeclaredMethod(event_action, paramTypes);
    method.setAccessible(true);
   }
   catch (Exception e)
   {
    throw new UserException("系统缺少对您的请求的处理机制: + event_action);
   }
   if (method != null)
   {
    method.invoke(this, paramValues); //调用web时间
   }
  }
 }
 //处理页面
 public void process() throws Exception
 {
  try
  {
   event_action = request.getParameter("action"); //得页面事件
   onLoad();//页面加载时的初始化
   eventBind();//处理事件
  }
  catch (Exception e)
  {
   e.printStackTrace(); ///////////////
   Format.alert(out, "发生了未知错误:" + Format.getString(e.getMessage()));
  }
 }
}

  • 上一篇文章:

  • 下一篇文章:
  •  
    最进更新
    普通文章foxpro 更新源表05-05
    普通文章foxpro 让视图与数据源相连05-05
    普通文章foxpro 机动查询和数据输入05-05
    普通文章foxpro 多个本地数据05-05
    普通文章foxpro 维护源表05-05
    普通文章Oracle 10g Release2新功能之05-05
    普通文章将Oracle 10g内置的安全特性05-05
    普通文章ACCESS 2003 建立数据库视频05-05
    普通文章三种SQL分页法效率分析05-05
    普通文章优化MySQL数据库查询的三种方05-05
     
    推荐文章
    推荐文章教你怎样在MySQL中提高全文搜05-05
    推荐文章SQL Server中数据导入导出三05-05
    推荐文章缓冲技术提高JSP程序的性能和04-17
    推荐文章asp去除HTML标记的三个实用函04-17
    推荐文章何时使用DataGrid、DataList04-17
    推荐文章MySQL存储过程示例04-14
    推荐文章华硕搭建Exchange2007企业邮03-14
    推荐文章升级Win 2003到Windows 200803-14
    推荐文章windows Server 2003 搭建域03-14
    推荐文章服务器成为IT中心的6个理由03-11
     
    热点文章 
    普通文章SQL Server 2008分析服务概览05-01
    普通文章Dlink路由器VPN设置04-29
    推荐文章缓冲技术提高JSP程序的性能和04-17
    普通文章教你优化你的ASP程序04-17
    推荐文章asp去除HTML标记的三个实用函04-17
    普通文章ASP添加验证码的解决方法04-17
    推荐文章何时使用DataGrid、DataList04-17
    普通文章Asp.net中禁止用户多次登录04-17
    普通文章MySQL之表结构修改04-14
    推荐文章MySQL存储过程示例04-14

    | 设为首页 | 加入收藏 | 联系站长 | 广告服务 | 友情链接 | 版权申明 | 网站地图 |

    在线交流 捷凌网安主群:51649627
    Copyright 2007-2008 © 捷凌网安. All rights reserved.
    备案序号:蜀ICP备08001812号