The Window toolkit provides a typical layout mechanism, such as determining the position of component elements in a container. Both AWT and Swing have a layout manager, and wrapper in VisualWorksSmalltalk. This article introduces a JSP template mechanism that allows layout encapsulation and reuse. JSP templates minimize the impact of layout changes. Here we provide a typical layout mechanism for the drum Window Toolkit, such as determining the position of component elements in a container. Both AWT and Swing have the layout manager, and wrapper in VisualWorks Smalltalk. This article introduces a JSP template mechanism that allows layout encapsulation and reuse. JSP templates minimize the impact of layout changes. here we will encourage you to adopt encapsulation modular design.
Despite rapid improvements to Web development tools, they still lag behind the graphical user interface (GUI) Toolkit (Swing and VisualWorks Smalltalk ). For example, a layout manager is provided in a traditional GUI toolkit, allowing layout operations to be encapsulated and reused in one form or another. This JSP template mechanism described in this article can encapsulate the layout just like the layout manager, so it can be reused instead of simply copied and used.
As many changes have occurred during the development of the layout, the function encapsulation is a very important step. it can be freely modified to minimize the impact on other applications.
JSP does not provide direct support for the encapsulation layout. Therefore, a webpage with a uniform format can usually copy the layout code. for example, a webpage is displayed in, it contains the title, footer, toolbar, and main content of the page.
. Webpage layout click to enlarge (22 KB)
The webpage layout displayed in is executed using HTML table labels:
Example 1. content:
JSPtemplates
<% @ Include fileate'sidebar.html '%> |
<% @ Include file='header.html '%> |
<% @ Include fileate'introduction.html '%> |
<% @ Include fileout'footer.html '%> |
|
In the preceding example, the JSP include command is included, which allows page content to be changed-by modifying the contained file-the webpage itself does not need to be modified. However, because the layout is hard to be encoded, the layout changes need to be modified on the webpage. If a website has multiple pages in the same format, modifications to the entire page are usually involved in even simple layout changes.
To reduce the impact of layout changes, we need a mechanism that only contains the layout. with this mechanism, the layout and content can be modified separately without modifying the file. This mechanism is the JSP template.
Use Template
A template is a JSP file that contains parameterized content. The template discussed here uses a set of customized labels for execution: template: get, template: put and template: insert. Template: The get tag accesses parameterized content, as in example 2. a. it generates a webpage in the same format as Figure 1.
Example 2. a template
<% @ Taglib uri = '/WEB-INF/tlds/template. tld' prefix = 'template' %>
<Template: get name = 'title'/>
Example 2. a is almost the same as example 1, but in example 2. in a, we used template: get to replace the include command in example 1. let's analyze how template: get runs.
Template: get uses a special name (within the request range) to modify a Java Bean. Bean contains URI (a uniform resource identifier, a component of a webpage, which is included in template: get ). For example, in the template list of example 2. a, template: get contains a uriworkflow containing header.html-from a Bean named header (within the request range ). Then we include header.html in template: get.
Template: put the Bean within the request range (this range will be modified by template: get later ). The template is included in template: insert. Example 2. B illustrates the use of put and insert labels:
Example 2. B. use the template from example 2.
<% @ Taglib uri = '/WEB-INF/tlds/template. tld' prefix = 'template' %>
The label at the beginning of insert specifies the included template. In this example, the template is in example 2.. Each put Tag stores a Bean within the request range, and the tag at the end of insert contains the template. The template then accesses the Bean as described above.
Direct attributes can be specified for template: put. if direct is set to true, the content associated with the tag is not included in template: get.
A website contains pages in the same format as multiple pages, so that you can use a template, for example, in example 2. a lists a template, which is used in many JSP pages (for example, 2. B.
Another advantage of using templates is that they can be modularized. For example, if header.html is included in the JSP file listed in example 2. B, let's take a look at example 2.c.
Example 2. c. header.html
Because header.html is contained, it does not have to copy its code on the page where the header needs to be displayed. Header.html is an HTML file, but the general starting HTML tag is not used in the file (for exampleOr), Because these labels will be defined by the template. Because header.html is included in the template, these labels do not need to be used again in header.html.
Note: JSP provides two methods to include content: static mode, using the include command; dynamic mode, using the include action. The include command contains the reference source of the target page, which is similar to # include in C and import in Java. Include action contains the response generated by the target during the running time.
Like JSP include action, a template contains dynamic content. Therefore, although the JSP pages in example 1 and Example 2. B are functionally consistent, the static content contained above is dynamically included later.
Optional
All template content is optional, and the template content can be easily used in more webpages. For example, in figure 2. a and Figure 2. B, two pages-logon and list-are displayed. they use the same template. Both pages contain a header, footer, and main content. The configuration page contains an editing Panel (which is missing from the login page) used to change the configuration.
Figure 2. a. click to enlarge a login window (24 KB)
Figure 2. B. click to enlarge a list page (42 KB)
Below, you will find that the template will be shared with the list page:
<% @ Taglib uri = 'template. tld 'prefix = 'template' %>
......
......
The list page uses the template above and the content specifically used to edit the Panel:
<% @ Taglib uri = 'template. tld 'prefix = 'template' %>
<% @ Taglib uri = 'security. tld 'prefix = 'security' %>
......
Content = '/editPanelContent. jsp'/>
......
Compared with the above, the login page does not have content specifically used to edit the Panel:
<% @ Taglib uri = 'template. tld 'prefix = 'template' %>
Content = '/login. jsp'/>
The logon page does not contain content specifically used to edit the Panel.
Role-based content
Web applications often generate different content based on different users. For example, for the same JSP template, editing Panel appears only when the user is the administrator. the following two different pages are obtained (3. a and 3. B .)
Figure 3. a. enlarge the administrator list page (27 KB)
Figure 3. B. click Zoom In (21 KB) on the other user list page)
The template in. a and 3. B uses the role attribute of template: get:
<% @ Taglib uri = 'template. tld 'prefix = 'template' %>
......
......
The get tag only contains content when the Role and Role attributes of the user match. Let's take a look at how the label handler uses the Role attribute:
Public class GettagextendstagSupport {
Private String name = null, role = null;
......
Public void setRole (String role) {this. role = role ;}
......
Public int doStartTag () throws JspException {
......
If (param! = Null ){
If (roleIsValid ()){
// Include or print content ......
}
}
......
}
Private boolean roleIsValid (){
Return role = null | // valid if role isn' t set
(Javax. Servlet. http. HttpServletRequest)
PageContext. getRequest (). isUserInRole (role );
}
}