0% found this document useful (0 votes)
952 views61 pages

Hands On Struts2: Ian Roughley, Consultant

Uploaded by

baranikumar_v
Copyright
© Attribution Non-Commercial (BY-NC)
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)
952 views61 pages

Hands On Struts2: Ian Roughley, Consultant

Uploaded by

baranikumar_v
Copyright
© Attribution Non-Commercial (BY-NC)
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/ 61

HANDS ON STRUTS2

Ian Roughley, Consultant


S295739
Learn how to combine Struts2
with complimentary technologies
to provide a complete web application stack

2008 JavaOneSM Conference | java.sun.com/javaone | 2


What I Know

2008 JavaOneSM Conference | java.sun.com/javaone | 3


My Goal

2008 JavaOneSM Conference | java.sun.com/javaone | 4


How Do I Find Them?

2008 JavaOneSM Conference | java.sun.com/javaone | 5


How Do I Order Them?

2008 JavaOneSM Conference | java.sun.com/javaone | 6


How Do I Eat Them?

2008 JavaOneSM Conference | java.sun.com/javaone | 7


Things To Know (Agenda)

How to start a new project


What application features are available
How to access business services
What page template options are there
What Ajax options are available

2008 JavaOneSM Conference | java.sun.com/javaone | 8


Things To Know (Agenda)

How to start a new project


What application features are available
How to access business services
What page template options are there
What Ajax options are available

2008 JavaOneSM Conference | java.sun.com/javaone | 9


Starting a New Project

Manually
• Configure web.xml
• Write configuration and code
Using Maven2
• Generate project
• Working baseline
• Integrated servlet engine

2008 JavaOneSM Conference | java.sun.com/javaone | 10


Basic Elements

Configuration files
• web.xml
• struts.xml
Application elements
• Action class
• JSP template
A way to run the application

2008 JavaOneSM Conference | java.sun.com/javaone | 11


web.xml
<web-app>

<filter>
<filter-name>action2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.FilterDispatcher
</filter-class>
</filter>

<filter-mapping>
<filter-name>action2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

</web-app>

2008 JavaOneSM Conference | java.sun.com/javaone | 12


struts.xml
<struts>

<package name="myPackage" namespace="/"


extends="struts-default">

<action name="index"
class="com.fdar.s2.IndexAction">
<result name="success">/jsp/index.jsp</result>
</action>

</package>

</struts>

2008 JavaOneSM Conference | java.sun.com/javaone | 13


Actions
public class IndexAction {

private String name;

public String getName() { return name; }

public void setName(Stringe newName) {


name = newName;
}

public String execute() throws Exception {



return "success";
}
}

2008 JavaOneSM Conference | java.sun.com/javaone | 14


JavaServer Pages™ (JSP™) technology
<%@taglib prefix="s" uri="/struts-tags" %>

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

<head>
<title>Index</title>
</head>

<body>
<s:form namespace="/" action="index">
<s:textfield label="What is your name?" name="name" />
<s:submit />
</s:form>
</body>
</html>

2008 JavaOneSM Conference | java.sun.com/javaone | 15


So What’s Changed?

Feature Struts1 Struts2

Configuration struts-config.xml struts.xml

Actions Action POJO

Data HTTP Objects / ActionForm Action / Model

Logic Method execute() Any Method

Multiple Logic DispatchAction Any Method

Threading Model Thread-safe POJO

2008 JavaOneSM Conference | java.sun.com/javaone | 16


Putting It Together

2008 JavaOneSM Conference | java.sun.com/javaone | 17


Available Interceptors
exception fileUpload
modelDriven checkbox
params profiling
prepare roles
validation servletConfig
workflow token

2008 JavaOneSM Conference | java.sun.com/javaone | 18


Configuring Interceptors

<struts>
<package name="myPackage" extends="struts-default">

<interceptor-stack name="validationWorkflowStack">
<interceptor-ref name="basicStack"/>
<interceptor-ref name="validation"/>
<interceptor-ref name="workflow"/>
</interceptor-stack>

<default-interceptor-ref name="defaultStack"/>

</package>
</struts>

2008 JavaOneSM Conference | java.sun.com/javaone | 19


Interceptors per Action
<struts>
<package name="myPackage" extends="struts-default">

<action name="test"
class="com.fdar.actions.MyAction">
<result name="success">/result.jsp</result>
<interceptor-ref name="defaultStack"/>
</action>

</package>
</struts>

2008 JavaOneSM Conference | java.sun.com/javaone | 20


Available Result Types
dispatcher chain
freemarker httpheader
velocity redirect
xslt redirectAction
plainText stream

2008 JavaOneSM Conference | java.sun.com/javaone | 21


Plug-ins

Adds new functionality


• Interceptors, Interceptor Stacks
& Result Types
Structure is a web application
• struts-plugin.xml for configuration
• Use JAR for deployment
Place in WEB-INF/lib directory

2008 JavaOneSM Conference | java.sun.com/javaone | 22


Things To Know (Agenda)

How to start a new project


What application features are available
How to access business services
What page template options are there
What Ajax options are available

2008 JavaOneSM Conference | java.sun.com/javaone | 23


Data Conversion

HTML Forms  Java™ technology objects/type

<s:textfield name="price" />

public void setPrice(float price)


public float getPrice()

2008 JavaOneSM Conference | java.sun.com/javaone | 24


Types Converted

Built in types
• boolean, Boolean, char, Character, int, Integer, float,
Float, long, Long, double, Double, and Date

Implement custom converters

2008 JavaOneSM Conference | java.sun.com/javaone | 25


Master-Detail Action
public class MyAction {

private List<Item> items;

public List<Item> getItems() {


return items;
}

public void setItems(List<Item> items) {


this.items = items;
}

}

2008 JavaOneSM Conference | java.sun.com/javaone | 26


Master-Detail Forms
<input type="text" name="items[0].name" />
<input type="text" name="items[0].description" />

<input type="text" name="items[1].name" />


<input type="text" name="items[1].description" />

2008 JavaOneSM Conference | java.sun.com/javaone | 27


Viewing and Updating
<s:iterator value="items" status="stat">

<s:hidden name="items[%{#stat.index}].id" />

Item #<s:property value="#stat.index+1" /> <br/>

<s:textfield label="Name"
name="items[%{#stat.index}].name" />

<s:textfield label="Description"
name="items[%{#stat.index}].description" />

</s:iterator>

2008 JavaOneSM Conference | java.sun.com/javaone | 28


Validation

Annotations for
• Required Fields, Ranges, Email, Expressions, Regular
Expressions,
URLs, Visitor
Can be configured via XML
validate() method in Validateable
Can create custom validators

2008 JavaOneSM Conference | java.sun.com/javaone | 29


Action Configuration
@Validation
public class MyAction {

private String name;

public String getName() {


return name;
}

@RequiredStringValidator(
message="Please enter a name", trim=true)
public void setName(String name) {
this.name = name;
}

}

2008 JavaOneSM Conference | java.sun.com/javaone | 30


Another Action Configuration
@Validation
public class MyAction {

private String name;


public String getName() { return name; }
public void setName(String name) { this.name = name; }

@Validations( expressions = {
@ExpressionValidator(message="Name cannot be Bob",
expression="name!='bob'" )
})
public String execute() throws Exception {

}
}

2008 JavaOneSM Conference | java.sun.com/javaone | 31


Validation Result

No changes to JSP
• errorMessage and errorLabel CSS classes
validation and workflow interceptors
Requires a configured INPUT result

2008 JavaOneSM Conference | java.sun.com/javaone | 32


Things To Know (Agenda)

How to start a new project


What application features are available
How to access business services
What page template options are there
What Ajax options are available

2008 JavaOneSM Conference | java.sun.com/javaone | 33


web.xml
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath*:applicationContext*.xml
</param-value>
</context-param>

<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>

2008 JavaOneSM Conference | java.sun.com/javaone | 34


applicationContext.xml
<beans
xmlns="http://www.springframework.org/schema/beans"
… >

<bean id="userService"
class="com.fdar.s2.services.UserServiceImpl" />

</beans>

public class UserServiceImpl implements UserService {



}

2008 JavaOneSM Conference | java.sun.com/javaone | 35


The Action
public class MyAction extends ActionSupport {

private UserService service;

public void setUserService( UserService userService) {


this.service = userService;
}

public String execute() {



return SUCCESS;
}
}

2008 JavaOneSM Conference | java.sun.com/javaone | 36


Injection Mechanism

Options
• name (default)
• type
• constructor
• auto
<constant value="type"
name="struts.objectFactory.spring.autoWire" />

2008 JavaOneSM Conference | java.sun.com/javaone | 37


Things To Know (Agenda)

How to start a new project


What application features are available
How to access business services
What page template options are there
What Ajax options are available

2008 JavaOneSM Conference | java.sun.com/javaone | 38


SiteMesh

Configured externally
Decorates HTML via URLs
• Can use META tags to specify template
Configuration options include
• Configuration File
• Agent
• Parameter
• Printable

2008 JavaOneSM Conference | java.sun.com/javaone | 39


Update web.xml
<filter>
<filter-name>sitemesh</filter-name>
<filter-class>
com.opensymphony.module.sitemesh.filter.PageFilter
</filter-class>
</filter>

<filter-mapping>
<filter-name>sitemesh</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

2008 JavaOneSM Conference | java.sun.com/javaone | 40


decorators.xml
<decorators defaultdir="/WEB-INF/decorators">

<excludes>
<pattern>/styles/*</pattern>
<pattern>/scripts/*</pattern>
<pattern>/api/*</pattern>
<pattern>/*.html</pattern>
</excludes>

<decorator name="main" page="main.jsp">


<pattern>/*</pattern>
</decorator>

</decorators>

2008 JavaOneSM Conference | java.sun.com/javaone | 41


main.jsp
<%@taglib prefix="decorator"
uri="http://www.opensymphony.com/sitemesh/decorator" %>

<html>
<head>
<title><decorator:title default="Struts Starter"/></title>
<link href="<s:url value='/styles/main.css'/>"
rel="stylesheet" type="text/css" media="all"/>
<decorator:head/>
</head>
<body id="page-home"
onload="<decorator:getProperty property="body.onload"/>">

<decorator:body/>

</body>
</html>

2008 JavaOneSM Conference | java.sun.com/javaone | 42


Apache Tiles

Part of the application configuration


Developer specifies template(s)
Plug-in provided functionality
• Uses Result Type

2008 JavaOneSM Conference | java.sun.com/javaone | 43


Update web.xml
<listener>
<listener-class>
org.apache.struts2.tiles.StrutsTilesListener
</listener-class>
</listener>

2008 JavaOneSM Conference | java.sun.com/javaone | 44


Configure struts.xml
<struts>
<package name="myPackage" extends="tiles-default">
<action name="index"
class="com.fdar.s2.IndexAction">
<result name="success" type="tiles">indexTile</result>
</action>
</package>
</struts>

2008 JavaOneSM Conference | java.sun.com/javaone | 45


tiles.xml
<tiles-definitions>

<definition name="indexTile"
template="/tiles/layout.jsp">
<put name="title" value="Tiles Showcase"/>
<put name="header" value="/tiles/header.jsp"/>
<put name="body" value="/tiles/indexBody.jsp"/>
</definition>

</tiles-definitions>

2008 JavaOneSM Conference | java.sun.com/javaone | 46


layout.jsp
<%@ taglib prefix="tiles"
uri="http://tiles.apache.org/tags-tiles" %>

<html>
<head>
<title><tiles:getAsString name="title"/></title>
</head>

<body>
<tiles:insertAttribute name="header"/>

<p id="body">
<tiles:insertAttribute name="body"/>
</p>
</body>
</html>

2008 JavaOneSM Conference | java.sun.com/javaone | 47


Things To Know

How to start a new project


What application features are available
How to access business services
What page template options are there
What Ajax options are available

2008 JavaOneSM Conference | java.sun.com/javaone | 48


Using Ajax Tag Libraries

Use the ajax theme


• div, a, submit, tabbedpanel, tree
Utilizes the Dojo Toolkit
Will change between 2.0.x and 2.1.x

2008 JavaOneSM Conference | java.sun.com/javaone | 49


Form Example

<div>
<s:form namespace="/search" action="search" method="POST">
<s:textfield label="Search for" name="titlePartial" />
<s:submit label="Go"/>
</s:form>
</div>

<div id="main">

</div>

2008 JavaOneSM Conference | java.sun.com/javaone | 50


Ajax Form Example
<s:head theme="ajax" />

<div>
<s:form namespace="/search" action="search" method="POST">
<s:textfield label="Search for" name="titlePartial" />
<s:submit label="Go" theme="ajax" targets="main" />
</s:form>
</div>

<div id="main">

</div>

2008 JavaOneSM Conference | java.sun.com/javaone | 51


Using the xslt Result Type
<struts>
<package name="services"
namespace="/api" extends="struts-default">

<action name="index"
class="com.fdar.s2.IndexAction">
<result type="xslt">
<param name="exposedValue">items</param>
</result>
</action>

</package>
</struts>

2008 JavaOneSM Conference | java.sun.com/javaone | 52


XML Result
<result>
<item>
<id>1</id>
<name>Book</name>
</item>
<item>
<id>2</id>
<name>Scroll</name>
</item>
</result>

2008 JavaOneSM Conference | java.sun.com/javaone | 53


Using the json Result Type
<struts>
<package name="services"
namespace="/api" extends="json-default">

<default-interceptor-ref name="json" />

<action name="index"
class="com.fdar.s2.IndexAction">
<result type="json">
<param name="root">items</param>
</result>
</action>

</package>
</struts>

2008 JavaOneSM Conference | java.sun.com/javaone | 54


JSON Result
[
{
"id": 1,
"name": "Book"
},
{
"id": 2,
"name": "Scroll"
}
]

2008 JavaOneSM Conference | java.sun.com/javaone | 55


Summary

Simple programming model


Everything you need is available
Plays well with others
Looks good in the latest fashions

2008 JavaOneSM Conference | java.sun.com/javaone | 56


Goal Achieved

2008 JavaOneSM Conference | java.sun.com/javaone | 57


Books

http://www.apress.com/book/view/978159059903
http://www.infoq.com/minibooks/starting-struts2

2008 JavaOneSM Conference | java.sun.com/javaone | 58


For More Information

Practical Apache Struts 2 Web 2.0 Projects


• http://www.apress.com/book/view/1590599039

Apache Struts2 Project


• http://struts.apache.org
Annotations
• http://struts.apache.org/2.x/docs/annotations.html
Ajax Tags
• http://struts.apache.org/2.x/docs/ajax-tags.html
Available Plug-ins
• http://cwiki.apache.org/S2PLUGINS/home.html

2008 JavaOneSM Conference | java.sun.com/javaone | 59


Questions

Ian Roughley

http://www.fdar.com
[email protected]

2008 JavaOneSM Conference | java.sun.com/javaone | 60


Ian Roughley, Consultant
S295739

2008 JavaOneSM Conference | java.sun.com/javaone | 61

You might also like