0% found this document useful (0 votes)
14 views31 pages

Web Work

Uploaded by

Srinath Vasudeva
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views31 pages

Web Work

Uploaded by

Srinath Vasudeva
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 31

WebWork Framework

December, 2006
What is WebWork?
• WebWork is a Model-View-Controlled type of framework.
• It separates logic from view.
• Simplifies Web development by using a powerful "action" concept on the server side.

• WebWork translates between the HTTP

web world and XWork


We b Wo rk

Se rv le t
• XWork is a generic command Dis p a tc h e r

pattern implementation

XWo rk

Commercial in Confidence 2
What does XWork provide?

• Command pattern implementation


– Actions are command objects in XWork
• Adds advanced features
– Interceptors
• Includes setting parameters, workflow, etc
– Results
• Includes one for chaining to another Action
– Powerful expression language – OGNL
– Flexible type conversion
– Metadata driven validation framework

Commercial in Confidence 3
What does WebWork add?

• Adapter for HTTP request / response

• ServletDispatcher translates HTTP requests into Action execution

• Request parameters passed to Action

• Results for servlet redirect & dispatch, Velocity, Freemarker, JasperReports, XSLT, etc.

Commercial in Confidence 4
Results: The “View” in MVC

• Results are what happens after an Action

• Displaying a page -> JSP / Velocity / FreeMarker / JasperReports / XSLT

• Chaining to another Action

• Add your own


– Email? Command line output?

Commercial in Confidence 5
ActionSupport

• Actions are only required to implement the Action interface


• ActionSupport is a useful base class
– Implements Action
– Provides error message support
• Field and Action level messages
• Messages automatically used by UI tags
– Provides message internationalization
• Message bundle for each Action
• Looks up class hierarchy
• UI tags use internationalization support to find text
– All features based on Interfaces, so you can implement your own from scratch!

Commercial in Confidence 6
A “Car Rental Service” example

• Simple requirements.
– Take the Car rental Details, save the details and display result page.
– If the user enters a rentalID, display the rental details.
• Shows.
– Implementing an action.
– Configuring WebWork.
– Using the taglib.
– Type conversion.
– Error messages.

Commercial in Confidence 7
RentalAction.java
public class RentalAction extends ActionSupport {
private RentalService rentalService;
private RentalDetailsVO rentalVO;
public RentalDetailsVO getRentalVO() {
return rentalVO;
}
public void setRentalVO(RentalDetailsVO rentalDetailsVO) {
this.rentalVO = rentalDetailsVO;
}

public String insertRentalDetails() {


if (hasErrors()) {
return INPUT;
}
rentalService. insertRentalDetails(rentalVO);
return SUCCESS;
}
public String getRentalDetails() {
rentalVO = rentalService. getRentalDetails(rentalVO.getRentalID());
return SUCCESS;
}
}

Commercial in Confidence 8
RentalDetailsVO.java
public class RentalDetailsVO {
private String rentalID;
private String vehicalType;
private String vehicleNo;
private Date fromDate;
private Date toDate;
private CustomerDetailsVO customer;

public CustomerDetailsVO getCustomer() { return customer; }


public void setCustomer(CustomerDetailsVO customer) { this.customer = customer;
}
public String getRentalID() { return rentalID; }
public void setRentalID(String rentalID) { this.rentalID = rentalID; }
…..
}

Commercial in Confidence 9
InsertRentalDetails.ftl

Rental ID:<input type ="text" name="rentalVo.rentalID" value=“${rentalVo.rentalID}”>


From Date:<input type="text" name="rentalVo.fromDate" value=“${rentalVo. fromDate?date}”>
To Date: <input type ="text" name="rentalVo.toDate"value=“${rentalVo. fromDate?date}”>

$action.getText (‘Customer_No_Label’)< input type="text" name="rentalVo.customer.custNo">


Customer Name:< input type="text" name="rentalVo.customer.custName“
value=“<@ww.property value='rentalVo.customer.custName'/>”>

Residence:<input type="hidden" name="rentalVo.customer.address[0].purpose" value="Residence">


Line1:<input type="text" name="rentalVo.customer.address[0].line1“>

Commercial in Confidence 10
Success_apex.ftl

<html>
<body>
<#if message?exists>
<@ww.property value='message'/>
<#else>
${action.getText('DataSaved')}
</#if>
</body>
</html>

Commercial in Confidence 11
Xwork.xml for Car Rental
<!DOCTYPE xwork PUBLIC "-//OpenSymphony Group//XWork 1.0//EN“
"http://www.opensymphony.com/xwork/xwork-1.0.dtd">
<xwork>
<include file="webwork-default.xml"/>
<package name="Rental" extends="webwork-default">
<action name="inputScreen" class="com.opensymphony.xwork.ActionSupport">
<interceptor-ref name="defaultStack"/>
<result name="success" type="dispatcher">/WEB-INF/ftl/InputScreen.ftl</result>
</action>
<action name="insertRentalDetails" class="com.sc.RentalActions" method="insertRentalDetails">
<interceptor-ref name="defaultStack"/>
<interceptor-ref name=“validation"/>
<result name="success" type="dispatcher">/WEB-INF/ftl/Success_apex.ftl</result>
<result name="input" type="dispatcher">/WEB-INF/ftl/InputScreen.ftl</result>
</action>
<action name="filterScreen" class="com.opensymphony.xwork.ActionSupport" >
<interceptor-ref name="defaultStack"/>
<result name="success" type="dispatcher">/WEB-INF/ftl/filterScreen.ftl</result>
</action>
<action name="getRentalDetails" class="com.sc.RentalActions" method="getRentalDetails">
<interceptor-ref name="defaultStack"/>
<result name="success" type="dispatcher">/WEB-INF/ftl/getDetailsScreen.ftl</result>
<result name="error" type="dispatcher">/WEB-INF/ftl/Error_apex.ftl</result>
</action>
</package>
</xwork>
Commercial in Confidence 12
web.xml for Car Rental
<web-app>
<servlet>
<servlet-name>action</servlet-name>
<servlet-class>com.opensymphony.webwork.dispatcher.ServletDispatcher</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.action</url-pattern>
</servlet-mapping>
<taglib>
<taglib-uri>webwork</taglib-uri>
<taglib-location>/WEB-INF/lib/webwork.jar</taglib-location>
</taglib>
</web-app>

Commercial in Confidence 13
Interceptors

XWo rk • Interceptors allow custom code into the


call stack
• Much of the core functionality of XWork
Inte rce pto r and WebWork is implemented as
Inte rce pto r Interceptors
Inte rce pto r • Add custom Interceptors
Actio n

Re s ult

Commercial in Confidence 14
Interceptor Stacks

• Interceptors can be grouped into named Interceptor Stacks


• 2 defined in webwork-default.xml
• defaultStack
<interceptor-stack name="defaultStack">
<interceptor-ref name="static-params"/>
<interceptor-ref name="params"/>
<interceptor-ref name="conversionError"/>
</interceptor-stack>
• validationWorkflowStack
<interceptor-stack name="validationWorkflowStack">
<interceptor-ref name="defaultStack"/>
<interceptor-ref name="validation"/>
<interceptor-ref name="workflow"/>
</interceptor-stack>

• Stacks can be built of other stacks and interceptors

Commercial in Confidence 15
Model-Driven vs. Field-Driven

• 2 types of Actions possible:


1. Model-driven
• Action has methods returning your model classes (myAction.getRentalVO())
• Fields in the view are fields of your model
• Excellent because it allows model reuse
2. Field-driven
• Action has fields of its own, which are fields in the view
• execute() collates fields and interacts with the model
• Views refer to action fields
• Useful where form is not parallel to model

Commercial in Confidence 16
ModelDriven Interface

• XWork / WebWork also supports model-driven Actions more directly


• The ModelDriven Interface has one method:
public Object getModel()

• Properties of the model will be directly available, i.e. “rentalID” instead


of “rentalVO.rentalID”
• Applies to UI tags, form field names, etc.

Commercial in Confidence 17
Making Rental ModelDriven

• Make the class implement ModelDriven


• Change getRentalVO() to getModel()
public class RentalAction extends ActionSupport implements ModelDriven {

public Object getModel() {
return rentalVO;
}
}

Commercial in Confidence 18
ModelDriven: Changes to the insert.ftl

• Change “rentalVo.rentalID”, etc. to just “rentalID”

<webwork:textfield label=“Rental ID" name=" rentalID " value="rentalID"/>


<webwork:textfield label=“From Date" name="fromDate" value="fromDate"/>

Commercial in Confidence 19
Applying the ModelDrivenInterceptor

• In xwork.xml

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


<action name="main" class="com.opensymphony.xwork.ActionSupport">
<result name="success" type="dispatcher">/WEB-INF/ftl/InsertScreen.ftl</result>
</action>
<action name=“insertRentalDetals" class=“com.sc.RentalAction">
<interceptor-ref name="model-driven"/>
<interceptor-ref name="defaultStack"/>
<result name="success" type="dispatcher">/WEB-INF/ftl/InsertScreen.ftl</result>
<result name="input" type="dispatcher">/WEB-INF/ftl/Success_apex.ftl</result>
</action>
</package>

Commercial in Confidence 20
What is the ValueStack?

OGNL Ex p re s s io n
• The ValueStack builds a stack of
objects
• Objects are used to find property
values Valu e Stack
• The ValueStack allows the

Fin d Pro p e rtie s


expression language to find
property values across multiple
Mo d e l
objects

Fin d Pro p e rtie s


Actio n

Commercial in Confidence 21
How is the ValueStack used?

• The Action instance is always pushed onto the ValueStack


• The Model is pushed on by the ModelInterceptor
• The UI tags use it to push values on during their scope and evaluate
expressions
– The <ww:iterator> tag pushes the current item onto the stack
– The <ww:bean> tag pushes a bean instance on
– The <ww:property> tag evaluates an expression against the ValueStack
– All tag attribute values are evaluated against the stack when being set onto the tag
instances

Commercial in Confidence 22
The OGNL expression language

• For expressions WW uses OGNL (Object Graph Navigation Language)


– an expression and binding language for getting and setting properties of
Java objects
– Normally the same expression is used for both getting and setting the value
of a property
– Easy to learn, yet powerful
– Incrementally compiled expressions - fast!
– Embedded everywhere – views, ValueStack, *.xml
– Independently run Open Source project - http://www.ognl.org

Commercial in Confidence 23
OGNL samples

OGNL Result

user.name getUser().getName()

user.toString() getUser().toString()

item.categories[0] First element of Categories collection

@com.example.Test@foo() Calls the static foo() method on the com.example.Test class

name in {null,”fred”} True if name is null or “fred”

categories.{name} Calls getName() on each Category in the collection, returning a new


collection (projection)

Commercial in Confidence 24
The XWork Validation Framework

• Separates validation from Action classes


• Allows for different validations in different contexts for the
same object
• Provides hooks for localized validation messages
• 2 types of validators, Object level and field level

Commercial in Confidence 25
RentalAction-insertRentalDetails-validation.xml

• Validation file in the same package as the class


• Defines one field validator and the error message to add if it fails
<!DOCTYPE validators PUBLIC "-//OpenSymphony Group//XWork Validator 1.0.2//EN"
"http://www.opensymphony.com/xwork/xwork-validator-1.0.2.dtd">
<validators>
<field name="rentalVO.rentalID">
<field-validator type="stringRequired" short-circuit="true">
<message key=“rentalID_mandatory.msg"/>
</field-validator>
<field-validator type="stringlength" short-circuit="true">
<param name="minLength">0</param>
<param name="maxLength">8</param>
<message key=" rentalID_sizeExceeds.msg "/>
</field-validator>
</field>
<field name="rentalVo.fromDate">
<field-validator type="checkdate">
<param name="afterDate">01/01/2006</param>
<message key="fromDateIllegal.msg"/>
</field-validator>
</field>
</validators>

Commercial in Confidence 26
Bundled Validators
Validator Result

RequiredField field != null

RequiredString field != null && string.length() > 0

IntRange Integer in a given range

DateRange Date in a given range

Email Valid email field

URL Valid URL field

Expression / Any OGNL expression evaluates to true. Eg. pet.name != “dog”


FieldExpression Allows you to create very powerful validations using just XML and your existing model

<validators>
<validator name="required" class="com.opensymphony.xwork.validator.validators.RequiredFieldValidator"/>
<validator name="requiredstring" class="com.opensymphony.xwork.validator.validators.RequiredStringValidator"/>
<validator name="int" class="com.opensymphony.xwork.validator.validators.IntRangeFieldValidator"/>
<validator name="double" class="com.opensymphony.xwork.validator.validators.DoubleRangeFieldValidator"/>
<validator name="conversion" class="com.opensymphony.xwork.validator.validators.ConversionErrorFieldValidator"/>
<validator name="stringlength" class="com.opensymphony.xwork.validator.validators.StringLengthFieldValidator"/>
<validator name="requireddouble" class="com.firstapex.fic.auxmaster.validators.DoubleMandatoryValidator"/>
<validator name="requireddate" class="com.firstapex.fic.auxmaster.validators.DateValidator"/>
<validator name="illegal" class="com.firstapex.fic.claims.validators.MiscTypeValidator"/>
<validator name="AuxCodeValidator" class="com.firstapex.fic.auxmaster.validators.AuxCodeValidator"/>
<validator name="AuxDataTypeValidator" class="com.firstapex.fic.claims.validators.AuxDataTypeValidator"/>
<validator name="PartyDataTypeValidator" class="com.firstapex.fic.claims.validators.PartyDataTypeValidator"/>
<validator name="ClaimNumberValidator" class="com.firstapex.fic.claims.validators.ClaimNumberValidator"/>
</validators>

Commercial in Confidence 27
Execution

Commercial in Confidence 28
Web works Architecture

Commercial in Confidence 29
Resources

• http://www.opensymphony.com
– Documentation
– Forums

Q & A ??

Commercial in Confidence 30

You might also like