Professional Documents
Culture Documents
HTTPD iBATIS
Ant Lucene
Cocoon SpamAssassin
Commons Tapestry
http://www.Apache.org/
What is Struts
An extensible "front controller" that
dispatches requests to an "action"
handler
A location handler that transfers control
to another resource
A tag library that helps developers create
interactive form-based applications.
What is Struts 2
Second generation of Struts
Merger of Struts and WebWork 2
A wrapper on top of a generic Command
Pattern implementation (XWork)
How Does Struts
Use XWork?
Demonstrates
Implementing an Action
Configuring Struts
Using the taglib
Type conversion
Error messages
Birthday.java
public class Birthday extends ActionSupport {
private User user;
public User getUser() { return user; }
public int getDaysTillNextBirthday() { ... }
public String execute() throws Exception {
String name = getUser().getName();
if ((name == null) || (name.trim().equals(""))) {
addFieldError("user.name", "You must enter a name.");
}
if (hasErrors()) {
return INPUT;
}
return SUCCESS;
}
}
User.java
public class User {
private String name;
private Date birthday;
Defining Workflow
DefaultWorkflowInterceptor
PrepareInterceptor
ServletConfigInterceptor
ExecuteAndWaitInterceptor
validationWorkflowStack
<interceptor-stack name="validationWorkflowStack">
<interceptor-ref name="defaultStack"/>
<interceptor-ref name="validation"/>
<interceptor-ref name="workflow"/>
</interceptor-stack>
In success.jsp
Hello <s:property value="name"/>!
<s:if test="birthday != null">
<s:property value="daysTillNextBirthday"/>
days till your next birthday.
</s:if>
Applying the ModelDrivenInterceptor
In struts.xml
<package name="Model" extends="struts-default"
namespace="/Model">
<action name="main"
class="com.opensymphony.xwork.ActionSupport">
<result>/Birthday/form.jsp</result>
</action>
<action name="HappyBirthday" class="happy.Birthday">
<interceptor-ref name="model-driven"/>
<interceptor-ref name="defaultStack"/>
<result>/Model/success.jsp</result>
<result name="input">/Model/form.jsp</result>
</action>
</package>
Looking at the ModelDrivenInterceptor
user.name getUser().getName()
user.toString() getUser().toString()
ShoppingCartAware.java:
public interface ShoppingCartAware {
public void setShoppingCart(ShoppingCart cart);
}
ShoppingCart.java:
public interface ShoppingCart {
public void addPet(Pet pet);
public void removePet(Pet pet);
public boolean isEmpty();
public int size();
public List getPets();
}
A Petstore Service
A PetStore service - provides management of our pet inventory
(application scoped)
PetStoreAware.java:
public interface PetStoreAware {
public void setPetStore(PetStore store);
}
PetStore.java:
public interface PetStore {
void savePet(Pet pet);
void removePet(Pet pet);
List getPets();
Pet getPet( long id);
}
A Service Client Action
public class AddToCart implements Action, PetStoreAware,
ShoppingCartAware {
...
public void setPetStore(PetStore ps) { this.petStore = ps; }
public void setShoppingCart(ShoppingCart c)
{ this.cart = c; }
<#include
"/${parameters.templateDir}/${parameters.theme}/controlheader
.ftl" />
<#include "/${parameters.templateDir}/simple/text.ftl" />
<#include "/${parameters.templateDir}/xhtml/controlfooter.ftl"
/>
UI Tag Templates
WebWork comes with default UI
templates
Show the label beside the form field
Show the field errors above the form field
You can also extend the component tag like the other UI tags do create
new UI tags
Reusable Application Modules
You can build reusable modules which can be included in any
Struts application
struts.xml include allows you to merge in external configuration
Struts allows you to include both Actions and templates in a JAR
file
Example: The Struts configuration browser
Adding the Config Browser to Your Application