STRUTS2 Official Tutorial Struts tags (eight)--ui tags (Themes and Templates)

Source: Internet
Author: User
Tags documentation html tags

UI tags are tags used to generate a web interface, or to provide some support for a web interface, such as: form labels are the data that needs to be presented in various ways, and are presented to the interface in the form of dynamically generated HTML.

UI tags are divided into the following categories: Form Tags: forms tags, including all the tags that can be used in form forms Non-form UI Tags: non-form tags, mainly include error display, components and other Ajax tags: to support ajax tags, This has a special chapter to learn in the back.

8.4.2 Templates and Themes

Before entering into a specific UI tag, it is important to understand the concepts of themes and templates, which are essential for using STRUTS2 UI tags, because the presentation and implementation of STRUTS2 UI tags is based on themes and templates.

1: Templates (template)

The so-called template, that is, some code, in the STRUTS2 is usually written in freemarker, tags using the code can be rendered to generate the corresponding HTML code.

A tag needs to know what data it is showing, and what kind of HTML code it will eventually generate. Among them, what data is displayed is often ognl by the user-specified value stack, and the resulting HTML code is defined by a set of Freemarker templates, each of which will have its own corresponding Freemarker template. This set of templates is in the template package for the STRUTS2 Core jar package (Struts2-core-2.1.8.1.jar).

2: Theme (Theme)

The so-called theme is a collection of a series of templates. Typically, this series of templates will have the same or similar style, which guarantees the consistency of functionality or visual effects.

As mentioned earlier, the STRUTS2 tag uses a template to generate the final HTML code, which means that if you use a different template, the HTML code generated by the same tag will not be the same, meaning that the style of the HTML code generated by the different tags may not be the same.

This is a hassle, a page will use a lot of tags, if each tag generated by the HTML code style is not the same, the page will be very messy, then how to unify the function or style of the multiple tags.

The answer is naturally a theme, and each theme contains a series of templates that will have the same or similar styles to solve the problem. This also means that in Struts2, you can toggle the HTML style of the tag by toggling the theme. Template Loading

Templates AE loaded first by searching the application and then by searching the classpath. If a template needs to being overridden, an edited copy can being placed in the application, so it is found first.

The loading of the theme will search the application first, and then it will be searched under Classpath.

One for all
Freemarker is the default templating engine. The Freemarker templates is used regardless of what format the view is use. Internally, the JSP, FTL, Velocity tags is all rendered using Freemarker.

Freemarker is the default template engine, and Freemarker templates can be used regardless of the possible view display.

Template and Themes

Templates is loaded based the template directory and theme name (see Selecting Themes). The template directory is defined by the Struts.ui.templateDir property in Struts.properties (defaults to template). If a tag is using the Ajax theme, the following and locations'll be searched (in this order):

Template loading is based on the template path and name, and the path is defined in Struts.ui.templateDir.

In the application /template/ajax/template.ftl
In the Classpath /template/ajax/template.ftl
Prioritize the first path based on efficiency considerations

For performance reasons, your may want to prefer the first location, although the second one are more flexible. See performance Tuning for a discussion in this topic. overriding Templates

The default templates provided in the Struts-core.jar should suit the needs of many applications. However, if a template needs to be modified, it's easy to plug in a new version. Extract the template you need to change from the Struts-core.jar, make the modifications, and save the updated copy To/te mplate/$theme/$template. FTL. If you is using the XHMTL theme and need to change how the select tags render, edit that template and save it To/templat E/XHTML/SELECT.FTL.

It is easier and better to edit and the override an existing template than provide a new one of your own.

The default templates provided in Struts-core.jar should apply to the needs of most applications. However, if a template needs to be modified, it should be easy to insert a new version from Struts-core.jar to find the template you want to modify, modify, then save, get to/template/$theme/$template. FTL. altering Template Loading Behavior

It is possible to load template from other locations, like the file system or a URL. Loading templates from alternate locations can is useful not only for tags, but for custom results. For details, see the Freemarker documentation and consult the sections on extending the Freemarkermanager.

You might load a template from somewhere else, such as a file system or a URL. Loading templates from other places not only allows you to use labels, but also customize the results, see Freemarker documentation for details. Alternative Template Engines

The framework provides for template rendering engines and other than Freemarker. (Though, there is rarely a need to use another system!)

Don ' t try this at home!
Alternative template engines is best left-to-advanced users with special needs!

The framework supports three template engines, which can be controlled by the Struts.ui.templateSuffix in Struts.propertie S.

FTL (default) freemarker-based Template engine
Vm velocity-based Template engine
Jsp jsp-based Template engine

The only set of templates and themes provided in the distribution are for Freemarker. In order to use the another template engine, you must provide your own template and theme for that engine.

Stay the course
Don ' t feel that's need to rewrite, the templates to match your view format. If you need to customize the template, try copying and modifying the Freemarker template first. Most changes should is obvious.
Selecting Template Directory

Template directory can selected using several different rules, in this order:

Set Theme method: Priority is also the following order the Templatedir attribute on the specific tag the page-scoped attribute named Templatedir the request- Scoped attribute named Templatedir the session-scoped attribute named Templatedir the application-scoped attribute named T Emplatedir the Struts.ui.templateDir property in Struts.properties (defaults to template)

1. Specify a theme by setting the Tehme property on a specific UI label
2. Specify the topic
3 by setting the theme property of the form label on the periphery of a particular UI tag. The topic
4 is determined by the property that is named theme in the drive's page session scope. The topic
5 is determined by obtaining a property with the name theme in the request scope. The topic
6 is determined by obtaining a property with the name theme in the session range. Determine the subject by obtaining an attribute with the theme name in the application range
7. Determine the subject by obtaining a constant named Struts.ui.theme (the default is XHTML), which can be determined in the Struts.properties file or the Struts.xml file.

Tips to support user-selected "Shttp://confluence.twdata.org/pages/editpage.action?pageid =723#
Wiki markupkins, override the template directory in the user ' s session. To change the template directory for the entire application, modify the struts.properties.

Built-in themes:
Simple (default theme), XHTML, Css_xhtml, Ajax
XHTML, css_xhtml themes are wrappers and extensions to simple themes
Simple theme, each UI tag generates only a single HTML element, and no additional content is generated

The XHTML theme, STRUTSS's default theme, expands on simple, adding the following additional features to the base of the topic
1. Use standard two-column table layouts for HTML tags
2. The label of each HTML tag can appear either to the left of the HTML element or to the top, depending on the setting of the Lableposition property
3. Automatic output Check error message
4. Client-side validation of the output JavaScript


css_html CSS style controls on XHTML themes

AJAX provides additional AJAX support for each label on XHTML topics. Ajax support is based on dojo and DWR
Add the following features
1. Increase the AJAX mode of client-side validation
2. Support Asynchronous submission of remote forms (preferably with the submit tag)
3. Provide advanced div tags that allow for the ability to partially update HTML
4. Provide advanced a-tag, allowing dynamic loading and execution of remote JavaScript code
5. Provide AJAX-enabled Tabbedpanel
6. Pub-sub event model with rich client model

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.