You are on page 1of 4

6/18/2009

Servlet Limitations
• No clear separation of concerns
– Making calls to business logic
– Making calls to the data layer
Apache Struts – Rendering presentation layer 

• Limited support for common application tasks
– Biding request parameters to Java types
numTimes = new Integer(request.getParameter("numTimes“)).intValue();
– Validating data
– Providing internationalization and localization

Model‐View‐Controller (MVC) Apache Struts 2
• Software engineering architectural pattern.  • Open‐source web application framework for 
developing Java EE web applications. 
• Isolates business logic from user interface 
– Extends the Java Servlet API
considerations. Enables independent 
• Encourage developers to adopt a (MVC) 
modification of  visual appearance of the  architecture. 
architecture
application or the underlying business rules.
• Created by Craig McClanahan. 
• Model: Information (the data) of the application.
• View: User interface, e.g.,  text, checkbox items. 
• Controller: Business rules used to manipulate the 
data to and from the model.

MVC Struts Control Flow

1
6/18/2009

Struts Building Blocks ActionInvocation
• ActionContext • com.opensymphony.xwork2. ActionInvocation
• ActionInvocation • Represents the execution state of an Action. 
• ActionSupport • First executes Interceptors, then the Action 
• Declarative architecture and then Result.
d h l
• Instantiates a new Action to handle each 
request

ActionContext Declarative Architecture
• com.opensymphony.xwork2.ActionContext • struts.xml
• Container of objects an action needs for execution  • Relationship between actions and results
like the session, parameters, locale, etc.
• Sets URLs for application
• The ActionContext
The ActionContext is thread local 
is thread local
– Values are unique per thread.

struts.xml JavaBean
<?xml version="1.0" encoding="UTF‐8" ?>
<!DOCTYPE struts PUBLIC "‐//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
• Java class that conforms to certain rules and 
"http://struts.apache.org/dtds/struts‐2.0.dtd"> conventions
<struts> 
<package name="helloworld" namespace="/helloworld" extends="struts‐default"> 
• Does not need to inherit from any specific class
<action name="Name">
<result>/helloworld/NameCollector.jsp</result>
• Rules & conventions:
</action> – Must be
M t b public
bli
<action name="HelloWorld" class="csc309.helloworld.HelloWorld">
<result name="SUCCESS">/helloworld/HelloWorld.jsp</result>
– Provide no  argument constructor
</action> – Setter and getter function for every attribute
</package>
<constant name="struts.devMode" value="true" />
• Attribute: String firstName
<constant name="struts.action.extension" value="action,do" /> • Getter: public String getFirstName()
</struts> • Setter: public void setFirstName(String fName)
/csc309Struts /helloworld
– No setter and getter overloading

2
6/18/2009

JSP JSP Life Cycle
• Servlets disadvantage
–A little java in a whole lot of HTML
• Java Server Pages
–Java embedded in HTML
p y ( pp y )
• Deployed in textual form (as opposed to byte code)
• Translated to into a servlet on the fly
$CATALINA_HOME/webapps/csc309Struts/hello.jsp

$CATALINA_HOME/work/Catalina/localhost/csc309Struts/org/apache/jsp/hello_jsp.java

13 14

JSP HelloWorld
• Expressions
<%= object.function() %> • if called with parameter “name”
Java expression that evaluates into a String
Translated into out.print() statement – Print  Success! Hello name
• Scriplets
<% 
java.util.Date currentTime = new java.util.Date; • Else
String strDate = currentTime.toString();
%> – Print  Failure! Need to provide “name” parameter
i il ! N d id “ ”
One or more Java statements
Left in place
• Directives
<%@ page import=“java.util.Date, java.text.SimpleDateFormat” %>
<%@ page contentType="text/html; charset=UTF‐8" %>
<%@ taglib prefix="s" uri="/struts‐tags" %>
• Tags
<s:iterator value="posts" status="itStatus“>
<s:form action="AddPost“>
<s:textfield name="username" label="Your name"/>

HelloWorld HelloWorld
csc309Struts
• Actions / Model helloworld
HelloSucceded.jsp
– HelloWorld.java HelloFailed.jps
WEB‐INF
classes
csc309
309
helloword
• Results / Views HelloWorld.class
– HelloSucceded.jps helloworld.xml
struts.xml
– HelloFailed.jps lib
struts2‐core‐2.0.11.jar ......
web.xml

3
6/18/2009

Behind the Scene ActionSupport
• Struts filter captures request • com.opensymphony.xwork2.ActionSupport
• Instantiates HelloWorld.java bean • Provides a default implementation for the most 
common actions.
• Sets value of bean property name by calling on setName()
• Execution
• Calls on execute()
– execute()
()
• Add HelloWorld bean to ValueStack • Validation
• Run HelloSuceeded.jps or HelloFailed.jsp – validate()
• JSPs access values stored in bean (i.e., name) – addFieldError()
• Internationalization
– getText() reads from properties file
– getLocale()

Blog Blog
• Actions
• Collection of posts – Blog
• Application entry point.  Display blog
• Post – PostCollector
• Provide form to collect new post
– username – AddPost
• Add post to blog and display blog
– comment • If something goes wrong, ask user to re‐enter info
• Result
– date – ShowBlog
• Print all comments 
– PostCollector
• Print post collection form
• Servlet
– Initialization
• Creates blog and stores it into the ServletContext

Blog
csc309Struts
blog
PostCollector.jsp
ShowBlog.jps
blog.css
WEB‐INF
classes
csc309
blog
AddComments.class
AddComments.properties
AddComments_es.properties
Blog.class
Initialization.class
Post.class
blog.xml
struts.xml
lib
struts2‐core‐2.0.11.jar ......
web.xml

You might also like