You are on page 1of 26

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 JSP JSP

Action
Initial Page Action Form
(HTML/JSP) Servlet
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 struts-
config.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
<?xml version=“1.0” encoding=“ISO-8859-1” ?>
<!DOCTYPE struts-config PUBLIC
“-//Apache SoftwareFoundation//DTD Struts Configuaration 1.0//EN”
http://jakarta.apache.org/struts/dtds/struts-config_1_0.dtd>
<struts-config>
<form-beans>
<form-bean name=“loginForm” type=“app.RegistrationForm”/>
</form-beans>
<action-mappings>
<action path=“/login”
type=“app.LoginAction”
name=“loginForm”,
input=“login.jsp”
<forward name=“success” path=“/success.html”/>
<forward name=“failure” path=“/failure.html”/>
</action>
<action-mappings>
</strut-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
TCS Internal

the ActionServlet.
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

global- Describes a set of exceptions that might be thrown by an Action object


exceptions

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
form- Describes a JavaBean property that can be used to configure
properties ActionForm object

global- Describes a set of ActionForward objects that are available to All


forwards Action objects.

forward Describes an ActionForward that is to be made available to an


Action as a return value.

action- Describes a set of action mapping that are available for


mappings 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 Describes a MessageResources object with message templates


resources 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(“<html>”);
out.println(“<head><title>Student
Info</title></head”);
out.println(“<body>”);
out.println(“<h1>”);
out.println(“Student name is “ +
s.getName());
out.println(“</h1>”);
out.println(“</body>”);
out.println(“</html>”);
TCS Internal September 3, 2009
Using JSP Tag
<%
Student s = new Student(“David”);
%>
<html>
<head><title>Student Info</title></head”);
<body>
<h1>
Student name is <%= s.getName() %>
</h1>
</body>
</html>

TCS Internal September 3, 2009


Example: Using tag extension
<%@ taglib uri=“/tags/struts-bean”
prefix=“bean”%>
<html>
<head><title>Student
Info</title></head”);
<body>
<h1>
Student name is <bean:write
name=“Student”
property=“name”/></h1>
</body>
</html> TCS Internal September 3, 2009
Struct Tag Libraries
•struts-bean library
– Tags useful in accessing JavaBeans and
their properties
•struts-html library
– Tags used to create HTML input forms
•struts-logic library
– Tags used for generating conditional
output like looping, etc.

TCS Internal September 3, 2009


Bean Tags
•cookie variable to handle request
cookies.
•define variable based on specified bean
property
•header variable based on specified request
header.
•include To load response from dynamic
application request
•message Renders internationalized message
string
•page Expose the item from the page
context as a bean
•parameter variable for request parameter
TCS Internal September 3, 2009
HTML tags
•base •messages
•button •option, options
•checkbox •password
•errors •radio
•file •reset
•form •rewrite
•hidden •select
•html •submit
•image •text
•img •textarea
•link

TCS Internal September 3, 2009


Logic Tags

Evaluation For testing if values are equal,


Tags less than, etc.
Control- fore redirecting or forwarding
flow tags request
Repeat for iterating over any type of
Tags collection.

TCS Internal September 3, 2009


Reference:
•Struts in Action: Building web applications
with the leading Java framework, Ted Husted
et. al., Mannin Publication, 2004.

TCS Internal September 3, 2009

You might also like