220 likes | 430 Views
Struts Framework. Tag Libraries. STRUTS Tag Libraries. HTML Tags Bean Tags Logic Tags Template Tags Custom Tags. HTML Tags.
E N D
STRUTS Tag Libraries • HTML Tags • Bean Tags • Logic Tags • Template Tags • Custom Tags
HTML Tags • The tags in the Struts HTML library form a bridge between a JSP view and the other components of a Web application. Since a dynamic Web application often depends on gathering data from a user, input forms play an important role in the Struts framework. Consequently, the majority of the HTML tags involve HTML forms. Other important issues addressed by the Struts-HTML tags are messages, error messages, hyperlinking and internationalization.
HTML "form" tags button cancel checkboxes file hidden image multibox password input fields radio buttons reset buttons HTML "form" tags select lists with embedded option options submit buttons text input fields textareas HTML Tags
HTML Tags – Typical HTML Form <HTML> <BODY> <FORM> <TABLE WIDTH="100%"> <TR><TD>First Name</TD> <TD><INPUT TYPE="TEXT" NAME="Name" SIZE="40" MAXLENGTH="40"></TD></TR> <TR><TD>Street Address</TD> <TD><INPUT TYPE="TEXT" NAME="Address" SIZE="40" MAXLENGTH="40"></TD></TR> <TR><TD>City</TD> <TD><INPUT TYPE="TEXT" NAME="City" SIZE="20" MAXLENGTH="20"></TD></TR> <TR><TD>State</TD> <TD><INPUT TYPE="TEXT" NAME="State" SIZE="2" MAXLENGTH="2"></TD></TR> <TR><TD>Postal Code</TD> <TD><INPUT TYPE="TEXT" NAME="ZipCode" SIZE="9" MAXLENGTH="9"></TD></TR> <TR><TD ALIGN=CENTER><INPUT TYPE="SUBMIT" NAME="Submit" VALUE="Save"></TD> <TD><INPUT TYPE="RESET" NAME="Reset" VALUE="Cancel"></TD></TR> </TABLE> </FORM> </BODY> </HTML>
HTML Tags – Typical Struts Form <HTML:HTML> <BODY> <HTML:FORM Action="/CustomerForm" focus=“name” > <TABLE WIDTH="100%"> <TR><TD><bean:message key="customer.name"/></TD> <TD><HTML:TEXT property="name" size="40" maxlength="40" /></TD></TR> <TR><TD><bean:message key="customer.address"/></TD> <TD><HTML:TEXT property="address" size ="40" maxlength ="40" /></TD></TR> <TR><TD><bean:message key="customer.city"/></TD> <TD><HTML:TEXT property="city" size ="20" maxlength ="20" /></TD></TR> <TR><TD><bean:message key="customer.state"/></TD> <TD><HTML:TEXT property="state" size ="2" maxlength ="2" /></TD></TR> <TR><TD><bean:message key="customer.zip"/></TD> <TD><HTML:TEXT property="zip" size ="9" maxlength ="9" /></TD></TR> <TR><TD ALIGN=CENTER><html:submit property="action" value ="Save"/></TD> <TD><html:reset property="action" value ="Reset"/></TD></TR> </TABLE> </HTML:FORM> </BODY> </HTML:HTML>
Bean Tags • The "struts-bean" tag library provides substantial enhancements to the basic capability provided by <jsp:useBean>, as discussed in the following sections: • Bean Properties - Extended syntax to refer to JavaBean properties with simple names (same as the standard JSP tags <jsp:getProperty> and <jsp:setProperty>), nested names (a property named address.city returns the value retrieved by the Java expression getAddress().getCity()), and indexed names (a property named address[3] retrieves the fourth address from the indexed "address" property of a bean). • Bean Creation - New JSP beans, in any scope, can be created from a variety of objects and APIs associated with the current request, or with the servlet container in which this page is running. • Bean Output - Supports the rendering of textual output from a bean (or bean property), which will be included in the response being created by your JSP page.
Tag Name Description cookie Define a scripting variable based on the value(s) of the specified request cookie. define Define a scripting variable based on the value(s) of the specified bean property. header Define a scripting variable based on the value(s) of the specified request header. include Load the response from a dynamic application request and make it available as a bean. message Render an internationalized message string to the response. page Expose a specified item from the page context as a bean. parameter Define a scripting variable based on the value(s) of the specified request parameter. resource Load a web application resource and make it available as a bean. size Define a bean containing the number of elements in a Collection or Map. struts Expose a named Struts internal configuration object as a bean. write Render the value of the specified bean property to the current JspWriter. Bean Tags
Bean Tag Example <table border="2"> <tr> <th align="left"><bean:message key=“imagebroker.lob”/></th> <th align="left"><bean:message key=“imagebroker.unitnbr”/></th> <th align="left"><bean:message key=“imagebroker.onbase_dns”/></th> </tr> <logic:iterate id="image" property="collection" name="ImageLocationListForm"> <tr> <td><a href="ImageLocationListForm.do?lob=<bean:write name='image' property='lob'/> &unitnbr=<bean:write name='image' property='unitnbr'/> &onbase_dns=<bean:write name='image' property='onbase_dns'/>" > <bean:write name="image" property="lob"/></a></td> <td><bean:write name="image" property="unitnbr"/></td> <td><bean:write name="image" property="onbase_dns"/></td> </tr> </logic:iterate> </table>
Logic Tags • The Logic tag library contains tags that are useful in managing conditional generation of output text, looping over object collections for repetitive generation of output text, and application flow management.
Logic Tags • For tags that do value comparisons (equal, greaterEqual, greaterThan, lessEqual, lessThan, notEqual), the following rules apply: • The specified value is examined. If it can be converted successfully to a double or a long, it is assumed that the ultimate comparison will be numeric (either floating point or integer). Otherwise, a String comparison will be performed. • The variable to be compared to is retrieved, based on the selector attribute(s) (cookie, header, name, parameter, property) present on this tag. It will be converted to the appropriate type for the comparison, as determined above. • A request time exception will be thrown if the specified variable cannot be retrieved, or has a null value. • The specific comparison for this tag will be performed, and the nested body content of this tag will be evaluated if the comparison returns a true result.
Logic Tags • For tags that do substring matching (match, notMatch), the following rules apply: • The specified variable is retrieved, based on the selector attribute(s) (cookie, header, name, parameter, property) present on this tag. The variable is converted to a String, if necessary. • A request time exception will be thrown if the specified variable cannot be retrieved, or has a null value. • The specified value is checked for existence as a substring of the variable, in the position specified by the location attribute, as follows: at the beginning (if location is set to start), at the end (if location is set to end), or anywhere (if location is not specified).
Tag Name Description empty Evaluate the nested body content of this tag if the requested variable is either null or an empty string. equal Evaluate the nested body content of this tag if the requested variable is equal to the specified value. forward Forward control to the page specified by the specified ActionForward entry. greaterEqual Evaluate the nested body content of this tag if requested variable is greater than or equal to specified value. greaterThan Evaluate the nested body content of this tag if the requested variable is greater than the specified value. iterate Repeat the nested body content of this tag over a specified collection. lessEqual Evaluate the nested body content of this tag if requested variable is greater than or equal to specified value. lessThan Evaluate the nested body content of this tag if the requested variable is less than the specified value. match Evaluate the nested body content of this tag if specified value is an appropriate substring of requested variable. messagesNotPresent Generate the nested body content of this tag if the specified message is not present in this request. messagesPresent Generate the nested body content of this tag if the specified message is present in this request. notEmpty Evaluate the nested body content of this tag if the requested variable is neither null nor an empty string. notEqual Evaluate the nested body content of this tag if the requested variable is not equal to the specified value. notMatch Evaluate the nested body content of tag if specified value not an appropriate substring of requested variable. notPresent Generate the nested body content of this tag if the specified value is not present in this request. present Generate the nested body content of this tag if the specified value is present in this request. redirect Render an HTTP Redirect Logic Tags
Logic Tags - Example <html:html> <head> <title><bean:message key="imagebrokerlink.title"/></title> <META name="GENERATOR" content="IBM WebSphere Studio"> </head> <body> <html:form action="/ImageLocationForm" > <center> <font size=3> <br> <b> <logic:notEqual property="action" name="ImageLocationForm" value="Insert"> <bean:message key="imagelocationdetail.title"/> </logic:notEqual> <logic:equal property="action" name="ImageLocationForm" value="Insert"> <bean:message key="imagelocationinsert.title"/> </logic:equal> … …
Template Tags • The Template tag library contains three tags: put, get, and insert. Put tags put content into request scope, which is retrieved by a get tag in a different JSP page (the template). That template is included with the insert tag.
Template Tags • Insert Inserts (includes, actually) a template. Templates are JSP pages that include parameterized content. That content comes from put tags that are children of insert tags. • Put Puts content into request scope. • Get Gets the content from request scope that was put there by a put tag.
Custom Tags <%@ taglib uri="WEB-INF/imagebroker.tld" prefix="broker" %> <table width=750 cellspacing=0 cellpadding=2 border=2 > <tr> <td><broker:form lob='<%=test.getLob()%>' unitnbr='<%=test.getUnitnbr()%>' userid='<%=test.getUserid()%>' > <broker:doctype value="Invoice"/> <broker:keyword name="CompanyNbr" value="55555"/> <broker:keyword name="PONbr" value="M12345"/> <broker:constraint name="FromDate" value="02/02/2002"/> <broker:constraint name="ToDate" value="02/28/2002"/> Image Broker Link Test </broker:form> </td> </tr> </table>
Custom Tags – tld File <tag> <name>doctype</name> <tagclass>com.pri.brokertag.ImageBrokerDoctype</tagclass> <attribute> <name>value</name> <required>true</required> <rtexprvalue>true</rtexprvalue> </attribute> </tag>
Custom Tags – Tag Class public class ImageBrokerDoctype extends TagSupport { private String value = null; public int doStartTag() throws JspException { Hashtable ht = null; String keyword_count = null; int iCnt = 0; HttpServletRequest request = (HttpServletRequest) pageContext.getRequest(); ht = (Hashtable) request.getAttribute("keyword_parms"); keyword_count = (String) request.getAttribute("queryobject_count"); iCnt ++; ht.put("QueryObject" + iCnt, value); request.setAttribute("keyword_parms", ht); request.setAttribute("queryobject_count", new String(new Integer(iCnt).toString())); return EVAL_PAGE; } }