Using struts to implement templates

Source: Internet
Author: User
Tags define definition contains copy implement include insert tomcat
It would be difficult for a template to develop a portal site without a framework. Using templates can reduce some of the hassle. He can make you blink. Update and deploy the content on the site easily. Use the Struts template tag to help you develop a template based portal site.
This article explains some basic template ideas related to portals, introduces struts support for templates, and discusses the struts template tags. This article also describes some of the other template mechanisms.

Get struts
The network application framework uses the MVC pattern to simplify the development of web sites. Struts is one such framework. It provides a class set, a tag library, and some interfaces that can be used as the basis for web development. Struts is an open source project under the Jakarta-apache Forum.

Like all Jakarta projects, struts provides downloads of the original code and binary programs. In this article, we use the 1.1b2 version of struts. Although this is not a formal version, this version has a number of significant improvements over the 1.0.2 version.

First, unzip the binaries to the directory you specified. There will be a directory created named JAKARTA-STRUTS-1.1-B2. Under it there are two subdirectories, respectively for Lib and WebApps. The Lib directory contains all the libraries, DTD files, TLD files (tag library definitions) We need, and we need to add them to the classpath of the application. The WebApps directory contains many. War (Web package) files. These installable running war files can be placed in the WebApps directory of any servlet container. We use Tomcat as a servlet container.

Template Basics
We can create a lot of things that are similar in structure through templates. Templates help us to archive things that will change, avoiding a lot of repetitive work. This is a really handy thing to create a Web application.

In most Web applications, it is often the content that needs to be replaced. JSP is easy to do this. Because the JSP defaults to provide an include mechanism--<jsp:include> tag. So a standard page can change its contents by changing the include file. But how can we change the layout of a page so easily? What happens when we need to change not just the content of the page, but also the layout of the page? We will answer these questions in a minute.

The layout management mechanism provided in Java can help us to understand these problems more visually. Java proposes a layout manager, component, container concept to create a graphical interface to the application.

When the components in the container change, the layout manager decides how to deploy the new components to the container. In addition, the container itself can be used as a component of other containers to achieve nesting. In some simple cases, the JSP can be used as a template for a Web application, and its function is equivalent to the layout manager. The template hides the way the Web page layout is implemented. In other words, it hides the rules of the layout. If the layout rules need to be changed, then these changes will be limited back to the template version. Web pages make changes in their content by implementing this template.

If the template is equivalent to the layout manager, what is the corresponding container and component? The corresponding component is the actual content file: JSP file, HTML file, image file and so on. The container gathers these visible components in one unit. A JSP file is a container. The container needs information about the template to deploy these components. With the Java GUI, for example, a panel (container, collection unit) can contain buttons and text areas (components and content), and use a Grid layout manager (GridLayout) to deploy these components.

Now we can answer the question that we raised earlier. We know that the template integrates the layout rules, and when we want to change the layout, we just change the template. These changes will be related to the layout of the page, without having to change the pages themselves. In a portal application, this means savings in time when a large number of pages require the same layout.

Struts uses three template tags to implement the template.

Inserts a label inserttag--<template:insert>.
Used in container/collection cell files (collecting unit file). It defines the template to use for the current container. Similar to the Container.setlayout (layout) method in the Java GUI. After it, define the component with the <template:put> tag.

Put the label puttag--<template:put>.
Used in container/collection cell files (collecting unit file). He defines the component/content that makes up the container/collection unit. It is similar to the Container.add (component) method in the Java GUI. Each Puttag label must have a name for the related component.

Get the label gettag--<template:get>.
Used in stencil files. It tells the template the names of the different components defined by the Puttag tag. It is similar to the layout.addlayoutcomponent (componentname, component) approach in the Java GUI.
A simple example

You can download this example. We're going to create two sets of content files, containers (collection unit) files, and template files. When we have these files, we can swap their templates and see how they change in layout.
Copy Struts-blank.war to Tomcat's WebApps directory and rename him to Template-example.war. Start Tomcat again. Template-example is automatically deployed by Tomcat. Deletes the pages directory and the index.jsp file. They have nothing to do with our discussion for the time being.

Let's define and define our own content files as a start. Under Template-example, create a directory named content with six files in it: top1.htm, top2.htm, bottom1.htm, bottom2.htm, middle1.jsp, and Middle2.jsp.
It's up to you to put in what file, but for beginners, static. htm files are easier to understand. For example, Top.htm includes the following:

This is the <b>TOP</b> Content for the <b>First</b>
Example. This is a static HTML file.



Similarly, middle1.jsp includes the following:

This is the <b>MIDDLE</b> Content for the <b>First</b>
Example. This is a dynamic JSP file and the
Current date and <%= new date ()%>



After defining the content, let's define two templates.
Under Template-example, create a directory named templates, under which two files template1.jsp and template2.jsp are created.
These templates define two layouts, as shown in the following illustration.
screen.width-430) this.width=screen.width-430 "Align=center border=0>
screen.width-430) this.width=screen.width-430 "Align=center border=0>

We define these two templates as follows:


<%@ taglib uri= '/web-inf/struts-
Template.tld ' prefix= ' template '%>
<title><template:get name= ' title '/>
</title>
<body>
<table border=1>
<tr>
<td><template:get name= ' top '/></td>
</tr>
<tr>
&LT;TD bgcolor= "#FF55FF" >
<template:get name= ' Middle '/></td>
</tr>
<tr>
<td><template:get name= ' Bottom '/></td>
</tr>
</table>
</body>
Definition Template 1






<%@ taglib uri= '/web-inf/struts-
Template.tld ' prefix= ' template '%>
<title><template:get name= ' title '/>
</title>
<body>
<table border=1>
<tr>
<td><template:get name= ' top '/></td>
&LT;TD bgcolor= "#FF55FF" >
<template:get name= ' Middle '/></td>
<td><template:get name= ' Bottom '/></td>
</tr>
</table>
</body>
Definition Template 2



Note that these two templates define two layouts, in other words, they define two different layout rules.
We already have our own content and templates. What we need now is the container file. Create two container files in the Tomcat-example directory named container1.jsp and container2.jsp respectively. The container file needs to contain the component (content). Similarly, the container file uses the template insert and put tags tags to define the components (content) that are used and added individually.

Let's take a look at one of these container files (container1.jsp) with these concepts.

<%@ taglib uri= '/web-inf/struts-template.tld ' prefix= ' template '%>
<template:insert template= '/templates/template1.jsp ' >
<template:put name= ' title ' content= ' Templates ' direct= ' true '/>
<template:put name= ' top ' content= '/content/top1.htm '/>
<template:put name= ' middle ' content= '/content/middle1.jsp '/>
<template:put name= ' bottom ' content= '/content/bottom1.htm '/>
</template:insert>




We can open the browser and enter http://localhost:8080/template-example/container1.jsp to see the actual output of this file. You will see the following page:
screen.width-430) this.width=screen.width-430 "Align=center border=0>
Later in this page, container1.jsp uses template template1.jsp to define the layout of the output page and to include content from different components. Note the usage of the attribute direct here <template:put name= ' title ' content= ' Templates ' direct= ' true '/>. When direct is set to true, the contents of the content label definition are called directly. It does not look for an external file. (When the ' direct is ' set to True, the content specified by the ' content ' is used directly. It isn't looked up in a external file. A little doubt, not a good turn.

Container2 similar, the only difference is that he uses template2, the page output is as follows:
screen.width-430) this.width=screen.width-430 "Align=center border=0 dypop=" Click here to browse the picture in the new window ">

The advantages of the work we have done to the present position are obvious. For example, we can replace template1 with Template2 (template2.jsp replace template2.js1 with simple copy and paste). Then container1.jsp will soon become like the next. We changed the layout of the container without touching the container1!. This is the role of templates.
screen.width-430) this.width=screen.width-430 "Align=center border=0 dypop=" Click here to browse the picture in the new window ">
Struts Template vs. Tiles

Starting with Struts 1.1, another template mechanism--tiles was introduced. Now it has become a major branch of the template mechanism. The tiles mechanism is defined in the Tiles network site and is compatible with the template label defined by struts. In fact, tiles is an extension set of template labels. So why do we have to tiles to replace the template?

Tiles allows us to pass parameters so that we have more control over the container. It's very useful when I want to make templates and containers have dynamic characteristics. In addition, tiles is more tightly integrated with the Struts action component (Struts action components).

However, tiles can make the code more complex. And it's more important that we take more time to master it.

Conclusion
Using templates makes it easy for you to manage your network applications. This article is the beginning of understanding the struts template. There is only a small part of the knowledge about the template, and a lot of knowledge, such as role-based (role-based) and optional (optional content), is to be learned on your own. I hope this article will inspire you to explore more about the characteristics of the template.



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.