在Jsp中模拟WebForm(一) 
     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; //页面参数
    static Class paramTypes[] = new Class[1];//页面事件的参数类型 
       //取得操作页面的基本组件        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()));//向用户给出出错提示.         }      }   }          当然,实用的 PageHandler应提供更为复杂的功能。     具体的页面处理器类从此类继承下来,现在,我们用一个简单的例子说明页面处理器的用法:假设有这样一个页面,有一个文本框要求用户输入一个数字,有两个按钮,点击一个要求计算出用户输入数字的2倍,点击另外一个按钮要求计算出用户输入数字的10倍。再假设此页面的页面处理器类为JspTest.    //test.jsp    <%@ page contentType="text/html; charset=GB2312" %>    <%@ page import="youpackage.JspTest" %>    <%       JspTest handler=new JspTest(pageContext);       handler.process();//调用页面处理器       String formAction=request.getRequestURI()+"?"+request.getQueryString();    %>    <html>    <head>    <title>测试页面处理器</title>    <script language="javascript">       function on_event(action,params)       {          window.form1.action.value=action;          window.form1.parameters.value=params;          window.form1.submit();       }    </script>    </head>    <body bgcolor="#ffffff">    <form name="form1" method="post" action="<%=formAction%>">    请输入数字:<input type="text" name="t_value" value="<%=handler.t_value%>">    <br><br>       <font color="red"><%=handler.result%></font>    <br><br>    <input type="button" name="b1" value="2倍" onclick="on_event('onTwo','')">           <input type="button" name="b2" value="10倍" onclick="on_event('onTen','')"> 
   <input type="hidden" name="action" value=""/>    <input type="hidden" name="parameters" value=""/>    </form>    </body>    </html> 
   则,我们为以上页面定义其页面处理器:JspTest    //JspTest.java    public class JspTest extends PageHandler    {         //定义页面变量          public int t_value;//用户输入的整数          public String result;//存储计算结果 
        public JspTest(PageContext page)         {             super(page);         } 
        protected void onLoad() throws Exception         {            t_value=0;            result="";            //在实际应用中,这里应作许多的初始化工作(如,得到页面参数)         }                  //双倍         private void onTwo(String str_params) throws Exception         {             try             {                 t_value=Integer.parseInt(request.getParameter("t_value"));             }             catch(Exception e)            {                 out.println("<script language='javaScript'>alert('您输入的不是有效的整                  数.');</script>");            }            int i=2*t_value;            result="计算结果为:"+i;         }         //10倍         private void onTen(String str_params) throws Exception         {             try             {                 t_value=Integer.parseInt(request.getParameter("t_value"));             }             catch(Exception e)             {                 out.println("<script language='javaScript'>alert('您输入的不是有效的整                数.');</script>");             }             int i=10*t_value;             result="计算结果为:"+i;        }    } 
      WebForm的基本思想也就在于此,当然,WebForm中的服务器端控件的状态可以自动保持(而我们的实现为保持状态还需作一些工作),WebForm的控件属性可以在后置代码中进行操作,服务器端事件可以在后置代码中进行邦定,服务器端控件支持数据邦定等等,我们的实现还无法做到。    如果能在Jsp中定义类似服务器端控件的东东,以上的功能在Jsp中可以得以实现。   (待续..)
  
 相关文章: 在Jsp中模拟WebForm(二) 在Jsp中模拟WebForm(三) 在Jsp中模拟WebForm(四) 在Jsp中模拟WebForm(五)
  
 
  |