Introduction to Struts
Unnat-e
1 10/14/08
Agenda Servlet
BackGround
– Servlets, JSP, web.xml – Request processing flow , issues with Servlets Struts
Components HelloWorld Application Exercise
Unnat-e
2 10/14/08
Servlets Servlet
Unnat-e
flow of execution
3 10/14/08
JSP JSP
Unnat-e
flow of execution
4 10/14/08
Issues with JSP and servlets Real
issues
– – – – – –
Servlets embed html into code JSP embeds code into GUI Project time increases Not modular Reuse is difficult Server side programmers are not comfortable with GUI code and vice versa – If target of GUI is changed, server side code gets affected. Unnat-e
5 10/14/08
Struts Overview MVC
architecture for Web based applications Separates view (JSP) from model (java code).
Unnat-e
6 10/14/08
Struts overview (Request flow)
Unnat-e
7 10/14/08
Struts Components ActionServlet ActionServlet
– This is the main struts class. It is a servlet. An application that uses struts directs all the requests to the ActionServlet class by specifying a url mapping in the web.xml file.
Unnat-e
8 10/14/08
Struts Components – strutsconfig.xml Similar
to web.xml, this is specified as a parameter in web.xml It contains two broad categories of information – Configuration information for struts – Action-mappings Struts-config
entries
– Form beans, action-mappings Unnat-e
9 10/14/08
Struts Components – ActionMappings An
action mapping entry in the struts config file maps a url to a model and a view. The model is an action class The view is a jsp file
Unnat-e
10 10/14/08
Struts Components – Action Classes Action
classes populate the model when a request is made – for example if a request is made to view the details of an employee with a particular id, then the model will retrieve the details of the employee using the id. Action classes should be written by the application programmer Action classes should extend org.apache.struts.action.Action Unnat-e
11 10/14/08
Struts Components – Action classes Action
classes need to override the following method Public ActionForward execute Takes following parameters – – – – Unnat-e
ActionMapping mapping ActionForm form HTTPRequest request HTTPResponse response 12 10/14/08
Struts Components – ActionForm Action
classes need to access to form variables. In servlets, this is done via request.getParameter() Struts allows the programmer to define a bean called an ActionForm object. This bean should contain variables with the same name as the variables in the form. An entry should be made in the form-beans section of struts-config.xml for this class An entry should be made in the action mapping for this url Unnat-e
13 10/14/08
Struts Components – ActionForm population mechanism When a request arrives, struts checks the struts
config.xml and retrieves the action-mapping corresponding to the url. It checks if there is a name parameter corresponding to the action mapping If there is a name parameter, struts finds out the corresponding class name from the form bean entry It creates an object of this type. It now traverses via all the members of this object via reflection, looks up corresponding variables from the request and populates the form Unnat-e object.
14 10/14/08
Struts Components – request flow details Tomcat
forwards request to ActionServlet ActionServlet retrieves action-mapping from struts-config.xml ActionServlet creates Actionform object ActionServlet creates Action object ActionServlet invokes execute on Action Action performs action directly or by delegating to business logic Action creates model and stores it into session Action returns a Forward Action Servlet retrieves JSP corresponding to Unnat-e Forward and forwards to the jsp
15 10/14/08
Struts Components – JSP tag libs Struts
provides a rich set of tag libraries which can be used in jsp pages HTML tag libs can be used to create jsp forms rapidly Logic tag libs are used to do evaluation of expressions and decision making Bean tag libs are used to manipulate java beans, cookies, and http headers Unnat-e
16 10/14/08
The first struts Application Application
shows a small form with a
message Enter Name <submit> button On submitting, it should show a screen with hardcoded details of username.
Unnat-e
17 10/14/08