Web Frameworks: and and
Web Frameworks: and and
and
WebWork
Problem area
• Mixing application logic and markup is bad practise
– Harder to change and maintain
– Error prone
– Harder to re-use
out.println( ”<html>\n<body>” );
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
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
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>
</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
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
<html>
<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>
</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 >
</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"/>
<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"/>
</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>
</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"/>
/>
<interceptors>
<interceptor name=”profiling” class=”no.uio.example.interceptor.ProfilingInterceptor”/>
</interceptors>
</package>
</xwork>
Provided interceptors
• Interceptors perform many tasks in WebWork
– ParametersInterceptor (HTTP request params)
– StaticParametersInterceptor (config params)
– ChainingInterceptor
<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
The properties in
GetRandomStringAction
will be available for <xwork>
InvertStringAction. <include file="webwork-default.xml"/>
<package name="default" extends="webwork-default">
</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
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
</div>