欢迎访问悦橙教程(wld5.com),关注java教程。悦橙教程  java问答|  每日更新
页面导航 : > > 文章正文

JSP设计模式中的两种常见模式(1)

来源: javaer 分享于  点击 43217 次 点评:110

JSP设计模式中的两种常见模式(1)


如果你经常去Servlet或JSP的新闻组或者邮件列表,那么一定会看到不少关于Model I 和Model II 方法的讨论。究竟采用哪一种,这取决于你的个人喜好、团队工作策略以及是否采用正统的OOP。

简单地说,Model I将事务逻辑business logic)和表示代码presentation code)融合在一起如在HTML中);Model II则提倡最大限度地将所有的代码放到内容表示之外。

Model I: 简单的单层次应用

如果是在一个人人都精通Java和HTML的环境中,或者你独自做着所有的工作,假如每个人都有清晰的编程结构和思路,那么这种方法会很有效,不过这样的假设不在本文讨论范围之内。这种方法的第一个优点是如果你的应用改变了,你只需维护一个文件。而最大的缺陷是可读性!除非十分小心,否则你的HTML和Java代码会相互混杂,从而难以维护。

在下面这个例子中,我们将增加一个 TimeZone 元素,从而使它变成JSP文件,它会返回基于时间的所期待的TimeZone。如果没有提交 TimeZone,那么缺省的是服务器的缺省时间。

  1. ======================================================================   
  2. ﹤xml version=“1.0“ ?﹥   
  3. ﹤H1﹥Time JSP﹤/H1﹥   
  4. ﹤jsp:scriptlet﹥   
  5. //the parameter “zone“ shall be equal to a number between 0 and 24 (inclusive)   
  6. TimeZone timeZone = TimeZone.getDefault(); //returns the default TimeZone for the server   
  7. if (request.getParameterValues(“zone“) != null)   
  8. {   
  9. String timeZoneArg = request.getParameterValues(“zone“)[0];   
  10. timeZone = TimeZone.getTimeZone(“GMT+“ + timeZoneArg + “:00“);   
  11. // gets a TimeZone. For this example we´re just going to assume   
  12. // its a positive argument, not a negative one.   
  13. }   
  14. //since we´re basing our time from GMT, we´ll set our Locale to Brittania, and get a Calendar.   
  15. Calendar myCalendar = Calendar.getInstance(timeZone, Locale.UK);   
  16. ﹤/jsp:scriptlet﹥   
  17. ﹤%= myCalendar.get(Calendar.HOUR_OF_DAY) %﹥:   
  18. ﹤%= myCalendar.get(Calendar.MINUTE) %﹥:   
  19. ﹤%= myCalendar.get(Calendar.SECOND) %﹥   
  20. ======================================================================  

相应地,数据也可以从JavaBean取得并加以显示。在下一个例子中我们就可以看到。

Model II: 重定向请求Redirecting Requests)

在一个团队开发环境中,有些是HTML设计者,另一些则是Java程序员,这时这一方法显得非常重要。Java程序员可以集中精力创建可重用代码,而HTML设计师可以集中精力于内容表示,彼此相对对立,可以分别动态地修改自己的内容,只要总体的输入输出不变。

现在我们可以使用Model II来表示Model I的那个例子。这一方法遵循了Model-View-Controller (MVC) 范例 (cite Design Patterns book)。 在这个例子中,我们只有一个类(页或者servlet) 处理请求(Controller),取得TimeZone,设置所有用于表示的变量,并将控制传递到表示页(View)。作为如此简单的应用,可以没有 “Model“。

Controller: timeByZone.jsp

controller可以是一个servlet或一个JSP页。我推荐使用JSP,因为这样我不必担心每当我做修改时要对类重新编译,但是,你将因此失去granularity颗粒性),以后要扩展该类也比较困难。

  1. ======================================================================   
  2. ﹤xml version=“1.0“ ?﹥   
  3. ﹤!--Worker Class, nobody should see me--﹥   
  4. ﹤jsp:scriptlet﹥   
  5. //the parameter “zone“ shall be equal to a number between 0 and 24 (inclusive)   
  6. TimeZone timeZone = TimeZone.getDefault(); //returns the default TimeZone for the server   
  7. if (request.getParameterValues(“zone“) != null)   
  8. {   
  9. String timeZoneArg = request.getParameterValues(“zone“)[0];   
  10. timeZone = TimeZone.getTimeZone(“GMT+“ + timeZoneArg + “:00“);   
  11. // gets a TimeZone. For this example we´re just going to assume   
  12. // its a positive argument, not a negative one.  
  13.  
  14. }   
  15. TimeBean timeBean = new TimeBean();   
  16. timeBean.setHours = myCalendar.get(Calendar.HOUR_OF_DAY);   
  17. timeBean.setMinutes = myCalendar.get(Calendar.MINUTE);   
  18. timeBean.setSeconds = myCalendar.get(Calendar.SECOND);   
  19. HttpSession mySession = request.getSession();   
  20. mySession.putValue(“tempTimeBean“, timeBean);   
  21. ﹤/jsp:scriptlet﹥   
  22. ﹤jsp:forward page=“displayTime.jsp“ /﹥   
  23. ======================================================================  

View: displayTime.jsp

同样地,这个view既可以是一个servlet也可以是一个jsp文件。这里我们从Session中取得并显示它的值。实际上我们会将这做两次,来示范Bean是如何被使用的。

  1. ======================================================================   
  2. ﹤xml version=“1.0“ ?﹥   
  3. ﹤H1﹥Time JSP﹤/H1﹥   
  4. ﹤jsp:useBean class=“TimeBean“ id=“tempTimeBean“ scope=“session“ /﹥   
  5. ﹤jsp:getProperty name=“tempTimeBean“ property=“hours“﹥:   
  6. ﹤jsp:getProperty name=“tempTimeBean“ property=“minutes“﹥:   
  7. ﹤jsp:getProperty name=“tempTimeBean“ property=“seconds“﹥   
  8. ﹤!-- these would have printed “null“ if tempTimeBean was not instantiated by timeByZone.jsp --﹥   
  9. ﹤jsp:scriptlet﹥   
  10. HttpSession mySession = request.getSession();   
  11. TimeBean timeBean = mySession.getValue(“tempTimeBean“);   
  12. if (timeBean != null)   
  13. { // check to make sure its not null, to avoid NullPointerExceptions   
  14. out.print(timeBean.getHours());   
  15. out.print(“:“);   
  16. out.print(timeBean.getMinutes());   
  17. out.print(“:“);   
  18. out.print(timeBean.getSeconds());   
  19. }   
  20. else   
  21. {   
  22. out.println(“Press your Back button and select a TimeZone“);   
  23. }   
  24. ﹤/jsp:scriptlet﹥   
  25. ======================================================================  


相关文章

    暂无相关文章
相关栏目:

相关文章

    用户点评