Struts framework in J2EE application

Gouri Shankar 29 May 2003

Agenda
    

MVC Pattern Brief about Servlets, JSP’s About Struts Using Struts – with examples Pros / Cons

MVC Architecture

About Web Applications – handling dynamic requests

About Servlets & JSP’s
  

  

Servlets/JSP’s aid web applications to serve dynamic content. Servlets/JSP’s are executed by a servlet container. JSP’s are an extension of Servlet Technology. In JSP’s we can write Java code inside a HTML document. Extends / enhances web applications using Java. Component based, platform independent method for building web applications. Web Server & Servlet container together serve Service Requests.
Contd….

About Servlets & JSP’s
Servlets are part of controller portion in MVC.  JSP’s are part of View portion in MVC.  A Controller performs the following

– Intercepts HTTP requests from a client. – Translates requests into a specific business operation to perform. – Either invokes business operation or delegates to handler. – Helps to select the next view to display to the client. – Returns the next view(HTML/JSP) to the client. – Centralized point of control for the web application.

Necessity of Struts framework
Provides solution to a specific problem.  Prepare reusable components.  Each may provide an abstraction of some particular concept.

The Problem

 

  

Scope of the problem dealt in this framework is View & Controller parts of MVC Problems addressed in View  Mixing code with presentation leading to problems like  Redundancy  Readability  Maintenance Problems addressed in Controller  Hard coding resource details in servlets. With these problems developer needs to concentrate on business objects as well as request processing. Above problems are addressed by emphasizing on building components and reusability of components.

About Struts
Open source framework.  One of Apache – Jakarta projects from Apache Software foundation(ASF).  Originally created by Craig R. McClanahan and later added to ASF in 2000.

Contd….

About Struts
   

 

Sits in web container. Built on top of JSP, Servlet & taglibs. Provides centralized point of control for web applications. Because of single entry point, this framework provides facilities like centralized logging, authentication, authorization and internationalization. Facilitates to scope the applications. Select either HTTP forward or HTTP request.
Contd….

Struts in J2EE Environment

About Struts
Custom tag libraries – 5 types of taglibs (html,bean,logic,template,nested).  View beans associated with JSP’s – Action form, Scope of View Bean(one instance / scope), dynamic View Beans, one View Bean for multiple JSP’s, validating user inputs.  Handle actions associated within a JSP – Multiple results per action, multiple actions in a action class, choose either redirect / forward, validate, calling a new JSP, handling multiple errors in each action.

Contd….

About Struts
     

Validate user inputs Error handling Maintain Message Resources Validator, tiles frameworks Approximately there are 200 classes and 15 java packages in this framework. Main components are – Configuration file, ActionMapping, ActionServlet, ActionForm, Action, ActionError(s), Tag libraries

About Configuration file
 

Configurations are maintained in struts-config.xml file. Contains following information – Bean Declarations – Global Exceptions – Global Forwards – Action Mappings – Controller – Message Resources – Data Sources – Plugin Possibility to create multiple configuration files.

Class diagram for config

Intialisation Process in Struts Framework

Life cycle for Request Processing an Action

Life cycle of an ActionForm

Flow of an Action in Struts

Exception handling
Declarative exceptions  Programmatic exceptions

Tags in struts
     

No mixing of logic with presentation. Easy to maintain and more emphasis on code reusability. No code redundancy. Better readability. Facility to create custom tags. Custom tags in struts are – HTML – Bean – Logic – Template – Nested

Taglibs Examples
<html> <body> <html:form action="/logon" focus="username"> </html:form> </body> </html> Equivalent HTML <html><body> <form name="logonForm" method="POST" action="/strutsexample/logon.do"> </form> <script language="JavaScript" type="text/javascript"> <!-document.forms["logonForm"].elements["username"].focus() // --> </script> </body> <html> Contd….

Tag libs Examples
Example 2

<td> <bean:write name="dept" property="name" /> </td> Equivalent Code <td> <%=depts[index].getName()%> </td>
Example 3 <title><bean:message key="title.message"/></title>
Contd….

Tag libs Examples
<td> <html:link page="/listEmployees.do" paramId="deptid" paramName="dept" paramProperty="id">show </html:link></td> Equivalent Code <td><a href="EmployeeListing.jsp?deptid=<%=depts[i ndex]. getId()%>">show </a></td>

How does Struts fit in MVC ?
In View – HTML / JSP,Custom taglibs, JavaScript, Style Sheets, Message Bundles, ActionForm classes  In Controller – ActionServlet, Action, RequestProcessor, ApplicationConfig, ActionMapping, Struts utility classes  In Model – Need to be implemented by the user.

How do I deploy ?
Web applications using Struts can be deployed as a standard WAR file.  Struts comes as a Java Archive (JAR) file.

Summary
  

 

Each action on a JSP page will be associated with an Action class. If necessary same Action class can be used for multiple action in JSP’s If JSP’s capture any data there will also be an associated Action Form that handles the data for the input. Action form is like a viewbean with some extra functionality. Effectively user develops a config file, JSP, Action class and Action form class (if necessary).

Miscellaneous

Advantages – Server-side persistence of the form data – Provides database support and includes JDBC connection pooling capabilities – Define multiple data sources in configuration file. – Exception handling. – Multiple actions handled in a single class. – Dynamic action form generation. – As the source code is available, we can extend framework to meet user needs. – Maintain multiple configuration files. Disadvantages – No vendor support – Future compatibility with Struts in case of extension of framework Other similar products in the market – Velocity, Freemarker, WebMacro, Barracuda.

Some Do’s , Don’t’s
      

Be selective to use synchronize keyword in Struts framework No instance variables in Action / ActionForm classes Make sub applications as per business flow of system Use declarative exception handling Use Pre built Action classes like ForwardAction, DispatchAction, SwitchAction etc.. Use Dynamic ActionForm Use visual tools for making config file

References
    

http://jakarta.apache.org/struts/ http://www.triveratech.com/downloads2/TriveraTe ch_StrutsTutorial.PDF http://www.onjava.com/lpt/a/2832 http://www.javaworld.com/javaworld/jw-042003/jw-0418-struts_p.html http://www.javaworld.com/javaworld/jw-022002/jw-0201-strutsxslt_p.html

Questions & Answers

Thank You

Sign up to vote on this title
UsefulNot useful