Struts Tag Libraries For Web Applications

Introduction

 
Struts framework provides a set of tag libraries that interacts with the rest of the framework.
 
Struts support three core libraries known as,
  • HTML Tag Library
  • Bean Tag Library
  • Logic Tag Library.
Tags within a particular library perform similar functions. Struts tags are like any other JSP customs tags and before a tag is used, the TLD (Top-Level-Domain) description of each tag library should be included. For example, to use the Logic tag libraries in a JSP page the following line should be written at the top of the JSP page,
 
<%@ taglib uri=”/WEB-INF/struts-logic.tld” prefix=”logic” %>
 
After these lines have been added in the JSP page, the web application deployment descriptor should also contain the required taglib elements.
 
The following lines should be added in the web.xml file
  1. <taglib>  
  2.    <taglib-uri>/WEB-INF/struts-logic.tld</taglib-uri>  
  3.    <taglib-location>/WEB-INF/struts-logic.tld</taglib-location>  
  4. </taglib>   
The .tld file should be placed in the WEB-INF directory
 

HTML Tag Library

 
The HTML tag libraries instead of HTML tags can be used to create forms that are bound to the Struts Framework. The tags in this library work closely with the components of the Struts framework. The tags present under HTML Tag Library are shown as,
  • base
    It generates an HTML <base> tag with its href attributes set to the absolute URL of the enclosing JSP page.
  • button
    It generates an HTML <input type=”button”> tag
  • cancel
    It generates an HTML <input type=”submit”> tag , that when executed triggers Cancel features of Struts.
  • checkbox
    It generates an HTML <input type=”checkbox”> tag.
  • errors
    It conditionally renders a set of accumulated error messages.
  • file
    It generates an HTML <input type=”file”>
  • form
    It generates an HTML <form> tag tied to an Action object and its corresponding Form Bean.
  • frame
    It renders an HTML <frame> tag
  • hidden
    It generates an HTML<input type=”hidden”> tag.
  • html
    It renders an <html>tag with language attributes set to the current user’s locale.
  • image
    It generates an HTML <input type=”image”> tag.
  • img
    It generate an HTML <img> tag
  • javascript
    It renders JavaScript validation rules loaded by ValidationPlugin.
  • link
    It renders an HTML anchor or hyperlink
  • messages
    It conditionally displays a set of accumulated messages.
  • multibox
    It renders input fields for multiple checkbox.
  • option
    It renders a select options
  • options
    It renders a collection of select options
  • options collection
    It renders a collection of select options.
  • password
    It renders an input field for password.
  • radio
    It renders an input field for radio button
  • reset
    It renders an input field for reset button
  • select
    It renders a select element.
  • submit
    It renders a submit button.
  • text
    It renders an input field of type “text”
  • textarea
    It renders an input field for textarea
  • xhtml
    It instructs the remaining tags to generate their output as XHTML rather than HTML.

The Bean Tag Library

 
The tags of Bean Tag Library are used for accessing JavaBeans, properties of JavaBeans and also for defining new JavaBeans. The Bean tag libraries is a collection of utility tags mainly used for referring objects and storing them in JSP scripting variables so that other tags can easily access them. At runtime, these tags can throw a JspException due to incorrect usage. The <page> directive can be used for declaring error page.
 
The tags present under the Bean tag library are as shown,
  • cookie
    It keeps a reference to HTTP cookies from the incoming request
  • define
    It keeps a reference in a scripting variable based on the value of specified bean property.
  • header
    It keeps a reference to the HTTP header from the incoming request.
  • include
    It keeps a reference in String object for the response obtained from the request.
  • message
    It makes an internationalized message in the form of String object available to the output stream.
  • page
    It keeps reference to implicit objects from the page context as a bean.
  • parameter
    It keeps a reference to the parameter specified in incoming request.
  • resource
    It keeps the reference to resource used in web application and makes it available as a String object.
  • size
    It indicates the number of elements in a Collection or Map
  • struts
    It keeps a reference to the Struts internal configuration object within page scope.
  • write
    It outputs the value of the specified bean property to the current output stream.

The Logic Tag Library

 
The JSP pages are converted into servlets and then compiled before it is executed . The service() method contains all the HTML and the code inside the JSP page. Thus, any variable that is declared with in the scriplet in JSP page, are local to the service() method. The implicit JSP objects are also local to the servlet’s service() method.
 
JSP and Servlets have the concept of scope of variables because some of the variable have a longer lifespan than the page request and some variables need to be accessed outside the service() method of the servlet.
 
The scope of JSP variables can either be application or page or request or session. A variable with application scope is available for the life of an application. A variable with page scope is available until the current JSP’s service() method completes. A variable with request scope persists until the processing for the current request is completed. A variable with session scope persists until the session of the current users expires.
 
The logic tag library allows users to execute conditional statements in JSP pages. With these tags, the content wrapped within the tag will be processed only when a particular condition is true. The tags under Logic Tag Library are shown as,
  • empty
    It evaluates the object specified in the request to check if it is null or an empty string.
  • equal
    It evaluates the object specified in the request to check if its value equals to the specified value.
  • forward
    It searches for the forward specified in the ActionForward entry and redirects control to the specified forward.
  • greaterEqual
    It evaluates the object specified in the request to check if it is greater than or equal to the specified value.
  • greaterThan
    It evaluates the object specified in the request to check if it is greater than the specified value.
  • iterate
    It repeats the content for each specified element of the collection.
  • lessEqual
    It evaluates the object specified in the request to check if it is less than or equal to the specified value.
  • lessThan
    It evaluates the object specified in the request to check if it less than the specified value.
  • match
    It evaluates the object specified in the request to check if it matches an appropriate substring of the requested variable.
  • messagesNotPresent
    It renders the content when the request does not contain any message or error.
  • messagesPresent
    It renders the content when the request contain message or error
  • notEmpty
    It evaluates the object specified in the request to check if it is neither null, nor an empty string , nor an empty java.util.Collection
  • notEqual
    It evaluates the object specified in the request to check if it is not equal to the specified value.
  • notMatch
    It evaluates the object specified in the request to check if it is not an appropriate substring of the requested variable.
  • notPresent
    It evaluates the object specified in the request to check if it is not present in this request.
  • present
    It evaluates the object specified in the request to check if it is present in this request.
  • redirect
    It renders an URL to the page specified in the HTTP redirect.

Advantages of using Template Tags

 
Struts also support a library containing collections of tags called template tags. These are JSP pages that include parameterized content.·
  • Template tags are useful for creating dynamic JSP templates for pages that share a common format.·
  • Unlike standard JSP directive which allow only static content , these tags allow for dynamic content.

Using Template Tags for Web Application

 
For creating templates you need to declare three template tags that work in an interrelated manner. They are :
  • get
    It gets the content from the request scope that was placed there by a put tag.
  • insert
    It includes a template . The templates are JSP pages with parameterized contents. This contents comes from the put tag which are children of insert tags.
  • put
    It puts the content into request scope.
The following code shows the use of Template tags.
 
Source Code 1,
  1. <template : insert template=’/format.jsp’>  
  2.    <template : put name=’title’ content=’ /Admin Logon’ />  
  3.    <template : put name=’header’ content=’ /header.jsp’ />  
  4.    <template : put name=’content’ content=’ /logonAdmin.jsp’ />  
  5.    <template : put name=’footer’ content=’ /footer.jsp’ />  
  6. </template:insert>   
This code uses two template tags named insert and put . It creates a template that will always display title, header, content and then footer. The insert tag includes a template named format.jsp. There are four put template tags defined within the insert tag that puts contents named “ Admin Logon”, “/header.jsp” and “/footer.jsp” respectively.
 
The following code defines format.jsp which uses the template tags as shown,
 
Source Code 2
  1. <html>  
  2.    <head>  
  3.       <title> <template:get name=’title’/ ></title>  
  4.    </head>  
  5.    <body>  
  6.       <table>  
  7.          <tr><td><template:get name=’header’ /> </td></tr>  
  8.          <tr><td><template:get name=’content’ /> </td></tr>  
  9.          <tr><td><template:get name=’footer’ /> </td></tr>  
  10.       </table>  
  11.    </body>  
  12. </html>   
The above code uses the get tag to get the content from request scope that was put there by the put tag defined in source code 1.
 

Summary

 
Struts is a Model View Controller (MVC) based open source framework for developing web applications. Spring and MVC  are some of the alternative frameworks and environments for dynamic web application development. The struts framework supports various packages which a user can use to build the components for web application. Struts supports three core libraries known as HTML, Bean, and Logic. Struts template tag contains three tags : put, insert and get.


Similar Articles