Learn what struts provides and form-related labels

Source: Internet
Author: User
Learn what struts provides and form-related labels
Continue learning the HTML tag library provided by struts. by Budi Kurniawan
In the first part of this series, I describe how to configure a struts application using the HTML tag library provided by struts. I also introduced a category of tags in the tag library: standalone labels. In the second section I'll go on to the second category of tags: labels related to form. The labels associated with the form include the <form> label itself and all the labels that must be included in it. For example,,<text> and <password> tags are tags associated with form, because they don't make sense if they are not placed in a form.
<form> Label
<form> tags are used to generate an HTML form. You must follow many rules when using this label.
First, the,<form> tag must contain an action attribute, which is the only required property in the label. If this property is not available, the JSP page throws an exception. You must then specify a valid value for this action attribute. A valid value refers to the access path of any <action> child element in the <action-mappings> element of the application's struts configuration file. Also, the corresponding <action> element must have a Name property whose value is the form bean. For example, if you have such a <form> tag:
Then your struts configuration file must have a <action> element in the <action-mappings> element that appears as bold:
<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 label is associated with a form bean.
Another rule to follow is any label that is included in <form> to receive user input (<text>, <password>, In addition to the attributes mentioned above, the,<form> tag has some attributes that are not necessarily but plus better "minor". For example, you can use the focus attribute to generate JavaScript, which is "fixed" (focal) to an element contained in the form. You need to specify the name of the element when you use the Focus property. For example, the following code is fixed on the second text element:
<body>
User Name: <br>password: <br>
</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 property in the,<form> label is post. Also, have you seen how this tag library builds JavaScript to set the focus to the password element? This is one of the most fascinating places in the library. You don't have to worry about how to program on the client, it will help you generate it automatically.
When running the previous example, be aware that you must have a related form that contains the username and password attributes. You can refer to the login procedure in part three of this article.
<text> Label
<text> tags are used to generate a text input area. It must contain the property attribute corresponding to the same property in the associated form Bean. The label is valid only if it is embedded in a <form> label.
For example: will be converted to: <input type= "text" name= "UserName" "value=" ">
<password> Label
<password> tags are used to generate an input area for a password character (type password). It must contain the property attribute corresponding to the same property in the associated form Bean. The label is valid only if it is embedded in a <form> label. A very important attribute in the label is "Redisplay", which is used to display the values previously entered into this area. The default value for this property is true. However, in order for the password to be displayed again, you might want to set the value of this property to false.
For example: will be converted to: <input type= "password" name= "password" "value=" ">
For example: will be converted to: <input type= "hidden" name= "UserName" "value=" ">
<textarea> Label
The <textarea> label is used to generate a text AREA element. It must contain the property attribute corresponding to the same property in the associated form Bean.
For example: will be converted into: <textarea name= "Details" cols= "rows=" >enter details here</textarea>
<radio> Label
The <radio> tab is used to display a radio button (radio buttons). It must contain the "value" attribute. Like this code:
will be converted to HTML like this:
<input type= "Radio" name= "title" value= "1" >mr.
<input type= "Radio" name= "title" Value= "2" >ms.
<input type= "Radio" name= "title" Value= "3" &GT;DR.
<checkbox> Label
The <checkbox> label is used to display the input area for the checkbox type. Like what:
is converted to: <input type= "checkbox" Name= "Notify" value= "on" >please Send me notification
<submit> Label
The <submit> label is used to generate the input area for the commit type (type Submit). Like what:
will be converted into: <input type= "Submit" value= "Login" >
<reset> Label
The <reset> label is used to generate the input area for the reset type (type reset). Like what:
will be converted into:
<input type= "reset" value= "reset" >
<option> Label
The <option> label is used to display an option in select box. Refer to the <select> label below.
<select> Label
The <select> tab is used to display an HTML selection element that contains 0 or more options. It must be embedded in a <form> tag to be valid. The following example shows how to use the tag:

will be converted into:
<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've learned some more important and form-related labels. Before you can use these tags, you must follow some rules to use them correctly. The third section describes how to use these tags in a real program.


Bean Tag Library:
Before using this tag, first import the corresponding tag definition on the JSP page, for example
<%@ taglib uri= "/web-inf/struts-bean.tld" prefix= "Bean"%>.
The method used is like <bean:*** id= ""/>.
The following is an introduction to the tag library:
1. Cookie: The property has Id,name, Multiple,value,id is the unique identification of the cookie on the page, and can be referenced elsewhere on the page by ID. is equivalent to a direct reference with a cookie. Name is the appropriate cookie name. Multiple can give an arbitrary value, if the value is defined will return the corresponding number of a given value of the cookie array, if the value is not defined here is a default value, when we call the cookie is not present here, its value is our values here.
2. Define: The equivalent of JSP tag <jsp:usebean&gt, the attribute has ID, name, property, scope, Toscope, type, value. ID and the ID in the cookie play the same role; name is the unique bean identifier in the corresponding scope; the scope will be searched accordingly; Toscope if you cannot find the appropriate bean in scope, The corresponding bean;type in Toscope will be the complete classpath and class of the class.
For example:
<bean:define id= "SubmitForm" scope= "session" Name= "SubmitForm"
Type= "Struts.test.SubmitActionForm"/>
<%=submitform.getname ()%>

For example:
<jsp:usebean id= "test1" scope= "page"
class= "Org.apache.struts.webapp.exercise.TestBean"/>
<bean:define id= "test1_string" name= "test1" property= "Stringproperty"/>
<bean:define id= "Test1_value" value= "ABCDE"/>
<%= test1_string%>
<%= Test1_value%>

3. Header: This tag enables access to the header information for the corresponding page request, with ID, multiple, name, value. The exact meaning is similar to that of a cookie.
For example: <%java.util.enumeration names = ((httpservletrequest) request). Getheadernames ();%>
<%while (Names.hasmoreelements ()) {
String name = (string) names.nextelement ();
%>
<bean:header id= "Head" name= "<%= name%>"/>
<tr>
<td><%= name%></td>
<td><%= Head%></td>
</tr>
<%}%>
4. Include: property has ID, name, forward, anchor, href, transacttion, page. The description of the attribute refers to the corresponding Apache Web site.
For example: <bean:include id= "index" page= "/index.jsp"/>
<%=index%>
Message: Not used, here ignored.
Page: Not used, this is ignored here.
Parameter: Property has ID, name, value, multiple. The request parameters and values that are used to output the client.
For example: <bean:parameter id= "param1" name= "param1"/>
<bean:parameter id= "param3" name= "param3" value= "UNKNOWN value"/>
<%=param1%><br>
<%=param3%>
Resource: attribute has ID, name, input. If the input property is defined as any value, the imported resource will return a InputStream object. If not defined, the resource will be returned as a string.
For example: <bean:resource id= "Webxml" name= "/web-inf/web.xml"/>
<bean:write name= "Webxml" filter= "true"/>
Size: Used to count the number of elements in a cellection or map type of data. Attributes are: ID, collection, name, property, scope.
For example: <%
Java.util.ArrayList data = new Java.util.ArrayList ();
Data.add ("a");
Data.add ("Second");
Data.add ("third");
Data.add ("Fourth");
Data.add ("fifth");
Pagecontext.setattribute ("list", data, Pagecontext.page_scope);
%>
<bean:size id= "DataSize" collection= "<%= data%>"/>

<jsp:usebean id= "list" scope= "page" class= "Java.util.ArrayList"/>
<bean:size id= "listsize" name= "list"/>
Struts: Not used, this is ignored.
Write: Properties are bundle, filter, format, Formatkey, ignore, local, name, property, scope. Please refer to the relevant website for specific usage.
HTML Tag Library:
Use this tag library to first introduce a tag library definition file into the appropriate page, such as:
<%@ taglib uri= "/web-inf/struts-html.tld" prefix= "html"%>. In fact, most of the tags in the tag library can be implemented with HTML tags. The value here is an introduction to several tags that it cannot implement with HTML.
Error: Attributes are: Bundle, name, property, Local. If none of the above properties are defined, it will be displayed from all error.*** error messages in the property file. Bundle, a State error information manager, if not defined, defaults to the application-level error manager created by the action. The name and property are used to display an error message by calling the error information management bean from the appropriate page. Local is an error information localization processor, which by default handles localization in a localized manner.
The attribute resource file should contain the following information to format the reality of the error message:
Errors.header all error messages that are displayed earlier, HTML can be decorated
Errors.footer the display after all error messages, which can be HTML decorated
Errors.prefix the display information in front of each error message and can be HTML decorated
Errors.suffix the display information that follows each error message and can be HTML decorated
Message: The property has bundle, footer, header, ID, local, a, and name. Please refer to the relevant website for specific usage.
Logic Tag Library: Use this tag library to first introduce a tag library definition file into the appropriate page, such as: <%@ taglib uri= "/web-inf/struts-logic.tld" prefix= "logic"%&GT;
Empty: attribute has name, property, scope.
Equal: The attribute has a cookie, header, name, parameter, property, scope, value. where cookies and headers, parameter are used independently. Name is used in conjunction with property, scope, and is relative to the bean in the system. If it is a simple parameter in the system, it can be combined with name and scope.
Forward: Property has only name.
Greaterequal: Attributes are the same as equal.
GreaterThan: Attributes are the same as equal.
Lessequal: Attributes are the same as equal.
LessThan: Attributes are the same as equal.
Notempty: Attributes are the same as empty.
Match: Properties are the same as equal.
Notmatch: Attributes are the same as equal.
NotEqual: Attributes are the same as equal.
Notpresent: Attributes are Coolie, header, name, property, parameter, scope, role, user.
Present: The same as notpresent.
Messagenotpresent: attribute has message, name, property.
Messagepresent: The same as messagenotpresent.
Iterate: Property has collection, ID, INDEXID, scope, name, property, length, offset, type.


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.