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:
</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.
</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:
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:
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:
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.