JSP template application Guide (top) _jsp programming

Source: Internet
Author: User
Tags tld
The Window Toolkit provides a typical layout mechanism, such as determining the location of a part element in a container. There are layout managers in both AWT and swing, and wrapper in VisualWorks Smalltalk. This article introduces a JSP template mechanism that allows the layout to be encapsulated and recycled. The JSP template minimizes the impact of layout changes, and here we encourage you to adopt a modular design of encapsulation.

Although web development tools have improved very quickly, 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 that allows layout operations to be encapsulated and recycled in one form or another form. The JSP template mechanism described in this article, like a layout manager, encapsulates the layout so it can be reused rather than replicated.

As a result of a lot of changes in the layout of the development process, and the encapsulation of the function is a very important step, it can be easily modified to achieve the least impact on other applications.

JSP does not provide direct support for the encapsulated layout, so a Web page with a uniform format can usually copy the layout code; For example, in Figure 1, a page is displayed that contains the title, footer, toolbar, and the main contents of the page.


Figure 1. Page Layout click to enlarge (KB)

The page layout shown in Figure 1 will be performed as HTML table tags:

Example 1. Contains content:


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

<table>

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

<td><table>

<tr><td><% @include file= ' 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 example above, the JSP include command is included, which allows the page content to change-by changing the contained file-without having to modify the page itself. However, because the layout is difficult to encode, layout changes need to modify the page. If a Web site has more than one page in the same format, the general situation or even a simple layout change involves the entire page modification.

To reduce the impact of layout changes, we need a mechanism that includes only the layout, and the layout and content can be modified separately without modifying the file. This mechanism is the JSP template.

Working with templates
A template is a JSP file that contains parameterized content. The template discussed here uses a set of custom tags to perform: Template:get,template:put and Template:insert. The Template:get tag accesses the parameterized content, as in example 2.a, and it generates the Web page in the same format as Figure 1.

Example 2.a. A template

<% @taglib uri= '/web-inf/tlds/template.tld ' prefix= ' template '%>


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

<table>

<tr valign= ' top ' ><td><template:get name= ' sidebar '/></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 identical to Example 1, but in example 2.a we used Template:get to replace the include command in Example 1. Let's analyze how Template:get works.

Template:get uses a special name (within the scope of the request) to modify a Java bean. The bean contains the URI (Uniform Resource designator, a component of the Web page, which is contained in Template:get). For example, in the template list for example 2.a, Template:get obtains a uri--header.html--from a bean named header (within the scope of the request). The header.html is then included in the Template:get.

Template:put The bean to the requested range (this range will be template:get modified later). The template is included in the Template:insert. Example 2.b illustrates the use of put and insert labels:

Example 2.b. Using templates from Example 2.a

<% @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= ' sidebar ' content= '/sidebar.jsp '/>

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

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

</template:insert>

The Insert opening tag specifies the included template, in this case the template is in the example 2.a. Each put label stores a bean within the request range, and the label at insert end contains the template. The template then accesses the bean as described above.

The properties of direct can be specified for template:put, and if direct is set to true, the content associated with the label will not be included in the Template:get.

A Web site contains multiple pages of the same format, so you can use a template, such as a template in Example 2.a, which is used in many JSP pages (example 2.b).

Another benefit of using templates is that you can design them in a modular format. For example, the JSP file listed in example 2.b contains header.html, so let's take a look at the example 2.c below.

Example 2.c. header.html

<table>

<tr>

<td></td>

<td></td>

</tr>

</table>
Because Header.html is a contained content, it does not have to copy its code in a page that needs to display headers. Also, although header.html is an HTML file, the usual starting HTML tags (such as
Note: JSP provides two ways to include content: statically, using the include command, and dynamically using the include action. The include command contains the reference source for the target page, which is similar to the #include in C and the import in Java. The include action contains the response generated by the target during run time.

Like the JSP include action, the template contains dynamic content. So, while the JSP pages in Example 1 and 2.b are functionally consistent, the static content that is contained in the previous section is included in the following dynamic.

Optional content
All of the template content is optional and the content of the template can be easily used in more pages. For example, two pages--logins and listings--are shown in Figure 2.a and figure 2.B--they use the same template. All two pages contain a header, footer, and main content. The list page has an edit panel (which is missing from the landing page) to change the list.


Figure 2.a. Click to enlarge a landing window (KB)


Figure 2.B. A list page click to enlarge (KB)

Below, you will find that the template will be shared by login and List page:

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

......

<table width= ' 670 ' >

&LT;TR&GT;&LT;TD width= ' ></td>

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

&LT;TR&GT;&LT;TD width= ' ></td>

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

&LT;TR&GT;&LT;TD width= ' ></td>

<td><template:get name= ' Editpanel '/></td></tr>

&LT;TR&GT;&LT;TD width= ' ></td>

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

</table>

......

The list page uses the template above and the content specifically for editing 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>

In contrast to the above, the login page is not specifically for editing 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 login page is not included because it is not specifically designed to edit the panel.

The content based on role
Web applications often generate different content based on different users. For example, the same JSP template appears only when the user is an administrator, and the following are two different pages (Figure 3.a and 3.b.)


Figure 3.a. Administrator's list page click to enlarge (MB)


Figure 3.b. Other user's list page click to enlarge (KB)

The templates in figures 3.a and 3.b use the Template:get role attribute:

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

......

<table>

......

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

......

</table>

......

Get tags contain content only when the user's role matches the role attribute. 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.