If you don't use a frame to develop a portal site will be a very difficult thing. Use a template to reduce some trouble. He allows you to easily update and deploy the contents of the site between blinks. Use the Struts template label to help you develop a template-based portal site. This article explains some basic template ideas related to the portal, introducing Struts for template support, discussing around the Struts template label. This article also expounds some other template mechanisms. Get the Struts Network Application Framework uses MVC mode to simplify the development of network sites. Struts is this frame. It provides a class set, label library, and some interfaces that can be used as a web development. Struts is an open source project under the Jakarta-Apache forum. Like all Jakarta projects, Struts provides the download of the original code and binary. In this article, we use the Struts 1.1B2 version. Although this is not a formal version, this version has many major improvements compared to version 1.0.2. First, decompress the binary program to the directory you specify. There will be a directory called Jakarta-Struts-1.1-B2. There are two subdirectories below it, which are lib and webapps, respectively. The lib directory contains all ownership, DTD files, TLD files (label definitions), and we need to add them to the application's classpath. The webapps directory contains many .war (web packages) files. These installed WAR files can be placed in a webApps directory of any servlet container. We use Tomcat as a servlet container. Template Foundation We can create a lot of structurally similar things through a template. The template helps us will change the things that archive and avoid many duplicate work. This is a very convenient thing to create a web application. In most network applications, you often need to replace content. JSP is easy to do this. Because JSP provides an Include mechanism by default - the tab. Therefore, a standard page can change its content by changing the incrude file. But how can we change the layout of a page so convenient? What happens when we need to change not only the content of the page, but also what happens when the layout of this page is? Let's wait for these questions. The layout management mechanism provided by Java can help us more imperfectly understand these issues. Java proposes the concept of layout manager, component, and containers to create a graphical interface. When the components in the container changes, the Layout Manager will decide how to deploy new components into the container. In addition, the container itself can also be used as components of other containers to achieve nested. In some simple situations, JSP can be a template for a network application, and its feature is equivalent to the layout manager. The template hides the implementation method of the web page layout. In other words, it hides the rules of the layout. If the layout method needs to change, then these changes will return to the template. The web page is made by this template. What is the corresponding container and components in the template is equivalent to the layout manager? The corresponding components are actual content files: JSP files, HTML files, image files, and more. The container gathers these visible components in a unit. A JSP file is a container. The container needs to deploy these components regarding the formation of the template. With Java GUI, a panel (container, set unit) can include button and text area (components, and content), using a GridLayout Manager to deploy these components. Now we can answer the question previously proposed. We know that the template integrates the layout law, when you want to change the layout, we have to do only change the template. These changes will change together related page layout without changing these pages itself. In the portal application, this means that when a large number of pages need to be saved during the same layout. Struts uses three template labels to implement templates.
Insert Tag INSERTTAG - . Used in the container / set unit file (Collecting Unit File). It defines the templates to use in the current container. Similar to the Container.setLayout (layout) method in the Java GUI. Use the tab after it define the component. Put the label PUTTAG - . Used in the container / set unit file (Collecting Unit File). He defines components / content that make up the container / set unit. It is similar to the Container.Add (Component) method in the Java GUI. Each PUTTAG tag must have a name of a related component. Get tag gettag - . Use in the template file. It tells the template from the name of different components defined by the Puttag tag. It is similar to the Layout.AddlayoutComponent (ComponentName, Component) method in 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, you can exchange their templates and see their changes on the layout. Copy the Struts-Blank.war to Tomcat's WebApps directory and renamed him to Template-Example.war. Take the Tomcat. Template-Example will be automatically deployed by Tomcat. Delete the pages directory and index.jsp file. They have nothing to do with our discussion. Let us define the beginning of our own content file as a start. Create a directory named Content under Template-Example, and create six files in it: top1.htm, top2.htm, bottom1.htm, bottom2.htm, middle1.jsp, and middle2.jsp. What document puts it depends on you, but for beginners, static.htm files are easier to understand. For example, Top.HTM includes the following content: this is the TOP b> Content for the first b> example. This is a static html file.
Similarly, middle1.jsp includes the following:
This is the middle b> Content for the first b> esple. This is a dynamic jsp file and thecurrent date and time is <% = new date ()%>
After defining the content, let us define two templates.
Create a directory called Templates under Template-Example, and create two files template1.jsp and template2.jsp.
These templates define two layout methods, as shown below.
Note that these two templates define two layout methods, in other words, they define two different layout rules.
We already have our own content and template. Now we need a container file. Create two container files in Tomcat-Example Directory named Container1.jsp and Container2.jsp. The container file needs to include components (content). Similarly, the container file uses the Template INSERT and PUT TAGS tags to define the components (content) (content) (content) (content) (content).
Let us take these concepts to see a container file (Container1.jsp).
<% @ Taglib Uri = '/ Web-INF / STRUTS-TEMPLATE.TLD' Prefix = 'Template'%> template: insert> We can open your browser, enter
Http: // localhost: 8080 / template-esample / container1.jsp Take a look at the actual output of this file. You will see the following page:
Behind this page, Container1.jsp uses template template1.jsp to define the layout of the output page and contain content from different components. Note Here the properties Direct usage . When Direct is set to True, the contents defined by the Content tag will be called directly. It doesn't look for an external file. (When Direct is set to true, the content specified by the content attribute is buyd directly. It is not looked up in an external file. Located, the turn is not good)
Container2 is similar, and the difference is just that he uses template2. The page output is as follows:
The advantage of our work to work at present is obvious. For example, we can replace Template1 with Template2 (replace Template2.js1 by simply replication and pasting with template2.jsp). The lack will become the following appearance immediately. We changed the layout of the container but did not touch Container1! This is the role of template.
Struts Template vs. Tiles
Starting from Struts 1.1, another template mechanism is introduced. It has now become a major branch of the template mechanism. The TILES mechanism defines in the Tiles network site and is compatible with the template label defined by Struts. In fact, Tiles is an extension of the template label. Then why do we have to tiles to replace the template?
Tiles allows us to deliver parameters to have more control over the container. It is very useful when I want to make the template and containers. In addition, Tiles combines Struts action components (Struts action components).
However, Tiles makes the code more complicated. And more importantly, we have to spend more time to master it.