Professional Documents
Culture Documents
Developed by Zackriya
Whats struts?
• Struts is a well-organized framework based on MVC architecture
• Simplified Actions - Actions are simple POJOs. Any java class with execute() method can be
used as an Action class.
• Intelligent Defaults - Most configuration elements have a default value which can be set according
to the need
• Action classes: Programming the abstract classes instead of interfaces is one of design issues of
struts1 framework that has been resolved in the struts 2 framework
• Threading Model: In Struts1, Action resources must be thread-safe or synchronized, there should
only be one instance of a class to handle all requests for that Action. In Struts 2, Action objects are
instantiated for each request, so there are no thread-safety issues.
• Harvesting inputs
• Expression Language: Struts1 integrates with JSTL. Struts 2 can also use JSTL, however it
supports a more powerful and flexible expression language called "Object Graph Notation
Language" (OGNL).
• Type Conversion
CONTROLLER- Filter Dispatcher
• The role of the controller is played by the Struts 2
FilterDispatcher
Java Annotation:
@Results({
@Result(name="input", value="/Login.jsp" )
@Result(value="/AccountPage.jsp" )
})
public class Login implements Action {
public String execute() {
//Business logic for login
}
}
Way to inform the framework.. contd
Model - Action
• Struts 2 action serves two roles
<filter>
<filter-name>struts2</filter-name>
<filter-
class>org.apache.struts2.dispatcher.FilterDispatcher</filter
-class> </filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
</web-app>
struts.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts
Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="default" namespace="/"
extends="struts-default">
<action name="Menu">
<result>/menu/Menu.jsp</result>
</action>
</package>
<include
file="manning/demoWorld/demoWorld.xml"/>
</struts>
demoWorld.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC….>
<struts>
<package name="demoWorld"
namespace="/demoWorld" extends="struts-default">
<action name="Name">
<result>/demoWorld/NameCollector.jsp</result>
</action>
<action name="HelloWorld"
class="manning.demoWorld.HelloWorld">
<result
name="SUCCESS">/demoWorld/HelloWorld.jsp</res
ult>
</action>
</package>
</struts>
Menu.jsp
<%@ page contentType="text/html; charset=UTF-8" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<title>Struts 2 in Action: Menu</title>
</head>
<body>
<hr>
<ul>
<li><a href="<s:url
action='demoWorld/Name'/>">HelloWorld</a></li>
</ul>
<hr>
</body>
</html>
struts.xml
NameCollector.jsp
<%@ page contentType="text/html; charset=UTF-8" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<title>Name Collector</title>
</head>
<body>
<hr>
<h4>Enter your name so that we can customize a
greeting just for you!</h4>
<s:form action="HelloWorld">
<s:textfield name="name" label="Your name"/>
<s:submit/>
</s:form>
<hr>
</body>
</html>
HelloWorld.jsp
<%@ page contentType="text/html; charset=UTF-8" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<title>HelloWorld</title>
</head>
<body>
<hr>
<h3>Custom Greeting Page</h3>
<h4><s:property value="customGreeting"/></h4>
<hr>
</body>
</html>
ValueStack as a repository
HelloWorld.java
D:\Tomcat 5.0\
webapps\HelloWorld\WEB-INF
Directory structure
Action in detail
Action encapsulates the unit of work through the
execute() method
Action – Data Transfer
• Request parameters from the form are moved to properties that
have matching names.
• Params interceptor has been the one moving data from request
parameters to our action’s JavaBean properties
Validation through Action
• Action to extend ActionSupport class to provide
validation
<result name="input">/demoWorld/NameCollector.jsp</result>
Validation code…
demoWorld.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE ….">
<struts>
<package name="demoWorld" namespace="/demoWorld"
extends="struts-default">
…..
<action name="HelloWorld"
class="manning.demoWorld.HelloWorld">
<result>/demoWorld/HelloWorld.jsp</result>
<result
name="input">/demoWorld/NameCollector.jsp</result>
</action>
</package>
</struts>
Validation code…
Validation_Demo
Interceptors
The framework creates an object called an ActionInvocation that
encapsulates the action and all of the interceptors that have been
configured to fire before and after that action executes
Interceptor invocation
• First url mapping
1. Do some preprocessing
2. Pass control on to successive interceptors, and
ultimately the action, by calling invoke(), or divert
execution by itself returning a control string.
3. Do some postprocessing
Interceptor invocation… contd
Output:
INFO: Executed action [/manning/demoWorld/HelloWorld] took 123
ms
Interceptors - Logger
Output:
INFO: Starting execution stack for action
/manning/demoWorld/HelloWorld
INFO: Finishing execution stack for action
/manning/demoWorld/HelloWorld
eg:
<action name="exampleAction"
class="example.ExampleAction">
<param name="firstName">John</param>
<param name="lastName">Doe</param>
</action>
<global-results>
<result name="error2">/menu/Menu.jsp</result>
<result name="error1">/index2.html</result>
</global-results>
<global-exception-mappings>
<exception-mapping
exception="java.lang.NullPointerException"
result="error1"/>
<exception-mapping
exception="java.lang.Exception" result="error2"/>
User defined Interceptors
public class CapsInterceptor implements Interceptor {
}
User defined Interceptors…contd
Changes in demoWorld.xml
<interceptors>
<interceptor name="capsInterceptor"
class="manning.demoWorld.CapsInterceptor" />
<interceptor-stack name="myinterceptor">
<interceptor-ref name="defaultStack" />
<interceptor-ref name="capsInterceptor" />
</interceptor-stack>
</interceptors>
<default-interceptor-ref name="myinterceptor" />
OGNL
Eg:
<h5>Congratulations! You have created </h5>
<h3>The <s:property value=“phoneNumber" />
Portfolio</h3>
• Even in this simple case of the Struts 2 property tag in the view
side, a conversion must be made from the Java type of the
property referenced by the OGNL expression language to the
string format of the HTML output.
• At this point, the original value in request is still the String “37”
for age, here’s where the type converters come into play.
• For this OGNL will consult its set of available type converters to
see if any of them can handle this particular conversion.
• The ages property the framework will create an array for us, so we
don’t need to initialize
• For names property we need to initialize the array with a size since
the OGNL expressions are targeting individual elements of an existing
array.
<s:form action=“ListDataTransferTest">
<s:textfield name="ages" label="Ages"/>
<s:textfield name="ages" label="Ages"/>
<s:textfield name="ages" label="Ages"/>
<s:textfield name="names[0]" label="names"/>
<s:textfield name="names[1]" label="names"/>
<s:textfield name="names[2]" label="names"/>
<s:submit/>
</s:form>
Mapping of List properties… contd
• If List need to handle non-string property, we need to define the non
string mapping in a properties file.
•If the Action file is ListDataTransferTest then the property file should be
ListDataTransferTest-conversion.properties and place this file in the classpath
• If we want to tell OGNL that we want “Double” type of element in our List
property, add the below line in the properties file
•Elements
Mapping of List properties… contd
Element-weights=java.lang.Double
Control Tags
index.jsp index.jsp
demoTag.xml Demotag.xml
IfControlTag.jsp IfControlTag.jsp
Append Tag
The append tag is a generic tag that is used to merge multiple
iterators into one iterator
AppendTag.java AppendTag.jsp
AppendTag.java AppendTag.jsp
UI tag: textfield
TextField tag:
<%@ taglib prefix="s" uri="/struts-tags" %>
<h1><span style="background-color
: #FFFFcc">Textfield Tag Example!</span></h>
<s:form>
<s:textfield label="Employee Name" name="empname" size="15"
maxlength="10" />
</s:form>
</body>
</html>
UI tag…contd
TextArea tag:
<body>
<h1><span style="background-color
: #FFFFcc">Textarea Tag Example!</span></h>
<s:form>
<s:textarea label="Description" name="description" cols="15" ro
ws="10" />
</s:form>
</body>
UI tag…contd
Radio tag:
<body>
<h1><span style="background-color: #FFFFcc">Radio Tag Example!
</span></h>
<s:form>
<s:radio label="Fruits" name="fruitsname" list="fruits" />
<s:radio label="Animals" name="animalsname" list="animals" />
</s:form>
</body>
UI tag…contd
Checkbox tag:
<body>
<h1><span style="background-
color: #FFFFcc">Checkbox Tag Example!</span></h1>
<b>Sex</b><br>
<s:checkbox label="Male" name="male" value="true" /><br>
<s:checkbox label="Female" name="male" />
</body>
UI tag…contd
TextArea tag:
<body>
<h1><span style="background-color
: #FFFFcc">Textarea Tag Example!</span></h>
<s:form>
<s:textarea label="Description" name="description" cols="15" ro
ws="10" />
</s:form>
</body>
UI tag…contd
TextArea tag:
<body>
<h1><span style="background-color
: #FFFFcc">Textarea Tag Example!</span></h>
<s:form>
<s:textarea label="Description" name="description" cols="15" ro
ws="10" />
</s:form>
</body>
UI tag…contd
TextArea tag:
<body>
<h1><span style="background-color
: #FFFFcc">Textarea Tag Example!</span></h>
<s:form>
<s:textarea label="Description" name="description" cols="15" ro
ws="10" />
</s:form>
</body>