Struts (http://struts.apache.org/) is a MVC framework and is used to implement the web application in Java using MVC design pattern. Struts provide a complete framework to divide the web tier in Model, View and Controllers.
- Struts provide action base classes to implement the controller at Web Tier, which will accept the request, call the business layer for required data or processing and will forward it to right view.
- Form Beans, defined by Struts framework, carry the data.
- JSP or HTML pages act as view. Controller i.e. action classes call business layer for data processing, set these as parameters in request/session and forward the request to view i.e. JSP or HTML page.
Class to override to make an action: org.apache.struts.action.Action
ActionForward - Override execute method in extended class to forward the request to some other page.
Dispatch Action - To use a single action for multiple methods. Request should have a parameter: method="
Form Bean: It works as a model for the JSP/presentation layer. Struts get the parameter values from request and set the matching properties in FormBean.
ActionMapping - It represents the mapping of URL to Actions. Struts framework builds this object after reading/parsing the struts-config.xml file.
- Mapping for URL <> Actions should be specified in struts-config.xml file under Action tag.
- A forward tag can also be specified in Action element.
- Attributes are Name and path to a JSP page
Now an Action can return a action from its execute method like actionMapping.forward (
). Struts framework forward the request to the JSP page associated with this tag.
struts-config.xml is the configuration file for struts framework. It contains
o Action Mapping
o Form Bean
o URL <> Action
o Forward Action <> Name <> URL
o Message Resource Factory
o Form Beans Definitions
We need to define the struts framework's ActionServlet in web.xml and map all *.do with this servlet. *.do represents all the actions.
- Now the servlet engine will route all the requests for *.do to ActionServlet class.
- Then struts-config will come in picture. Strut framework parse the web config XML and maintain the mappings.
- It find the mapping for received request's URL in action mapping
- Retrieve the Action corresponding to this mapping
- And hand over the request to this action
Tag Libraries: Sturts has some tag libraries for presentation/jsp development. But it is recommended to use standard JSP tag library over this.Various tag libraries provided by struts are:
- html:base > Renders an HTML element with an href attribute pointing to the absolute location of the enclosing JSP page. This tag is valid only when nested inside an HTML '' element.
o html:button > Renders an HTML <input> element of type button, populated from the specified value or the content of this tag body. This tag is only valid when nested inside a form tag body. If a graphical button is needed (a button with an image), then the image tag is more appropriate. Attributes >
o accessKey: The keyboard character to move the focus to this button
o disabled: True if this button should be disabled
o title: The advisory title for this button
o titleKey: The message resources key for the advisory title for this element.
o value: Value of the label to be placed on this button. This value will also be submitted as the value of the specified request parameter. [Body of this tag (if any), or "Click"]
o html:submit > Renders an HTML <input> element of type submit.
o html:cancel > Renders an HTML <input> element of type submit. This tag is only valid when nested inside a form tag body. Pressing of this submit button causes the action servlet to bypass calling the associated form bean validate() method. The action is called normally.
Attributes > About same as that of button
o html:reset > Renders an HTML <input> element of type reset.
o html:checkbox > Renders an HTML <input> element of type checkbox, populated from the specified value or the specified property of the bean associated with our current form. This tag is only valid when nested inside a form tag body.
o html:form > Renders an HTML <form> element whose contents are described by the body content of this tag. The form implicitly interacts with the specified request scope or session scope bean to populate the input fields with the current property values from the bean. The form bean is located, and created if necessary, based on the form bean specification for the associated ActionMapping.
o action: The URL to which this form will be submitted. This value is also used to select the ActionMapping we are assumed to be processing, from which we can identify the appropriate form bean and scope. If a value is not provided, the original URI (servletPath) for the request is used.
- html:hidden > Renders an HTML <input> element of type hidden, populated from the specified value or the specified property of the bean associated with our current form. This tag is only valid when nested inside a form tag body.
o value: Value to which this field should be initialized. [Use the corresponding bean property value]
- html:link >Renders an HTML <a> element as an anchor definition (if "linkName" is specified) or as a hyperlink to the specified URL. URL rewriting will be applied automatically, to maintain session state in the absence of cookies. The content displayed for this hyperlink will be taken from the body of this tag.
- html:multibox > Renders an HTML <input> element of type checkbox, whose "checked" status is initialized based on whether the specified value matches one of the elements of the underlying property's array of current values. This element is useful when you have large numbers of checkboxes, and prefer to combine the values into a single array-valued property instead of multiple boolean properties. This tag is only valid when nested inside a form tag body.
- html:select > Renders an HTML <select> element, associated with a bean property specified by our attributes. This tag is only valid when nested inside a form tag body.
- html:option > Render an HTML <option> element, representing one of the choices for an enclosing <select> element. The text displayed to the user comes from either the body of this tag, or from a message string looked up based on the bundle, locale, and key attributes.
- html:radio > Renders an HTML <input> element of type radio, populated from the specified property of the bean associated with our current form. This tag is only valid when nested inside a form tag body
- html:text > Render an input field of type text. This tag is only valid when nested inside a form tag body.
- html:textArea > Render a textarea element. This tag is only valid when nested inside a form tag body.
- Attributes >
rows: The number of row
Others are nested, and logic etc