The struts template method obtained from other places will be used later

Source: Internet
Author: User
Tags tld
Reprinted: Reprinted please keep this information, this article from http://www.matrix.org.cn/resource/article/0/701.html

Author: Vikram Goyal 11/20/2002
Original article: http://www.onjava.com/pub/a/onjava/2002/11/20/templatestruts.html
Translator: holexp
Email: lidaigang1@sina.com
QQ: 15761693
MSN: chenmulong@hotmail.com

It is very difficult to develop a portal without a framework. Using templates can reduce the trouble. It allows you to easily update and deploy content on the site in the twinkling of an eye. The struts template tag helps you develop a template-based portal site.
This article explains some basic template ideas related to portals, introduces struts's support for templates, and discusses struts template labels. This article also describes some other template mechanisms.

Obtain struts
The network application framework uses the MVC mode to simplify the development of network sites. Struts is such a 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 download of the original code and binary program. In this article, we use struts version 1.1b2. Although this is not a formal version, it has many significant improvements compared with version 1.0.2.

First, extract the binary program to the specified directory. There is a directory named jakarta-struts-1.1-b2 created. There are two subdirectories under it: Lib and webapps. The lib directory contains all the required libraries, DTD files, and TLD files (TAG library definitions). We need to add them to the classpath of the application. The webapps directory contains many. War (Web package) files. These war files that can be installed and run can be placed in the webapps directory of any servlet container. We use Tomcat as the servlet container.

Template Basics
We can use templates to create many similar structures. Templates help us archive and package the changed things to avoid a lot of repetitive work. This is indeed very convenient for creating network applications.

In most network applications, content is often replaced. JSP can easily achieve this. By default, JSP provides an include Mechanism -- <JSP: Include> label. Therefore, you can change the content of a standard page by changing the include file. But how can we easily change the layout of a page? What happens when we need to change not only the page content, but also the page layout? We will answer these questions later.

The layout management mechanism provided by Java can help us better understand these problems. Java introduces the concept of layout manager, components, and containers to create graphical applications.

When the components in the container change, the layout manager determines how to deploy the new components to the container. In addition, the container itself can also be used as a component of other containers to implement nesting. In some simple cases, jsp can be used as a template for a network application, and its functions are equivalent to layout manager. The template hides the implementation of web page layout. In other words, it hides the layout rule. If the layout rule needs to be changed, these changes will be limited by the template. The web page changes its content by implementing this template.

If the template is equivalent to the layout manager, what are the corresponding containers and components? The corresponding components are actual content files: JSP files, HTML files, image files, and so on. The container aggregates these visible components in a unit. A JSP file is a container. The container needs information about the template to deploy these components. For example, a panel (container, set unit) can contain buttons and widgets (components and content) and deploy these components using a grid layout manager (gridlayout.

Now we can answer the previous question. We know that templates integrate layout rules. To change the layout, we only need to change the template. These changes will change the page layout together without changing the page itself. In portal applications, this means saving time when a large number of pages need to adopt the same layout.

Struts uses three template tags to implement the template.

Insert tag inserttag -- <template: Insert>.
Used in the container/collection unit file (collecting unit file. It defines the template to be used by the current container. Similar to the container. setlayout (layout) method in Java GUI. Use the <template: Put> label to define the component.

Put the tag puttag -- <template: Put>.
Used in the container/collection unit file (collecting unit file. It defines components/contents that constitute the container/collection unit. It is similar to the container. Add (Component) method in the Java GUI. Each puttag tag must have a component name.

Get the tag gettag -- <template: Get>.
In the template file. It tells the template the names of different components defined by the puttag tag. It is similar to the layout. addlayoutcomponent (componentname, component) method in the Java GUI.
A simple example

You can download this example. We will create two sets of content files, container (Set Unit) files, and template files. When we have these files, we can swap their templates and see their layout changes.
Copy the Struts-blank.war to Tomcat's webapps directory and rename it to the template-example.war. Restart tomcat. Template-example is automatically deployed by Tomcat. Delete the pages directory and index. jsp file. They have nothing to do with our discussion.

Let's define and define our own content file as a start. Create a directory named content under template-example and create six files: top1.htm, top2.htm, bottom1.htm, bottom2.htm, middle1.jsp, and middle2.jsp.
The portable file is yours, but is easier to understand for beginners and static. HTM files. In my example, top.htm includes the following content:

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 content:

This is the <b>MIDDLE</b> Content for the <b>First</b> 
example. This is a dynamic JSP file and the
current date and time is <%= new Date() %>

After the content is defined, Let's define two templates.
Create a directory named templates under template-example, and create two files: template1.jsp and template2.jsp.
These templates define two la s, as shown in.

Let's define these two templates as follows:


<% @ Taglib uri = '/WEB-INF/Struts-
Template. TLD 'prefix = 'template' %>
<HTML>
<Head>
<Title> <template: Get name = 'title'/>
</Title>
</Head>
<Body>
<Table border = 1>
<Tr>
<TD> <template: Get name = 'top'/> </TD>
</Tr>
<Tr>
<TD bgcolor = "# ff55ff">
<Template: Get name = 'middle'/> </TD>
</Tr>
<Tr>
<TD> <template: Get name = 'bottom '/> </TD>
</Tr>
</Table>
</Body>
</Html>
Define TEMPLATE 1


<% @ Taglib uri = '/WEB-INF/Struts-
Template. TLD 'prefix = 'template' %>
<HTML>
<Head>
<Title> <template: Get name = 'title'/>
</Title>
</Head>
<Body>
<Table border = 1>
<Tr>
<TD> <template: Get name = 'top'/> </TD>
<TD bgcolor = "# ff55ff">
<Template: Get name = 'middle'/> </TD>
<TD> <template: Get name = 'bottom '/> </TD>
</Tr>
</Table>
</Body>
</Html>
Define template 2

Note that the two templates define two la S. In other words, they define two different la S.
We already have our own content and templates. Now we need a container file. Create two container files named container1.jsp and container2.jsp under the tomcat-example directory. The container file must contain components (content ). Similarly, the container file uses the template insert and put tags labels to define the components (content) to be used and added one by one ).

Let's take a look at a container file (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>

Open the browser and enter http: // localhost: 8080/template-example/container1.jsp to check the actual output of the file. You will see the following page:

At the end of the page, container1.jsp uses the template1.jsp template to define the layout of the output page and contain content from different components. Note the usage of attribute direct here <template: Put name = 'title' content = 'templates' direct = 'true'/>. When direct is set to true, the content defined by the content tag is called directly. It does not look for an external file. (When direct is set to true, the content specified by the content attribute is used directly. It is not looked up in an external file. It is a bit difficult to translate)

Container2 is similar. The difference is that it uses template2, and the page output is as follows:

The advantages of our current job are obvious. For example, we can replace template1 with template2 (replace template2.js1 with template2.jsp through simple copy and paste ). Then container1.jsp will immediately become the following. We changed the container layout but did not touch container1! This is the role of the template.

Struts template vs. Tiles

From 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 compatible with the template tag defined by struts. In fact, tiles is an extension set of template labels. So why should we use tiles to replace the template?

Tiles allows us to pass parameters so that we have more control over the container. It is useful when I want to make templates and containers have dynamic features. In addition, tiles is more closely integrated with Struts action components.

However, tiles makes the code more complex. What's more, we need to spend more time mastering it.

Conclusion
You can use templates to conveniently manage network applications. This article begins to understand the struts template. Here, I only introduced a few pieces of knowledge about templates. There are also a lot of Role-Based and optional Content-based knowledge that you can learn by yourself. I hope this article will inspire you to explore more about the features of templates.

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.