JSP template Application Guide (I)

Source: Internet
Author: User
Tags tld

The Window Toolkit provides a typical layout mechanism, 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, in Figure 1, a webpage is displayed, it contains the title, footer, toolbar, and main content of the page.

Figure 1. webpage layout click to enlarge (22 KB)

The page layout shown in Figure 1 is executed using HTML table labels:

Example 1. content:

<Html>

<Body background = 'graphics/background.jpg '>

<Table>

<Tr valign = 'top'> <td> <% @ include file='sidebar.html '%> </td>

<Td> <table>

<Tr> <td> <% @ include fileate'header.html '%> </td> </tr>

<Tr> <td> <% @ include file='introduction.html '%> </td> </tr>

<Tr> <td> <% @ include file}'footer.html '%> </td> </tr>

</Table>

</Td>

</Tr>

</Table>

</Body>

In the preceding example, the JSP include command is included, which allows page content to be changed-by modifying the contained file-the page 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' %>

<Html>

<Body background = 'graphics/background.jpg '>

<Table>

<Tr valign = 'top'> <td> <template: get name = 'inclubar'/> </td>

<Td> <table>

<Tr> <td> <template: get name = 'header'/> </td> </tr>

<Tr> <td> <template: get name = 'content'/> </td> </tr>

<Tr> <td> <template: get name = 'footer '/> </td> </tr>

</Table>

</Td>

</Tr>

</Table>

</Body>

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 retrieves a uri--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' %>

<Template: inserttemplate = '/articleTemplate. jsp'>

<Template: put name = 'title' content = 'templates' direct = 'true'/>

<Template: put name = 'header' content = '/header.html'/>

<Template: put name = 'inclubar' content = '/sidebar. jsp'/>

<Template: put name = 'content' content = '/introduction.html'/>

<Template: put name = 'footer 'content = '/footer.html'/>

</Template: insert>

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

<Table>

<Tr>

<Td> </td>

<Td> </td>

</Tr>

</Table>

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 does not use general starting HTML tags (such as

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' %>

......

<Table width = '000000'>

<Tr> <td width = '60'> </td>

<Td> <template: get name = 'header'/> </td> </tr>

<Tr> <td width = '60'> </td>

<Td> <template: get name = 'main-content'/> </td> </tr>

<Tr> <td width = '60'> </td>

<Td> <template: get name = 'editpanel '/> </td> </tr>

<Tr> <td width = '60'> </td>

<Td> <template: get name = 'footer '/> </td> </tr>

</Table>

......

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' %>

<Template: inserttemplate = '/template. jsp'>

......

<Template: put name = 'editpanel'

Content = '/editPanelContent. jsp'/>

......

</Template: insert>

Compared with the above, the login page does not have content specifically used to edit the Panel:

<% @ Taglib uri = 'template. tld 'prefix = 'template' %>

<Template: inserttemplate = '/template. jsp'>

<Template: put name = 'title' content = 'login' direct = 'true'/>

<Template: put name = 'header' content = '/header. jsp'/>

<Template: put name = 'main-Content'

Content = '/login. jsp'/>

<Template: put name = 'footer 'content = '/footer. jsp'/>

</Template: insert>

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)

In the templates shown in Figure 3. a and 3. B, the role attribute of template: get is used:

<% @ Taglib uri = 'template. tld 'prefix = 'template' %>

......

<Table>

......

<Td> <template: get name = 'editpanel 'role = 'curator'/> </td> </tr>

......

</Table>

......

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 );

}

}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.