Apache Struts Version 1.0.
Introduction •Apache Struts is a framework which helps developing J2EE applications using Model 2 MVC architecture. •In the normal model 2 MVC architecture, servlets acts as controller, bean acts as model and JSP pages acts as view. •There is no link between all the three and the programmer has to manually do the link in his code. •Using Struts, the link between model, view and controller is automatically taken care. TCS Internal
September 3, 2009
Important Classes •The central part of the framework is ActionServlet. •This class controls navigational flow. •The class Action is used to access business objects. •The input from the user is handled by ActionForm class. •The class ActionMapping bundles the above classes together.
TCS Internal
September 3, 2009
Struts Framework JSP
Initial Page (HTML/JSP)
JSP
JSP
Action Servlet
Action Form Action
STRUCTS-CONFIG.XML
TCS Internal
September 3, 2009
Action Form •The ActionForm is a JavaBean that extends org.apache.struts.action.ActionFrom. •This object captures the input fields sent through the request. •ActionForm has a corresponding property for each field on the HTML form.
TCS Internal
September 3, 2009
ActionForm Example package app; import org.apache.struts.action.*; public class LoginForm extends ActionForm{ protected String username; protected String password; public String getUserName(){ return username;} public String getPassword(){ return password; } public void setUsername(String username){ this.username = username; } public void setPassword(String password){ this.password = password; } }
TCS Internal
September 3, 2009
The corresponding HTML page
TCS Internal
September 3, 2009
Action •Action is responsible doing the actual business logic such a validation, accessing business information, etc. •Depending on the result of action, it takes a decision of which ActionForward should be sent, and informs it to the action servlet. •The core method of Action object is perform. The new versions use TCS Internal
September 3, 2009
Action - Example package app; import org.apache.struts.action.*; import javax.servlet.http.*; import java.io.*; public class LoginAction extends Action { public ActionForward perform(ActionMapping mapping, ActionForm form, HttpServletRequest req, HttpServletResponse res){ LoginForm lf = (LoginForm) form; String username = lf.getUsername(); String password = lf.getPassword(); if(DbUtil.verifyUser(username, password)){ return mapping.findForward(“success”); }else{ return mapping.findForward(“failure”); } }
TCS Internal
September 3, 2009
ActionMapping Class •In web applications, resources are identified through a Uniform Resource Identifier (URI) mechanism. •Resources includes HTML pages, JSP pages, and any custom actions. •ActionMapping objects are used to give custom Actions for URI, or path. •Action mapping object is built using the struts-config.xml file.
TCS Internal
September 3, 2009
Struts Configuration File •This is an XML file with name strutsconfig.xml •This file contains details that ActionServlet needs to handle the requests made to your application. •This file is stored in the WEB-INF folder.
TCS Internal
September 3, 2009
struts-config.xml <struts-config>
TCS Internal
September 3, 2009
ActionServlet Object •This is the core object which works behind the scenes, binding other components together.
TCS Internal
September 3, 2009
The complete picture •A client requests a path that matches the Action URI pattern. •The container passes the request to ActionServlet. •ActionServlet looks up the mapping for the path in configuration file. •If the mapping specifies a form bean, the ActionServlet sees if there is one already exists or creates a new one. •If it is a form bean, the ActionServlet resets and populates it from the HTTP request. •If the mapping has a validate property set to true, it calls validate on form bean. TCS Internal
September 3, 2009
The Complete Picture (Contd...) •If it fails, the servlet forwards to the path specified by the input property and the control flow ends. •If the mapping specifies an Action type, it is reused if it already exists or instantiated. •The Action’s perform or execute method is called and passed the instantiated form bean. •The Action may populate the form bean, call business objects, and do what ever is needed. •The Action returns ActionForward object to the ActionServlet. TCS Internal
September 3, 2009
The Strut Config Elements data-sources
Contains a set of DataSource objects (JDBC 2.0 )
data-source
Specifies a DataSource object that is to be instantiated.
set-property
Specifies a method name and initial value of an additional JavaBean
globalexceptions
Describes a set of exceptions that might be thrown by an Action object
exceptions
Registers an ExceptionHandler for an exception type
form-beans
A set of form bean descriptors for this application module.
form-bean
Describes Actionform subclass that can be referenced by Action object
TCS Internal
September 3, 2009
The Strut Config Elements formproperties
Describes a JavaBean property that can be used to configure ActionForm object
globalforwards
Describes a set of ActionForward objects that are available to All Action objects.
forward
Describes an ActionForward that is to be made available to an Action as a return value.
actionmappings
Describes a set of action mapping that are available for processing.
action
Describes an ActionMapping object that is to be used to process a request for a specific
controller
Describes the controller config bean that encapsulates an application module’s runtime configuration.
message resources
Describes a MessageResources object with message templates for this module.
pulg-in
Specifies the fully qualified class name of general purpose application plug-in module. TCS Internal
September 3, 2009
What are tags? •Normal browsers render HTML pages. •Any java program (servlets) has to print all the HTML tags along with dynamic content. This is a tedious process. •Tags enable to embed java code along with HTML code. •JSP is a set of tag library. •JSTL and Struct provides some additional feature in addition to the normal tags. These are called as Tag extensions. TCS Internal
September 3, 2009
Example: Using Servlet Student s = new Student(“David”); out.println(“”); out.println(“Student Info”); out.println(“”); out.println(“Student name is “ + s.getName()); out.println(“
”); out.println(“”); out.println(“