Struts2 form tag theme attributes

Source: Internet
Author: User
Http:// reprinted

In struts2, theme attributes include XHTML, HTML, simple, and Ajax. The default is XHTML.

Theme: Set the topic of the struts2 tag. The default value is XHTML. When theme = XHTML: Additional Tr and TD are generated by default. Theme = simple: generate the HTML tag format corresponding to the tag.

By default, form elements are distributed across different rows. The following code:
<S: Form Action = "login2"> <s: textfield label = "username" name = "username"/> <s: password label = "password" name = "password"/> <s: Submit label = "Submit"/> </S: Form>
We can see that the above Code is not much different from HTML, but because the struts2 form divides every element in the form into a single row by default, the label attribute is the same as the text label before <input type = "text"/> in HTML. If we don't want it to wrap automatically, we should write it in the following format:

<S: Form Action = "login2"Theme = "simple"> <S: textfield
Label = "username" name = "username"/> <s: Password
Label = "password" name = "password"/> <s: Submit label = "Submit"/> </S: Form>

HoweverTheme = "simple"The label attribute of the form element is invalid. In this case, you must add the text you want to display with the label before the form element. For example:

<S: Form Action = "login2"Theme = "simple"> User name: <s: textfield
Label = "username" name = "username"/> password: <s: Password
Label = "password" name = "password"/> <s: Submit label = "Submit"/> </S: Form>

In this case, the text in front of the text box is displayed instead of the value in the label. The button displays its original default value: sbumit.

The following is an extended knowledge:1. template directory-> topic (Template File) This is the template/topic directory organization method. Take the actual as an example, open the struts2-core-2.x.x.jar can see there is a template, under the template has five directories Ajax, css_xhtml, simple, XHTML and archive, the first four of them are Ajax, css_xhtml, simple, and XHTML.
Topic directory. Each topic directory contains its own template files, mainly FTL files, and CSS and JS files. The last archive is an archived topic directory, under which Ajax, simple, XHTML, and template files are. VM files. We can see that the template language strongly recommended by struts2 is freemarker rather than velocity. In the future, we should take a good look at freemarker and only know that freemarkder is more XML-based.
2. The template directory name is template, which is in the struts2-core-2.x.x.jar. In fact, the directory name is specified by the Struts. UI. templatedir constant, but the default value is template. This means that struts 2 loads specific template files from the template directory of the Web application or the template directory (package) of classpath in sequence.
3. for example, if we use a select tag and specify the subject as XHTML, the order of loading the template file is (1) web application/template/xhmlt/select. flt (2) classpath/template/XHTML/select. FTL. Struts2 uses freemarkder template technology by default. You can set constant struts. UI. templatesuffix to change the default template technology. Optional values include FTL, Vm, and JSP. However, you need to provide complete implementation for the VM and JSP. struts2 does not help you with this.
4. Sometimes we want to customize the theme. If you want the label in front of the input box to display a red color, you do not want to check the error message on the top of the input box but on the right. Of course, you can modify the template file of a topic under the template in the struts2-core-2.x.x.jar, or copy a copy to the template directory of the Web application directory to modify the template to be customized, this is always a bit uncomfortable. Struts2 also supports two more flexible theme customization methods. Package and inherit existing themes, which can be used at the same time. The simplest theme customization method is to take advantage of the theme template's loading priority, put the Custom template file in a directory with a higher priority, such as putting a text. FTL in the WEB-INF/classes/template/XHTML/
Directory that overwrites the template/XHTML/text. FTL definition in the struts2-core-2.x.x.jar.
5. let's first look at a packaging example in ComboBox under XHTML. the FTL content is as follows: <# include "/$ {parameters. templatedir}/$ {parameters. theme}/controlheader. FTL "/>

<# Include "/$ {parameters. templatedir}/simple/ComboBox. FTL "/> <# include"/$ {parameters. templatedir}/XHTML/controlfooter. FTL "/> <# NT/>. encapsulate a controlheader Based on the FTL. FTL and a controlfooter. FTL, the limitation of packaging is that you still need to provide a separate template file for every UI component, that is, a full set. Similar to the packaging (decoration) mode in the design mode, such
Bufferedinputstream encapsulates inputstream, but bufferedinputstream provides a full set of operation methods that are the same as inputstream.
6. inherit from custom themes. If you simply change the style of individual UIS, inheritance is the most efficient. This inheritance is also related to Java inheritance (extends. If you want to change or add a new topic, use your own definition. Otherwise, use the parent topic. For example, to customize a topic named custom to inherit from XHTML and only change the select label style, you must edit your own select. FTL put in WEB-INF/classes/template/custom, and put a file theme in this directory. properties, content: # specify the subject to be extended based on XHTML
Parent = xhml
You can specify the UI tag, for example, <s: form name = "AA" theme = "Custom "..., the result is that select in form uses the custom select in the Custom directory. FTL. Others directly use the template file in the parent-topic XHTML. The Ajax topic provided by struts2 is inherited from the XHTML topic.
7. Briefly introduce the built-in topics of struts2, including simple, XHTML, css_xhtml, and Ajax. The simple theme is needless to say. It is weaker than the HTML Tag of struts1. It only corresponds to simple HTML elements and does not generate additional content. XHTML is the default topic, which is the packaging and Extension (that is, inheritance) of the simple topic. Under this topic, there is a head. FTL used to import JavaScript class libraries (such as Dojo ). XHTML adds the following features based on simple: 1) HTML tags (such as textfield and select tags)
Use the standard two-column table layout. 2) Each HTML tag has the label attribute. The label attribute is displayed on the left by default. You can use the labelposition attribute to set the location. 3) Automatic output of background validation errors or Javascript front-end validation errors.
8. Continue to the topic of the built-in topic of struts2. The css_xhtml topic is an extension of XHTML, and the display is added with the CSS style control feature. The Ajax topic is an extension of the XHTML topic. Ajax support (based on djoj and DWR) is added for each tag of the XHTML topic ). The added Ajax features include: 1) Client verification in Ajax Mode 2) asynchronous submission of remote forms 3) Advanced Div tag, partial update allowed 4) advanced a tag, allows dynamic loading and execution of remote JavaScript code. 5) provides tabbedpanel supporting Ajax
6) provides the pub-sub event model of the "rich client" model.
9. Some netizens have asked me how to use struts2 for verification, but the error output is above the input box, but I hope the error message is displayed on the right of the input box. At that time, I only told him to modify the template file, and probably told him that it was an FTL file under a template directory, because I was not quite clear about the specific operations at that time. Now I know the clues, but the actual modification is still very troublesome. Input box <s: textfield.../> the default is to use the XHTML/text. Flt template, text. FTL wrapped smple/text. FTL, error information can be traced found in the controlheader-core.ftl
So you can set xthml/text. FTL and controlheader-core.ftl are copied to the WEB-INF/classes/template/XHTML directory for modification, template files in WEB-INF/classes/template/XHTML are prior to template file loading in the template/XHTML directory in the struts2-core-2.x.x.jar.
10. in some cases, it is generally described as follows: All form elements have a special attribute: Form, which references the form where the element is located. Through this attribute, form elements can interact with single tables, for example, you can use $ {parameters. form. id} ID of the access form. I just felt confused about this sentence. view it like <s: textfield... /> and other labels do not have the form attribute. Use <s: textfield value = "$ {parameters. form. id} "name =" AA "/> cannot see the ID of the output form. When I look online, I can see that the previous sentence is different. In fact, it is about the usage in the theme template file. Open some theme template files, such
The ComboBox. FTL or controlheader-core.ftl file allows you to see a lot of parameters representations-parameters. required, parameters. Id. Let's imagine that the parameters attribute is fundamental. It represents the attribute set of the form element. parameters also has points like this, this. ID, this. Form. ID, this. required, and so on...

When developing a project using struts2, it finds that it always adds some HTML tags to the final HTML code, for example, using the <s: Form> </S: Form> tag, the generated HTML code is as follows:

<Form...> <Table class = "wwformtable"> </table> </form>
The blue highlighted part is automatically generated by struts. It is sometimes useful for these automatically generated items, but it is not suitable for some projects. For example, we cannot add table labels to all pages, or we need to use other style sheets and so on, so we do not want to generate these extra labels.

So how can we prevent struts2 from generating these labels? In fact, in the struts-core-2.06.jar package contains some default template files, they are located under $ {struts-core-2.06.jar}/template, which has Ajax, simple, XHTML and so on.

View struts. properties, if not, you can view $ {struts-core-2.06.jar}/org/Apache/struts2/default. properties file, which has the following configuration: struts. UI. theme = XHTML struts. UI. templatedir = template struts. UI. templatesuffix = FTL

This section describes the struts2 template configuration. You can modify struts. properties file and change it to struts. UI. theme = simple struts. UI. templatedir = template struts. UI. templatesuffix = FTL

If there is no struts. properties file, you can modify the Struts. xml file and add the following lines to it:
<Constant name = "struts. UI. theme "value =" simple "/> <constant name =" struts. UI. templatedir "value =" template "/> <constant name =" struts. UI. templatesuffix "value =" FTL "/> at this time, the HTML code generated by the <s: Form> </S: Form> tag will be <form...> </form>.

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