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,
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 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,
- <template : insert template=’/format.jsp’>
- <template : put name=’title’ content=’ /Admin Logon’ />
- <template : put name=’header’ content=’ /header.jsp’ />
- <template : put name=’content’ content=’ /logonAdmin.jsp’ />
- <template : put name=’footer’ content=’ /footer.jsp’ />
- </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
- <html>
- <head>
- <title> <template:get name=’title’/ ></title>
- </head>
- <body>
- <table>
- <tr><td><template:get name=’header’ /> </td></tr>
- <tr><td><template:get name=’content’ /> </td></tr>
- <tr><td><template:get name=’footer’ /> </td></tr>
- </table>
- </body>
- </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.