JSP 构架-2种方式:Model I和Model II__教程 |
|
日期:2007-5-20 1:18:54 人气:165 [大 中 小] |
|
|
|
======================================================================
Time JSP //the parameter "zone" shall be equal to a number between 0 and 24 (inclusive) TimeZone timeZone = TimeZone.getDefault(); //returns the default TimeZone for the server if (request.getParameterValues("zone") != null) { String timeZoneArg = request.getParameterValues("zone")[0]; timeZone = TimeZone.getTimeZone("GMT+" + timeZoneArg + ":00"); // gets a TimeZone. For this example we´re just going to assume // its a positive argument, not a negative one. } //since we´re basing our time from GMT, we´ll set our Locale to Brittania, and get a Calendar. Calendar myCalendar = Calendar.getInstance(timeZone, Locale.UK); <%= myCalendar.get(Calendar.HOUR_OF_DAY) %>: <%= myCalendar.get(Calendar.MINUTE) %>: <%= myCalendar.get(Calendar.SECOND) %> ====================================================================== Similarly, the data to be displayed could have been gotten from a JavaBean. We´ll see a little of that in the next example. Model II: Redirecting Requests In a team environment where some members are HTML designers and others are Java programmers, this approach can be particularly strong. The Java programmers can focus on creating (re)usable code, while the HTML designers can focus on presentation. While the two remain dependant on each other, one or the other can change dramatically so long as the principle inputs and outputs (respectively) remain the same.
Now we´ll take the same desired behaviour from the Model I example, and present it using the Model II methodology. This methodology follows the Model-View-Controller (MVC) paradigm (cite Design Patterns book). For this example, we´ll have one class (or page or servlet) process the request (Controller), get the TimeZone, set all the required variables for presentation, and pass control off to a presentation page (View). For simple apps like this, there is no "Model".
Controller: timeByZone.jsp The controller can be a servlet or a jsp file. I prefer to use JSP, as I don´t have to worry about compiling the class each time I make changes. However, you lose granularity this way, and make it more difficult to extend this class later (we´ll review this in Advanced JSP Programming).
====================================================================== //the parameter "zone" shall be equal to a number between 0 and 24 (inclusive) TimeZone timeZone = TimeZone.getDefault(); //returns the default TimeZone for the server if (request.getParameterValues("zone") != null) |
|
出处:本站原创 作者:佚名 |
|
|