INTRODUCTION

In any MVC based application , a Model component typically is one of the most important components as it implements the core business logic and the data access functionality. They resemble real-life entities and business processes. The model component needs to be totally independent of the View component as in this manner the same core application code can be reused with different types of view such as web based interface, standalone interface, or with mobile device interface and so on.
Model components also provide interfaces to database or back-end systems. The Model components should be independent of the type of client accessing the business objects.
View component on the other hand provides an interface to your application. The View component represents a display of the model in a user interface. There can be different views of the same model.

MODEL COMPONENTS

The model component has three types of classes or sub-layers

External Interface

Classes at this layer will be responsible for providing interface to the external code. It is through these interfaces that the external code will interact with the Model. The data values arriving from View components need to be converted to objects , which are suitable to the business logic. These are part of External Interface and are known as value Objects. For example , in the given source code below, the ProductSearch application the class Product will be a Value Object.
  1. public class Product {
  2. private String name;
  3. private float price;
  4. public Book(String name, float price) {
  5. this.name = name;
  6. this.price = price;
  7. }
  8. public void setName(String name) {}
  9. public String getName() {}
  10. public void setPrice(float price) {}
  11. public float getPrice() {}
  12. }
This code defines a Product class with two attributes, name and price. It implements a getter and setter method for each of the attributes.

Business Logic

The classes that implement business logic facilitates communication between External Interface classes and the Data Access classes. They implement the actual business requirement . For example in ProductSearch application, the functionality such as SearchProductbyName() or SearchProductbyPrice() will be implemented as Business Logic.

Data Access

The classes will generally implement functions related to data such as loading , storing, accessing or modifying the data in a format required by the data store. For example in ProductSearch application, data access classes will actually build the query for getting the required product from the database.

VIEW COMPONENT

The advantage of separating Model and View layer is that no business logic is embedded in this component, it allows the applications’ interface to change without affecting any kind of Model component code.
Technology used for implementing Struts view component can be HTML/JSP or XML or Swing and so on.
ActionForm
Struts ActionForm class is an abstract class. A subclass of this class is created to capture the application specific form data as well as to display the required data on the presentation page. Such a sub class is also known as FormBean as it is a basic Java bean with getter and setter method for every property allowing setting and retrieving the data. These classes do not contain the code for business logic and data access. The reset() and validate() methods are the only methods present in these classes. The validate() method is invoked only if the Form Bean has been configured for validation in the configuration file.
In Struts based applications a Form Bean has a well-defined life cycle. Form Bean act as a pipe for transferring data between the Model and View layer of Struts Application. Figure 1 below depicts the life cycle of typical struts Form Bean.
Form Bean Life Cycle
Fig 1: Form Bean Life Cycle

STRUTS TAG LIBRARY

This library is a collection of tags that provide a way to interact with the web application objects in a JSP.
Advantages of Bean tags
Attribute of Bean Tags
The common attributes that can be applied to all the Bean tags are,
Struts Logic Tag Library
This library provides a set of tags for implementing simple conditional logic in a JSP. The advantages of Logic tags are:

Struts HTML Tag Library

The struts HTML tag library provides custom tags that are useful for generating HTML controls.
Cancel Tag
The tag generates an HTML <input type=”submit”> tag, that when executed triggers a cancel feature. This tag is always embedded within the <form> tag. This tag will not invoke the Form Bean’s validate() method if the incoming request consists of a request parameter named CANCEL.
Some of the attributes of this tag are,
Code below the use of this tag
  1. <html : form action=”/login”>
  2. <html:cancel property=”cancelButton” value=”CANCEL”/>
  3. </html:form>
This code uses cancel tag to create a button component title “CANCEL” and is identified as “cancelButton”
The checkbox Tag
It generates an HTML <input type=”checkbox”> tag. It is always embedded within the <form>tag. The form field associated with this tag should be of Boolean or Boolean type. The reset() method should have statement to reset the field to false.
Some of the attribute of this tag are,
The code uses a checkbox tag that will search for the FormBean named “/login” which is defined in the action attribute of the form tag. It then calls the getter method of this bean’s property named as ‘register” which is specified with the property attribute of checkbox tag. The checkbox control is then populated with the data available from the Form Bean property values.
The errors Tag
The tag displays the error messages that are stored as an ActionError object defined in the org.apache.struts.action package. It can also format and display the error message along with header , footer, prefix and suffix. Header is the output String that will be displayed before the list of error messages. Footer is the output String that will be displayed after the list of error messages. Prefix is the output String displayed before each error message and suffix is output String displayed after each error message.
Some of the attributes of this tag are:
  1. <html:errors property=”employeeID”/>
The code uses errors tag to display errors that are limited to the property named “employedID”. If the property attribute is not defined then it will show all the errors declared in the ActionError object.
The file tag
The tag generates an HTML <input type=”file”> tag. This tag must be always embedded within <form> tag and the action attribute must be defined for the <form> tag. The <form> tag must specify the HTTP method as POST and its encoding type set to “multipart/form-data”.
The following code demonstrate the use of this tag
  1. <html:form method=”post” action=”/sendFile”>
  2. Send File Here :<html:file property=”sendFile”><br>
  3. <html:submit>
  4. </html:form>
This code uses the html tag within the form tag with method set to “post” and action attribute set to “sendFile”. The property attribute of the file tag specifies the field for which the HTML tag’s name attribute is set.
The frame Tag
This tag generates an HTML <frame> tag. The URL for frame can be specified by four ways such as using action attribute, using forward attribute, using href attribute or using page attribute . The action attribute is used to specify the name of an Action from config file whose URL will be used. The forward attribute is used to specify the name of a global ActionForward that contains the URL which will be used. The href attribute is used to specify an absolute URL where as page attribute is used to specify an application-specific URL.
The following code demonstrate the use of this tag.
  1. <html:frame href=http://www.ashishb.com/>
  2. Or
  3. <html:frame page=”/login.jsp” />
The first line of code uses href attribute to specify the absolute URL as http://www.ashishb.com/ whereas , the second line of code uses page attribute to specify the URL that is specific to application in concern.
The hidden Tag
It generates an HTML <input type=”hidden” > tag . This tag is always embedded within the form tag. Some attributes of this tag are below:
The following code demonstrate the use of this tag,
  1. <html:form action=”/findProduct”>
  2. <html:hidden property=”lastProductId”/>
  3. <html:submit>
  4. </html:form>
The above code uses hidden tag within form tag. The action attribute with form tag indicates the Form Bean named “findProduct” whose getter method will be called for the “lastProductId” as specified with property attribute of hidden tag. Then , this field’s value will populate the HTML control with the contents with the contents of lastProductId.
The html Tag
This tag generates an <html> tag that has the user’s locale defined with the language attribute . Some of the attribute of this tag are:
The following code demonstrate the use of this tag
  1. <html:html locale=”true” xhtml=”true” />
This code uses html tag with its attribute locale and xhtml set to true.
The javascript Tag
This tag generates a JavaScript code that is used by the validation framework to validate the client-side data.
Some attributes for this tag are,
The following code demonstrate the use of this tag
  1. <html:javascript formName=”login” staticJavaScript=”false” src=”staticLogin.jsp”/>
This code uses the javascript tag specifying that the Form Bean name “login” will be used for validation. The javascript code is separated into two pieces, static and dynamic.
The static javascript attribute is set to false indicating that only dynamic piece of Javascript will be used given by src attribute.
The link Tag
The tag generates an HTML anchor tag . The URL for link can be specified by four ways such as using action attribute , using forward attribute , using href attribute or using page attribute. The action attribute is used to specify the name of an Action form config file whose URL will be used. The forward attribute is used to specify the name of a forward whose URL will be used. The href attribute is used to specify an absolute URL whereas page attribute is used to specify an application specific URL.
The following code demonstrate the use of this tag,
  1. <html:link href=”http://www.ashishb.com/ “ paramId=”query” paramName=”queryObject”/>
The code uses link tag with href attribute set to “http://www.ashishb.com/”. The paramId and paramName attribute are used to query the string to be appended to the absolute URL.
The option Tag
The tag generates an HTML <option> tag. This must always be embedded within the select tag. The control will be selected if its value matches with the property of Form Bean specified with the select tag.
Some of the attribute of this tag are,
The following code demonstrate the use of this tag,
  1. <html:select property =”status”>
  2. <html:option value=”married”> Married</html:option>
  3. <html:option value=”unmarried”> Unmarried</html:option>
  4. </html:select>
The code uses option tag within the select tag. The property attribute specifies that the value of field “status” of Form Bean is to be used for matching with the values of option. Only the option control for which the property value is matched will be marked selected at the time of generation.
The rewrite Tag
This tag generates a URL. As opposed to link tag it does not create anchor tag for the URL. It can be used for creating constant strings. The URL can be specified by four ways such as using action attribute, or forward attribute or href attribute or page attribute.
The following code demonstrate the use of this tag
  1. <html:rewrite page=”/login.jsp” name=”args”/>
This code uses page attribute to generate a URL that is specific to application. The name attribute specifies the instances of Map whose entries will be added as query string parameters to this URL.
The text Tag
The tag generates an HTML <input type=”text”> tag . It must always be embedded within the form tag. Some of the attributes of this are:
The following code demonstrate the use of this tag.
  1. <html:form action=”/searchProduct”>
  2. Search Product: <html:text property=”product Id” maxlength=”4” /><br>
  3. <html:submit>
  4. </html:form>
This code uses text element to create a textbox where user can enter the ProductId. The property attribute indicates that the property field named “productID” of Form Bean specified with form tag will be used for searching. The maxlength attribute indicates that the length of “productId” can not exceed 4 characters.

Summary

A Model component implements the core business logic and the data access functionality. Separation of Model and View Components allows the application’s interface to change without affecting any of the Model component code. Struts ActionForm class is an abstract class which is subclassed to capture the application specific form data as well as to display the required data on the presentation page. Struts tags libraries help in creating interactive form-based application using server pages.