Thursday, January 12, 2012
The Apache Struts web framework is a free open-source solution for creating Java web applications.
Model: represents the business or database code
View: represents the page design code
Controller: represents the navigational code.
-A "request" handler provided by the application developer that is mapped to a standard URI.
-A "response" handler that transfers control to another resource which completes the response.
-A tag library that helps developers create interactive form-based applications with server pages.
The framework's Controller acts as a bridge between the application's Model and the web View. When a request is received, the Controller invokes an Action class. The Action class consults with the Model (or, preferably, a Facade representing your Model) to examine or update the application's state. The framework provides an ActionForm class to help transfer data between Model and View.
web.xml :deployment descriptor,initialize resources like servlets and taglibs
struts-config.xml: initialize framework resources
ActionForm: collect input from users; represents an HTML form that the user interacts with over one or more pages.
ActionMappings: direct input to server-side Actions
ActionForwards: select output pages
<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE struts-config PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 1.3//EN"
<form-bean name="logonForm" type="app.LogonForm"/>
<action path="/Logon" forward="/pages/Logon.jsp"/>
<action path="/LogonSubmit" type="app.LogonAction"
name="logonForm" scope="request" validate="true"
<forward name="success" path="/pages/Welcome.jsp"/>
<forward name="failure" path="/pages/Logon.jsp"/>
<action path="/Logoff" type="app.LogoffAction">
<forward name="success" path="/pages/Logoff.jsp"/>
<!--the base name of the resource bundle for the application-->
Controller: focused on receiving requests from the client (web browser), deciding what business logic function is to be performed, and then delegating responsibility for producing the next phase of the user interface to an appropriate View component.
ActionServlet: is configured by ActionMappings.
ActionMapping: defines a path that is matched against the request URI of the incoming request and usually specifies the fully qualified class name of an Action class.
When initialized, the controller parses a configuration file (struts-config.xml)
path: Context-relative path of the submitted request, starting with a slash ("/") character, and omitting any filename extension if extension mapping is being used.
type: Fully qualified Java class name of the Action class to be used to process requests for this mapping if the forward and include properties are not set. Exactly one of forward, include, or type must be specified.
page - Beans that are visible within a single JSP page, for the lifetime of the current request.
request - Beans that are visible within a single JSP page, as well as to any page or servlet that is included in this page, or forwarded to by this page.
session - Beans that are visible to all JSP pages and servlets that participate in a particular user session, across one or more requests.
application - Beans that are visible to all JSP pages and servlets that are part of a web application.
JSP pages and servlets in the same web application share the same sets of bean collections. For example, a bean stored as a request attribute in a servlet like this:
MyCart mycart = new MyCart(...);
is immediately visible to a JSP page which this servlet forwards to, using a standard action tag like this:
<jsp:useBean id="cart" scope="request"
forms with error handling -- if the user makes an error, the application should allow them to fix just what needs to be changed -- without having to re-enter any of the rest of the information on the current page or form.
<input type="text" name="username"
value="<%= loginBean.getUsername() >"/>
using Struts Taglibs:
< html:errors > -Displays a set of error messages prepared by a business logic component and stored as an ActionMessages object, an ActionErrors object, a String, or a String array in any scope. If such a bean is not found, nothing will be rendered. This tag displays the messages which are set in the Validate method of the ActioForm. ActionErrors object should be created to hold the error messages. String is passed to the constructor of ActionErrors object. The String is a key from the ApplicationResources.properties file.
< html:errors/> Tag will extract the information from ActionErrors object and displays the errors.