0% found this document useful (0 votes)
50 views30 pages

Web Frameworks: and and

This document discusses web frameworks and WebWork. It describes how WebWork uses the MVC pattern to separate application logic and web design. It provides advantages of MVC including loose coupling, reusability and division of labor. The document then explains key aspects of WebWork including the action flow, configuration files like web.xml and xwork.xml, action classes that map to results, and integrating views like Velocity.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
50 views30 pages

Web Frameworks: and and

This document discusses web frameworks and WebWork. It describes how WebWork uses the MVC pattern to separate application logic and web design. It provides advantages of MVC including loose coupling, reusability and division of labor. The document then explains key aspects of WebWork including the action flow, configuration files like web.xml and xwork.xml, action classes that map to results, and integrating views like Velocity.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 30

Web Frameworks

and

WebWork
Problem area
• Mixing application logic and markup is bad practise
– Harder to change and maintain
– Error prone
– Harder to re-use

public void doGet( HttpServletRequest request, HttpServletResponse response )


{
PrintWriter out = response
response.getWriter();
getWriter();

out.println( ”<html>\n<body>” );

if ( request.getParameter( ”foo” ).equals( ”bar” ) )


out.println(
i l ( ””<p>Foo
F iis bbar!</p>”
! / ” ));
else
out.println( ”<p>Foo is not bar!</p>” );

out.println(
p ( ”</body>\n</html>”
y );
}
Advantages
• Separation of application logic and web design through
the MVC pattern
• Integration
I t ti with
ith template
t l t languages
l
• Some provides built-in components for
– Form validation
alidation
– Error handling
– Internationalization
– IDE integration
The MVC pattern
• Breaks an application into three parts:
– Model: The domain object model / service layer
– View: Template code/markup
– Controller: Presentation logic/action classes

• Defines interaction between components to promote


loose coupling and re-use
– Each file has one responsibility
– Enables division of labour between programmers and designers
WebWork
• Sophisticated web framework
• Latest version is 2.2.6 – project merged with Struts
• Built on top of XWork – a command pattern framework
• Can be integrated with object factories like Spring
• Required libraries
– webwork
– commons-logging
l i
– velocity + velocity-tools
– servlet api
servlet-api
MVC with Front Controller
Web browser.
Displays output.
Front controller.
Maps request URLs
to controller classes.
Implemented as a
servlet. Web page
template like
JSP or Velocity.
y

Command instances/
Action classes.
classes
Interacts with backend
services of the system.

Backend services
working with the API/
data model.
Action Flow
(random.vm)

Response to client

Result

Client HTTP (getRandomString.action)


request to URL

Stack of
web.xml
interceptors

xwork.xml
k l WebWork
W bW k Stackk off
St Action
A ti classes
l
(config file) Servlet dispatcher interceptors (User code)

(GetRandomStringAction.java)
Web xml
Web.xml
• Maps URL patterns to the WebWork dispatcher
• Most typical pattern is *.action
• Located in WEB-INF/ folder
• Can redirect to the Filter- or ServletDispatcher

<filter>
<filt >
<filter-name>webwork</filter-name>
<filter-class>com.opensymphony.webwork.dispatcher.FilterDispatcher</filter-class>
</filter>

<filter-mapping>
<filter-name>webwork</filter-name>
<url-pattern>*.action</url-pattern>
</filter-mapping>
Xwork xml
Xwork.xml
• Located in root of classpath
• Must include webwork-default.xml
• Maps URLs to action classes
• Maps result codes to results

<xwork>
<include file="webwork-default.xml"/>

<package name="default" extends="webwork-default">

<action name=”invertString" class="no.uio.inf5750.example.action.InvertStringAction">


<result name="success" type="velocity">word.vm</result>
</action>

</package>
</xwork>
Action classes
• Java code executed when a URL is requested
• Must implement the Action interface or extend
A ti S
ActionSupport
t
– Provides the execute method
– Must return a result code (SUCCESS,
(SUCCESS ERROR
ERROR, INPUT)
– Used to map to results
• Properties
ope es seset by the
e request
eques through
oug pub
public
c se
set-methods
e ods
• Properties made available to the response through
public g
p get-methods
Action classes
HTTP request getRandomString.action?word=someEnteredWord

public class InvertStringAction


Implements Action implements Action
{
private String word;
Must correspond to public void setWord( String word ) {
request parameter, this.word = word;
exposed through set-method
set method }

private String invertedWord;


Must correspond to public String getInvertedWord() {
property name in view
view, return
t this.invertedWord;
thi i t dW d
exposed through get-method }

public String execute()


{
Execute method with
char[] chars = word.toCharArray();
user code // do some inverting here...

invertedWord = buffer.toString();
Mustt return
M t a result
lt code
d
return SUCCESS;
(defined in Action) }
}
View
• WebWork integrates with many view technologies
– JSP
– Velocity
– Freemarker
– JasperReports
p p
• Values sent to controller with POST or GET as usual
• Values
a ues made
ade available
a a ab e too the
e view
e by the
e co
controller
o e
View
• Velocity is a popular template engine and -language

Java Action class public


bli St
String
i getInvertedWord()
tI t dW d() {
return this.invertedWord;
}

<html>

Form action to URL <body>


mapped in xwork.xml
<form method=”post”
method= post action=
action=”invertString
invertString.action
action”>
>

<p><input type=”text” name=”word”></p>


Input name corresponding
<p><input type=”submit” value=”Invert”></p>
to set-method in action class
</form>

<p>$invertedWord</p>
Velocity
ypparameter
corresponding to </body>
get-method in action class
</html>
Xwork xml advanced (1)
Xwork.xml
• Different result codes can be mapped to different results

<xwork>
<include file="webwork-default.xml"/>

<package name="default" extends="webwork-default">

<action name=”invertString" class="no.uio.inf5750.example.action.InvertStringAction">


<result name="success" type="velocity">word.vm</result>
<result name=”input” type=”velocity”>input.vm</result>
</action>

</package>
</xwork>
Xwork xml advanced (2)
Xwork.xml
• Static parameters can be defined
• Requires public set-methods in action classes
• WebWork provides automatic type conversion

<xwork>
<include file="webwork-default.xml"/>

<package name="default"
name= default extends=
extends="webwork-default">
webwork-default >

<action name=”invertString" class="no.uio.inf5750.example.action.GetRandomStringAction">


<result name="success" type="velocity">random.vm</result>
<param name=”numberOfChars”>32</param>
</action>
/ ti

</package>
</xwork>
Xwork xml advanced (3)
Xwork.xml
• Xwork.xml files can include other files
– Files are merged
• F
Facilitates
ilit t b breaking
ki complex
l applications
li ti iinto
t
manageable modules
– Specified files are searched for in classpath
– Configuration can be separated in multiple files / JARs

<xwork>
<include file="webwork-default.xml"/>

<package name=”default" extends=”webwork-default”>


<!–
! Default
D f lt action
ti mappings
i -->
</package>

<include file=”xwork-public.xml”/>
<include file=”xwork-secure.xml”/>

</xwork>
Xwork xml advanced (4)
Xwork.xml
• Actions can be grouped in packages
• Useful for large systems to promote modular design
• A package can extend other packages
– Definitions from the extended package are included
– Configuration
Config ration of commons elements can be centrali
centralized
ed

<xwork>
<include file="webwork-default.xml"/>

<package name="default" extends="webwork-default">


<action name=”invertString” class=”no.uio.no.example.action.InvertStringAction”> <!– mapping omitted -->
</action>
</package>

<package name=”secure” extends=”default”>


<!– Secure action mappings -->
</package>

</xwork>
Xwork xml advanced (5)
Xwork.xml
• Actions can be grouped in namespaces
• Namespaces map URLs to actions
– Actions identified by the name and the namespace it belongs to
– Facilitates modularization and maintainability

<xwork>
<include file="webwork-default.xml"/>

<package name=”secure" extends=”default” namespace=”/secure”>

<action name=”getUsername” class=”no.uio.inf5750.example.action.GetUsernameAction”>


<result name=”success” type=”velocity”>username.vm</result>
</action>
/ ti

</package>

</xwork>
Interceptors
• Invoked before and/or after the execution of an action
• Enables centralization of concerns like security, logging

<xwork>
<include file="webwork
file= webwork-default.xml
default xml"/>
/>

<package name="default" extends="webwork-default">

<interceptors>
<interceptor name=”profiling” class=”no.uio.example.interceptor.ProfilingInterceptor”/>
</interceptors>

<action name=”invertString” class=”no.uio.no.example.action.InvertStringAction”>


<result name=”success” type=”velocity”>word.vm</result>
yp y
<interceptor-ref name=”profiling”/>
</action>

</package>
</xwork>
Provided interceptors
• Interceptors perform many tasks in WebWork
– ParametersInterceptor (HTTP request params)
– StaticParametersInterceptor (config params)
– ChainingInterceptor

• Many interceptor stacks provided in webwork-default.xml


– defaultStack
– i18nStack
– fileUploadStack and more…
Interceptor stacks
• Interceptors should be grouped in stacks
• A default interceptor stack can be defined
– Should include the WebWork default stack

<xwork> <!– include file and p


package
g omitted -->

<interceptors>
<interceptor name=”profiling” class=”no.uio.example.interceptor.ProfilingInterceptor”/>
<interceptor name=”logging” class=”no.uio.example.logging.LoggingInterceptor”/>

<interceptor-stack name=”exampleStack”>
<interceptor-ref name=”defaultStack”/>
<interceptor-ref name=”profiling”/>
<interceptor-ref name=”logging”/>
</interceptor-stack>

</interceptors>

<default-interceptor-ref
p name=”exampleStack”/>
p

</xwork>
Result types
• Determines behaviour after the action is executed and
the result is returned
• Several
S l resultlt ttypes bundled
b dl d with
ith W
WebWork
bW k
• Dispatcher (JSP)
– Default
Defa lt - will
ill generate a JSP view
ie
• Velocity
– Will generate a Velocity view
• Redirect
– Will redirect the request to the specified action after execution
• Chain
– Same as redirect but makes all p
parameters available to the
following action
Result types

Chain result type.

The properties in
GetRandomStringAction
will be available for <xwork>
InvertStringAction. <include file="webwork-default.xml"/>
<package name="default" extends="webwork-default">

<action name=”getRandomString” class=”no.uio...GetRandomStringAction”>


Redirect result type. <result name=”success” type=”chain”>invertString</result>
<result name=”input” type=”redirect”>error.action</result>
Redirects the request
q </action>
to another action after
<action name=”invertString” class=”no.uio...InvertStringAction”>
being executed. <result name=”success” type=”velocity”>word.vm</result>
</action>

</package>
Velocity result type. </xwork>

Generates a HTML
response based on a
Velocity template.
Result types
• Several provided result types integrated with ext tools
– JasperReports
– Flash
– Freemarker
• Custom result types can be defined

<xwork> public class XMLResult


<include name=”webwork-default.xml”/> implements Result
<package name=”default” extends=”webwork-default”> {
public void execute( ActionInvocation invocation )
<result-types> {
<result-type
yp name=”velocityXML”
y Action action = invocation.getAction();
g ();
class=”no.uio.inf5750.example.XMLResult”/>
</result-types> // Print to HTTPServletResponse or
// modify action parameters or whatever..
</package> }
</xwork> }
IoC
• WebWork has its own IoC container - deprecated
• Should be integrated with third party containers (Spring)

webwork.properties webwork.objectFactory = spring

<xwork> <!– Include file and package omitted -->


xwork.xml
<action
action name=”getRandomString”
name getRandomString class
class=”getRandomStringAction”>
getRandomStringAction
The class
Th l property iin
<result name=”success” type=”chain”>invertString</result>
action mappings will
</action>
refer to Spring bean ids
instead of classes.
</xwork>

beans.xml
<bean id=”getRandomStringAction”
class=”no.uio.inf5750.example.action.GetRandomStringAction”/>
Spring configuration file.
Velocity
• Velocity is a template language
– Template: basis for documents with similar structure
– Template language: format defining where variables should be
replaced in a document
• Features include:
– Variable replacement
– Simple control structures
– Method invocation
• Velocity result is included in webwork-default.xml
• Velocity is a runtime language
– Fast
– Error
E prone
Velocity

Variable replacement <html>


<head><title>$word</title></head>
<body>

#if ( $word == ”Hello” )


<div style=”background-color: red”>
Control
#elseif ( $word == ”Goodbye” )
structures <div style =background-color: blue”>
#else
<div>
#end

Method call $word.substring( 10 )

</div>

#foreach ( $word in $words )


<p>$word</p>
#end
Loop
</body>
</html>
WebWork in DHIS 2
• WebWork support project (dhis-support-webwork)
– Filters
– Application logic interceptors
– Custom results
• Webwork commons project (dhis-web-commons)
(dhis web commons)
– Java code for widgets, security, portal
– Interceptor, result configuration
– Packaged as JAR file
• Webwork commons resources project (dhis-web-
commons-resource )
– Web resources like templates, javascripts, css
– Packaged as WAR file
Web modules in DHIS 2
• Templates included in backbone template – main.vm
– Static params in WebWork configuration
• M
Mustt depend
d d on dhi
dhis-web-commons
b and
d dhi
dhis-web-
b
commons-resources
• XWork package must
– Include and extend dhis-web-commons.xml instead of webwork-
default.xml
– Have the same package name as the artifact id
– Have the same namespace as the artifact id
• Development tip: $ mvn jetty:run –war
– Packages and deploys war file to Jetty for rapid development
Resources
• Patrick Lightbody, Jason Carreira: WebWork in Action

• Velocity user guide:


– http://velocity.apache.org/engine/devel/user-guide.html

• Webwork self study


– Taglibs
– Internationalisation
– Validation

You might also like