Use of the HTML Tag library in struts (second-group tags, and form-related tags)

Source: Internet
Author: User
Continue to learn about the HTML Tag library provided by struts.
By Budi kurniawan
In the first part of this series, I will show you how to use the HTML Tag library provided by struts to configure a struts application. I also introduced a type of tag in the tag Library: Independent tags. In the second part, I will continue to introduce the second type of labels: The labels related to form.

The form-related labels include the labels themselves and all the labels that must be included in them. For example, tags are form-related labels, because they are meaningless if they are not put into a form.

Tag
The tag is used to generate an HTML form. Many rules must be followed when using this label.

First, the tag must contain an action attribute, which is the only required attribute in the tag. If this attribute is not available, the JSP page throws an exception. You must specify a valid value for this action attribute. A valid value is the access path of any child element in the struts configuration file of the application. The corresponding element must have a name attribute. Its value is the name of form bean.

For example, if you have a tag: <HTML: Form Action =/"/login/">
 

The following element must be in bold in your struts configuration file: <action-mappings>
<Action Path =/"/login /"
Type =/"com. javapro. Struts. loginaction /"
Name =/"loginform /"
Scope =/"Request /"
Input =/"/login. jsp/">
<Forward name =/"success/" Path =/"/mainmenu. jsp/"/>
</Action>
.
.
.
</Action-mappings>

This means that a form tag is associated with form bean.

Another rule to be followed is: any labels (<text>, <password>,

In addition to the attributes mentioned above, the <form> tag has some attributes that are not mandatory but are better "secondary. For example, you can use the focus attribute to generate JavaScript, which will "Focus" on an element contained in the form. You need to specify the element name for the focus attribute. For example, the following code is fixed on the second text element: <body>
<HTML: Form Action =/"/login/" Focus =/"password/">
User name: <HTML: Text property =/"username/"/>
<Br> password: <HTML: Text property =/"password/"/>
<Br> <HTML: Submit/>
</Html: Form>
</Body>

The code is converted to: <body>
<Form name =/"loginform/" method =/"post /"
Action =/"/mystrutsapp6/login. Do/">
User name: <input type =/"text/" name =/"username /"
Value =/"/">
<Br> password: <input type =/"text /"
Name =/"password/" value =/"/">
<Br> <input type =/"Submit /"
Value =/"Submit/">
</Form>
<Script language =/"javascript /"
Type =/"text/JavaScript/">
<! --
If (document. Forms [/"loginform/"]. elements [
/"Password/"]. type! =/"Hidden /")
Document. Forms [/"loginform/"]. elements [
/"Password/"]. Focus ()
// -->
</SCRIPT>

</Body>

Note: The default value of the method attribute in the <form> label is post. In addition, have you seen how this tag Library builds JavaScript to focus on the password element? This is also a fascinating part of the library. You don't have to worry about how to program on the client, it will automatically generate for you.

When running the preceding example, note that you must have a form that contains the username and password attributes.

<Text> label
<Text> label is used to generate an input area of text. It must contain the "property" attribute corresponding to the same attribute in the related form bean. This label is valid only when it is embedded into a <form> label.

Example: <HTML: Text property =/"username/"/>
Will be converted to: <input type =/"text/" name =/"username/" value =/"/">

<Password> tag
<Password> the tag is used to generate a type password input area. It must contain the "property" attribute corresponding to the same attribute in the related form bean. This label is valid only when it is embedded into a <form> label. A very important attribute in this label is "redisplay", which is used to re-display the value previously entered in this area. The default value of this attribute is true. However, to prevent the password from being re-displayed, you may want to set the value of this attribute to false.

Example: <HTML: Password property =/"password /"
Redisplay =/"false/"/>
Will be converted to: <input type =/"password/" name =/"password /"
Value =/"/">

<Hidden> tag
<Hidden> the tag is used to generate an input area for hidden text. It must contain the "property" attribute corresponding to the same attribute in the related form bean. This label is valid only when it is embedded into a <form> label:

Example: <HTML: hidden property =/"username/"/>

Will be converted to: <input type =/"hidden/" name =/"username/" value =/"/">

<Textarea> label
<Textarea> A tag is used to generate a text area element ). It must contain the "property" attribute corresponding to the same attribute in the related form bean.

For example: <HTML: textarea property =/"details /"
Cols =/"80 /"
Rows =/"20 /"
Value =/"enter details here/"/>
Will be converted to: <textarea name =/"details/" Cols =/"80 /"
Rows =/"20/"> enter details here </textarea>

<Radio> label
<Radio> the tag is used to display a single-choice button ). It must contain the "value" attribute. For example: <HTML: Radio property =/"title/" value =/"1/"/> mr.
<HTML: Radio property =/"title/" value =/"2/"/> MS.
<HTML: Radio property =/"title/" value =/"3/"/> dr.

HTML: <input type =/"Radio/" name =/"title /"
Value =/"1/"> mr.
<Input type =/"Radio/" name =/"title /"
Value =/"2/"> MS.
<Input type =/"Radio/" name =/"title /"
Value =/"3/"> dr.
<Checkbox> label
<Checkbox> the label is used to display the checkbox type input area. For example: <HTML: checkbox property =
/"Sort y/"/> Please send me notification
Will be converted to: <input type =/"checkbox/" name =/"Y /"
Value =/"on/"> Please send me notification

<Submit> label
<Submit> the label is used to generate the input area of the type submit. For example: <HTML: Submit value =/"login/"/>
Will be converted to: <input type =/"Submit/" value =/"login/">

<Reset> tag
<Reset> A tag is used to generate an input area of the reset type (Type reset. For example: <HTML: reset/>
Will be converted to: <input type =/"Reset/" value =/"Reset/">

<Option> label
<Option> A label is used to display an option in select box. Refer to the <SELECT> label below.

<SELECT> label
<SELECT> A tag is used to display an HTML selection element that contains zero or more options. It is valid only when it is embedded with a <form> label. The following example shows how to use the tag: <HTML: Select Property =/"color/" size =/"3/">
<HTML: option value =
/"R/"> Red <HTML: option value =
/"G/"> green <HTML: option value =
/"B/"> blue </Html: Select>

Will be converted to: <select name =/"color/" size =/"3/"> <Option
Value =/"r/"> Red </option>
<Option value =/"g/"> green </option>
<Option value =/"B/"> blue </option>
</SELECT>

In the second part of this series, you have learned some more important form-related labels. Before using these tags, you must follow some rules to use them correctly. The third part describes how to use these labels in a real program.

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.