Migrating to Struts2 of Struts1

Preview:

Citation preview

Migrating to Struts 2A tutorial for Struts 1 Developers

Struts University Series

Migrating to Struts 2A tutorial for Struts 1 Developers

Struts University Series

Ted? Who's Ted?

Apache Software Foundation Member Apache Struts & Apache iBATIS PMCsJUnit in Action, Struts in Action, and Professional JSP Site DesignConsult with teams throughout the United StatesStill working with the OK DEQ

Ted? Who's Ted?

Apache Software Foundation Member Apache Struts & Apache iBATIS PMCsJUnit in Action, Struts in Action, and Professional JSP Site DesignConsult with teams throughout the United StatesStill working with the OK DEQ

Ted? Who's Ted?

Apache Software Foundation Member Apache Struts & Apache iBATIS PMCsJUnit in Action, Struts in Action, and Professional JSP Site DesignConsult with teams throughout the United StatesStill working with the OK DEQ

Ted? Who's Ted?

Apache Software Foundation Member Apache Struts & Apache iBATIS PMCsJUnit in Action, Struts in Action, and Professional JSP Site DesignConsult with teams throughout the United StatesStill working with the OK DEQ

Ted? Who's Ted?

Apache Software Foundation Member Apache Struts & Apache iBATIS PMCsJUnit in Action, Struts in Action, and Professional JSP Site Design.Consult with teams throughout the United StatesStill working with the OK DEQ

Ted? Who's Ted?

Apache Software Foundation Member Apache Struts & Apache iBATIS PMCsJUnit in Action, Struts in Action, and Professional JSP Site Design.Consult with teams throughout the United StatesStill working with the OK DEQ

Migrating to Struts 2

Help Struts 1 developers become knowledgeable Struts 2 developers as quickly as possibleMigrate Struts 1 application to Struts 2Intended for experienced Java web developers

Migrating to Struts 2

Help Struts 1 developers become knowledgeable Struts 2 developers as quickly as possibleMigrate Struts 1 application to Struts 2Intended for experienced Java web developers

Migrating to Struts 2

Help Struts 1 developers become knowledgeable Struts 2 developers as quickly as possibleMigrate Struts 1 application to Struts 2Intended for experienced Java web developers

Migrating to Struts 2

Help Struts 1 developers become knowledgeable Struts 2 developers as quickly as possibleMigrate Struts 1 application to Struts 2Intended for experienced Java web developers

Baseline Questions

What is Struts 2? How are Struts 1 and Struts alike?What's changed in Struts 2? Is Struts 1 obsolete?Is it difficult to migrate?Why are "plain old Java objects" important?Where should a migration begin?

Baseline Questions

What is Struts 2? How are Struts 1 and Struts alike?What's changed in Struts 2? Is Struts 1 obsolete?Is it difficult to migrate?Why are "plain old Java objects" important?Where should a migration begin?

Baseline Questions

What is Struts 2? How are Struts 1 and Struts alike?What's changed in Struts 2? Is Struts 1 obsolete?Is it difficult to migrate?Why are "plain old Java objects" important?Where should a migration begin?

Baseline Questions

What is Struts 2? How are Struts 1 and Struts alike?What's changed in Struts 2? Is Struts 1 obsolete?Is it difficult to migrate?Why are "plain old Java objects" important?Where should a migration begin?

Baseline Questions

What is Struts 2? How are Struts 1 and Struts alike?What's changed in Struts 2? Is Struts 1 obsolete?Is it difficult to migrate?Why are "plain old Java objects" important?Where should a migration begin?

Baseline Questions

What is Struts 2? How are Struts 1 and Struts alike?What's changed in Struts 2? Is Struts 1 obsolete?Is it difficult to migrate?Why are "plain old Java objects" important?Where should a migration begin?

Baseline Questions

What is Struts 2? How are Struts 1 and Struts alike?What's changed in Struts 2? Is Struts 1 obsolete?Is it difficult to migrate?Why are "plain old Java objects" important?Where should a migration begin?

Baseline Questions

What is Struts 2? How are Struts 1 and Struts alike?What's changed in Struts 2? Is Struts 1 obsolete?Is it difficult to migrate?Why are "plain old Java objects" important?Where should a migration begin?

What is Struts 2?

Free open-source framework Two major versions

Struts 1 Proven solution to common problems

Struts 2 Elegant solution to difficult problems

What is Struts 2?

Free open-source framework Two major versions

Struts 1 Proven solution to common problems

Struts 2 Elegant solution to difficult problems

What is Struts 2?

Free open-source framework Two major versions

Struts 1 Proven solution to common problems

Struts 2 Elegant solution to difficult problems

What is Struts 2?

Free open-source framework Two major versions

Struts 1 Proven solution to common problems

Struts 2 Elegant solution to difficult problems

How are Struts 1 and Struts 2 alike?

A "request" handler maps Java classes to web application URIs

A "response" handler maps logical names to web resources

A tag library creates rich, form-based applications

How are Struts 1 and Struts 2 alike?

A "request" handler maps Java classes to web application URIs

A "response" handler maps logical names to web resources

A tag library creates rich, form-based applications

How are Struts 1 and Struts 2 alike?

A "request" handler maps Java classes to web application URIs

A "response" handler maps logical names to web resources

A tag library creates rich, form-based applications

How are Struts 1 and Struts 2 alike?

A "request" handler maps Java classes to web application URIs

A "response" handler maps logical names to web resources

A tag library creates rich, form-based applications

What's changed in Struts 2?

Smarter! Improved Design Intelligent Defaults Enhanced Results Enhanced Tags First-class AJAX support Stateful Checkboxes QuickStart

What's changed in Struts 2?

Smarter! Improved Design Intelligent Defaults Enhanced Results Enhanced Tags First-class AJAX support Stateful Checkboxes QuickStart

What's changed in Struts 2?

Smarter! Improved Design Intelligent Defaults Enhanced Results Enhanced Tags First-class AJAX support Stateful Checkboxes QuickStart

What's changed in Struts 2?

Smarter! Improved Design Intelligent Defaults Enhanced Results Enhanced Tags First-class AJAX support Stateful Checkboxes QuickStart

What's changed in Struts 2?

Smarter! Improved Design Intelligent Defaults Enhanced Results Enhanced Tags First-class AJAX support Stateful Checkboxes QuickStart

What's changed in Struts 2?

Smarter! Improved Design Intelligent Defaults Enhanced Results Enhanced Tags First-class AJAX support Stateful Checkboxes QuickStart

What's changed in Struts 2?

Smarter! Improved Design Intelligent Defaults Enhanced Results Enhanced Tags First-class AJAX support Stateful Checkboxes QuickStart

What's changed in Struts 2?

Smarter! Improved Design Intelligent Defaults Enhanced Results Enhanced Tags First-class AJAX support Stateful Checkboxes QuickStart

What's changed in Struts 2?

Smarter! Improved Design Intelligent Defaults Enhanced Results Enhanced Tags First-class AJAX support Stateful Checkboxes QuickStart

What's Changed in Struts 2?

Easier! Easy-to-test Actions Easy-to-customize controller Easy-to-tweak tags Easy cancel handling Easy Spring integration Easy plugins

What's Changed in Struts 2?

Easier! Easy-to-test Actions Easy-to-customize controller Easy-to-tweak tags Easy cancel handling Easy Spring integration Easy plugins

What's Changed in Struts 2?

Easier! Easy-to-test Actions Easy-to-customize controller Easy-to-tweak tags Easy cancel handling Easy Spring integration Easy plugins

What's Changed in Struts 2?

Easier! Easy-to-test Actions Easy-to-customize controller Easy-to-tweak tags Easy cancel handling Easy Spring integration Easy plugins

What's Changed in Struts 2?

Easier! Easy-to-test Actions Easy-to-customize controller Easy-to-tweak tags Easy cancel handling Easy Spring integration Easy plugins

What's Changed in Struts 2?

Easier! Easy-to-test Actions Easy-to-customize controller Easy-to-tweak tags Easy cancel handling Easy Spring integration Easy plugins

What's Changed in Struts 2?

Easier! Easy-to-test Actions Easy-to-customize controller Easy-to-tweak tags Easy cancel handling Easy Spring integration Easy plugins

What's Changed in Struts 2

POJO-ier! POJO forms POJO Actions

What's Changed in Struts 2

POJO-ier! POJO forms POJO Actions

What's Changed in Struts 2

POJO-ier! POJO forms POJO Actions

Is Struts 1 obsolete?

Robust and vibrant communityThousands of applications in production New 1.3.5 releaseBut, is it time to try Struts 2?

Is Struts 1 obsolete?

Robust and vibrant communityThousands of applications in production New 1.3.5 releaseBut, is it time to try Struts 2?

Is Struts 1 obsolete?

Robust and vibrant communityThousands of applications in production New 1.3.5 releaseBut, is it time to try Struts 2?

Is Struts 1 obsolete?

Robust and vibrant communityThousands of applications in production New 1.3.5 releaseBut, is it time to try Struts 2?

Is Struts 1 obsolete?

Robust and vibrant communityThousands of applications in production New 1.3.5 releaseBut, is it time to try Struts 2?

Is it difficult to migrate?

Not difficult but non-trivial Migrating Actions takes time and effortStable: may not be worth the effortGrowth: good investment

Is it difficult to migrate?

Not difficult but non-trivial Migrating Actions takes time and effortStable: may not be worth the effortGrowth: good investment

Is it difficult to migrate?

Not difficult but non-trivial Migrating Actions takes time and effortStable: may not be worth the effortGrowth: good investment

Is it difficult to migrate?

Not difficult but non-trivial Migrating Actions takes time and effortStable: may not be worth the effortGrowth: good investment

Is it difficult to migrate?

Not difficult but non-trivial Migrating Actions takes time and effortStable: may not be worth the effortGrowth: good investment

Is it difficult to migrate?

Not difficult but non-trivial Migrating Actions takes time and effortStable: may not be worth the effortGrowth: good investmentStruts 2 is smarter, easier

Is it difficult to migrate?

Not difficult but non-trivial Migrating Actions takes time and effortStable: may not be worth the effortGrowth: good investmentStruts 2 is smarter, easier, and POJO-ier

Why are POJOs important?

No "extra" objects Instantiate Actions with new or Spring Simulate servlet state with HashMap

Rich input properties No ActionForms! Use Action or any other object Non-String properties are not a problem

Why are POJOs important?

No "extra" objects Instantiate Actions with new or Spring Simulate servlet state with HashMap

Rich input properties No ActionForms! Use Action or any other object Non-String properties are not a problem

Why are POJOs important?

No "extra" objects Instantiate Actions with new or Spring Simulate servlet state with HashMap

Rich input properties No ActionForms! Use Action or any other object Non-String properties are not a problem

Struts University Series

How are Struts 1 and Struts 2 alike?

A "r******" handler maps Java classes to web application URIs

A "r*******" handler maps logical names to web resources

A *** library creates rich, form-based applications

How are Struts 1 and Struts 2 alike?

A "request" handler maps Java classes to web application URIs

A "r*******" handler maps logical names to web resources

A *** library creates rich, form-based applications

How are Struts 1 and Struts 2 alike?

A "request" handler maps Java classes to web application URIs

A "response" handler maps logical names to web resources

A *** library creates rich, form-based applications

How are Struts 1 and Struts 2 alike?

A "request" handler maps Java classes to web application URIs

A "response" handler maps logical names to web resources

A tag library creates rich, form-based applications

How are Struts 1 and Struts 2 alike?

A "request" handler maps Java classes to web application URIs

A "response" handler maps logical names to web resources

A tag library creates rich, form-based applications

Where should a migration begin?

Add Struts 2 JARs Migrate application page at a timeUse both versions at the same timeArchitectures can remain the sameRemove Struts 1 red tapeSwap in new tags

Where should a migration begin?

Add Struts 2 JARs Migrate application page at a timeUse both versions at the same timeArchitectures can remain the sameRemove Struts 1 red tapeSwap in new tags

Where should a migration begin?

Add Struts 2 JARs Migrate application page at a timeUse both versions at the same timeArchitectures can remain the sameRemove Struts 1 red tapeSwap in new tags

Where should a migration begin?

Add Struts 2 JARs Migrate application page at a timeUse both versions at the same timeArchitectures can remain the sameRemove Struts 1 red tapeSwap in new tags

Where should a migration begin?

Add Struts 2 JARs Migrate application page at a timeUse both versions at the same timeArchitectures can remain the sameRemove Struts 1 red tapeSwap in new tags

Where should a migration begin?

Add Struts 2 JARs Migrate application page at a timeUse both versions at the same timeArchitectures can remain the sameRemove Struts 1 red tapeSwap in new tags

Where should a migration begin?

Add Struts 2 JARs Migrate application page at a timeUse both versions at the same timeArchitectures can remain the sameRemove Struts 1 red tapeSwap in new tags

Where should a migration begin?

Migrate "Hello World" applicationDisplays message in two languagesAdd input form for custom messageValidate data entry

Where should a migration begin?

Migrate "Hello World" applicationDisplays message in two languagesAdd input form for custom messageValidate data entry

Where should a migration begin?

Migrate "Hello World" applicationDisplays message in two languagesAdd input form for custom messageValidate data entry

Where should a migration begin?

Migrate "Hello World" applicationDisplays message in two languagesAdd input form for custom messageValidate data entry

Where should a migration begin?

Update web.xml to add ...1)The Struts 2 Filter2)A filter mapping3)The Spring Listener

Where should a migration begin?

Update web.xml to add ...1)The Struts 2 Filter2)A filter mapping3)The Spring Listener

Where should a migration begin?

Update web.xml to add ...1)The Struts 2 Filter2)A filter mapping3)The Spring Listener

Where should a migration begin?

Update web.xml to add ...1)The Struts 2 Filter2)A filter mapping3)The Spring Listener

Where should a migration begin?

Update web.xml to add ...1)The Struts 2 Filter2)A filter mapping3)The Spring Listener

<web-app> <!-- Struts 2 --> <filter> <filter-name>struts2</filter-name> <filter-class> org.apache.struts2.dispatcher.FilterDispatcher </filter-class> </filter> <filter-mapping><filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <listener> <listener-class> org.springframework.web.context.ContextLoaderListener </listener-class> </listener>

<!-- Struts 1 --> <servlet> <servlet-name>action</servlet-name> <servlet-class> org.apache.struts.action.ActionServlet </servlet-class> <init-param> <param-name>config</param-name> <param-value> /WEB-INF/classes/struts-config.xml </param-value> </init-param> <load-on-startup>2</load-on-startup> </servlet> <servlet-mapping> <servlet-name>action</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping>

<!-- Either version --> <welcome-file-list> <welcome-file>index.html</welcome-file> </welcome-file-list></web-app>

<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"> <beans></beans>

applicationContext.xml

Lets code it!

Where should a migration begin?

Struts 1 actions handle *.do Struts 2 actions handle *.actionMigrate one to the other, until there's nothing left to "do"!

Where should a migration begin?

Struts 1 actions handle *.do Struts 2 actions handle *.actionMigrate one to the other, until there's nothing left to "do"!

Where should a migration begin?

Struts 1 actions handle *.do Struts 2 actions handle *.actionMigrate one to the other, until there's nothing left to "do"!

Where should a migration begin?

Struts 1 actions handle *.do Struts 2 actions handle *.actionMigrate one to the other, until there's nothing left to "do"!

Struts University Series

Where should a migration begin?

For dependency injection, by default, Struts 2 uses HiveMind Spring Castle Pico

Where should a migration begin?

For dependency injection, by default, Struts 2 uses HiveMind Spring Castle Pico

Where should a migration begin?

Update web.xml to add ... The Struts 2 ****** A filter ******* The ****** Listener

Where should a migration begin?

Update web.xml to add ... The Struts 2 ****** A filter ******* The ****** Listener

Where should a migration begin?

Update web.xml to add ... The Struts 2 Filter A filter ******* The ****** Listener

Where should a migration begin?

Update web.xml to add ... The Struts 2 Filter A filter mapping The ****** Listener

Where should a migration begin?

Update web.xml to add ... The Struts 2 Filter A filter mapping The Spring Listener

Where should a migration begin?

Update web.xml to add ... The Struts 2 Filter A filter mapping The Spring Listener

Is the Struts 2 configuration file different?

Struts 2 configuration file is streamlinedLess to configureElement names are shorterStruts 1 is "struts-config.xml"Struts 2 is "struts.xml"

Is the Struts 2 configuration file different?

Struts 2 configuration file is streamlinedLess to configureElement names are shorterStruts 1 is "struts-config.xml"Struts 2 is "struts.xml"

Is the Struts 2 configuration file different?

Struts 2 configuration file is streamlinedLess to configureElement names are shorterStruts 1 is "struts-config.xml"Struts 2 is "struts.xml"

Is the Struts 2 configuration file different?

Struts 2 configuration file is streamlinedLess to configureElement names are shorterStruts 1 is "struts-config.xml"Struts 2 is "struts.xml"

Is the Struts 2 configuration file different?

Struts 2 configuration file is streamlinedLess to configureElement names are shorterStruts 1 is "struts-config.xml"Struts 2 is "struts.xml"

Is the Struts 2 configuration file different?

Struts 2 configuration file is streamlinedLess to configureElement names are shorterStruts 1 is "struts-config.xml"Struts 2 is "struts.xml"

<?xml version="1.0" encoding="ISO-8859-1" ?><!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.3//EN" "http://struts.apache.org/dtds/struts-config_1_3.dtd">

<struts-config> <form-beans> <form-bean name="HelloForm" type="actions.HelloForm"> </form-bean> </form-beans> <action-mappings> <action path="/Hello" name="HelloForm" type="actions.HelloAction" validate="false"> <forward name="success" path="/HelloPage.jsp"/> </action> </action-mappings> <message-resources parameter="resources"/> </struts-config>

<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd">

<struts> <include file="struts-default.xml"/> <package name="hello-default" extends="struts-default"> <action name="Hello" class="actions.Hello"> <result>/Hello.jsp</result> </action> </package> </struts>

Lets code it!

To migrate configuration

Replace the DTD Change <struts-config> to <struts>Add <include file="struts-default.xml"/>Remove the <form-beans> element Change <action-mappings> to <package name="hello-default" extends="struts-default">Update each <action> element

To migrate configuration

Replace the DTD Change <struts-config> to <struts>Add <include file="struts-default.xml"/>Remove the <form-beans> element Change <action-mappings> to <package name="hello-default" extends="struts-default">Update each <action> element

To migrate configuration

Replace the DTD Change <struts-config> to <struts>Add <include file="struts-default.xml"/>Remove the <form-beans> element Change <action-mappings> to <package name="hello-default" extends="struts-default">Update each <action> element

To migrate configuration

Replace the DTD Change <struts-config> to <struts>Add <include file="struts-default.xml"/>Remove the <form-beans> element Change <action-mappings> to <package name="hello-default" extends="struts-default">Update each <action> element

To migrate configuration

Replace the DTD Change <struts-config> to <struts>Add <include file="struts-default.xml"/>Remove the <form-beans> element Change <action-mappings> to <package name="hello-default" extends="struts-default">Update each <action> element

To migrate configuration

Replace the DTD Change <struts-config> to <struts>Add <include file="struts-default.xml"/>Remove the <form-beans> element Change <action-mappings> to <package name="hello-default" extends="struts-default">Update each <action> element

To migrate configuration

Replace the DTD Change <struts-config> to <struts>Add <include file="struts-default.xml"/>Remove the <form-beans> element Change <action-mappings> to <package name="hello-default" extends="struts-default">Update each <action> element

To migrate configuration

To update each <action> element, we 1)Remove from <action> the "name" attribute2)Change the <action> "path" attribute to

"name", and "type" to "class"3)Change the <forward> element into a

<result> element.

To migrate configuration

To update each <action> element, we 1)Remove from <action> the "name" attribute2)Change the <action> "path" attribute to

"name", and "type" to "class"3)Change the <forward> element into a

<result> element.

To migrate configuration

To update each <action> element, we 1)Remove from <action> the "name" attribute2)Change the <action> "path" attribute to

"name", and "type" to "class"3)Change the <forward> element into a

<result> element.

To migrate configuration

To update each <action> element, we 1)Remove from <action> the "name" attribute2)Change the <action> "path" attribute to

"name", and "type" to "class"3)Change the <forward> element into a

<result> element.

Struts University Series

Is the Struts 2 configuration file different?

Struts 2 configuration file is (less/more) to configureStruts 2 configuration file is named "******.xml"Most Struts 2 configurations include the “struts-*******.xml”

Is the Struts 2 configuration file different?

Struts 2 configuration file is (less/more) to configureStruts 2 configuration file is named "******.xml"Most Struts 2 configurations include the “struts-*******.xml”

Is the Struts 2 configuration file different?

Struts 2 configuration file is (less/more) to configureStruts 2 configuration file is named "struts.xml"Most Struts 2 configurations include the “struts-*******.xml”

Is the Struts 2 configuration file different?

Struts 2 configuration file is (less/more) to configureStruts 2 configuration file is named "struts.xml"Most Struts 2 configurations include the “struts-default.xml”

Is the Struts 2 configuration file different?

Struts 2 configuration file is (less/more) to configureStruts 2 configuration file is named "struts.xml"Most Struts 2 configurations include the “struts-default.xml”

To migrate configuration

Replace the DTD ****** <struts-config> to <struts>Add <include file="struts-default.xml"/>****** the <form-beans> element Change <action-mappings> to <package name="hello-default" extends="struts-default">****** each <action> element

To migrate configuration

Replace the DTD Change <struts-config> to <struts>Add <include file="struts-default.xml"/>****** the <form-beans> element Change <action-mappings> to <package name="hello-default" extends="struts-default">****** each <action> element

To migrate configuration

Replace the DTD Change <struts-config> to <struts>Add <include file="struts-default.xml"/>Remove the <form-beans> element Change <action-mappings> to <package name="hello-default" extends="struts-default">****** each <action> element

To migrate configuration

Replace the DTD Change <struts-config> to <struts>Add <include file="struts-default.xml"/>Remove the <form-beans> element Change <action-mappings> to <package name="hello-default" extends="struts-default">Update each <action> element

To migrate configuration

Replace the DTD Change <struts-config> to <struts>Add <include file="struts-default.xml"/>Remove the <form-beans> element Change <action-mappings> to <package name="hello-default" extends="struts-default">Update each <action> element

Why so many changes?

Obsolesce We just plain don't need them anymore

Consistency Attribute names are applied consistently

Comprehension Concise and easier to understand

Why so many changes?

Obsolesce We just plain don't need them anymore

Consistency Attribute names are applied consistently

Comprehension Concise and easier to understand

Why so many changes?

Obsolesce We just plain don't need them anymore

Consistency Attribute names are applied consistently

Comprehension Concise and easier to understand

Why so many changes?

Obsolesce We just plain don't need them anymore

Consistency Attribute names are applied consistently

Comprehension Concise and easier to understand

Intelligent Defaults

When an essential value is omitted, the system automatically provides

a predefined value, eliminating the need to explicitly qualify

each and every aspect of a declaration. (Adapted from the CULE Knowledgebase)

Intelligent Defaults

When an essential value is omitted, the system automatically provides

a predefined value, eliminating the need to explicitly qualify

each and every aspect of a declaration. (Adapted from the CULE Knowledgebase)

Intelligent Defaults

When an essential value is omitted, the system automatically provides

a predefined value, eliminating the need to explicitly qualify

each and every aspect of a declaration. (Adapted from the CULE Knowledgebase)

Intelligent Defaults

When an essential value is omitted, the system automatically provides

a predefined value, eliminating the need to explicitly qualify

each and every aspect of a declaration. (Adapted from the CULE Knowledgebase)

Intelligent Defaults<forward name="success" path="/Hello.jsp"/>

<result>/Hello.jsp</result>

The <result> name defaults to "success"The <result> content defaults to "location" (e.g. path)The <result> type defaults to "dispatch"

Intelligent Defaults<forward name="success" path="/Hello.jsp"/>

<result>/Hello.jsp</result>

The <result> name defaults to "success"The <result> content defaults to "location" (e.g. path)The <result> type defaults to "dispatch"

Intelligent Defaults<forward name="success" path="/Hello.jsp"/>

<result>/Hello.jsp</result>

The <result> name defaults to "success"The <result> content defaults to "location" (e.g. path)The <result> type defaults to "dispatch"

Intelligent Defaults<result name="cancel" type="redirect"> /Welcome.jsp</result>

Changing the result name and type

Struts University Series

Why so many changes?

O******** We just plain don't need them anymore

C********** Attribute names are uniform

C************ Concise and easier to understand

Why so many changes?

Obsolesce We just plain don't need them anymore

C********** Attribute names are uniform

C************ Concise and easier to understand

Why so many changes?

Obsolesce We just plain don't need them anymore

Consistency Attribute names are uniform

C************ Concise and easier to understand

Why so many changes?

Obsolesce We just plain don't need them anymore

Consistency Attribute names are uniform

Comprehension Concise and easier to understand

Why so many changes?

Obsolesce We just plain don't need them anymore

Consistency Attribute names are uniform

Comprehension Concise and easier to understand

Intelligent Defaults

When an ********* value is omitted, the system automatically provides

a ********** value, eliminating the need to explicitly qualify

each and every aspect of a declaration. (Adapted from the CULE Knowledgebase)

Intelligent Defaults

When an essential value is omitted, the system automatically provides

a ********** value, eliminating the need to explicitly qualify

each and every aspect of a declaration. (Adapted from the CULE Knowledgebase)

Intelligent Defaults

When an essential value is omitted, the system automatically provides

a predefined value, eliminating the need to explicitly qualify

each and every aspect of a declaration. (Adapted from the CULE Knowledgebase)

Intelligent Defaults

When an essential value is omitted, the system automatically provides

a predefined value, eliminating the need to explicitly qualify

each and every aspect of a declaration. (Adapted from the CULE Knowledgebase)

Do Action classes change too?

Mostly simpler Can combine Struts 1 ActionForm, Add input properties directly to Action Or, use POJO

Do Action classes change too?

Mostly simpler Can combine Struts 1 ActionForm, Add input properties directly to Action Or, use POJO

Do Action classes change too?

Mostly simpler Can combine Struts 1 ActionForm, Add input properties directly to Action Or, use POJO

package forms; import org.apache.struts.action.ActionForm;

public class HelloForm extends ValidatorForm {

private String message;

public String getMessage() { return message; }

public void setMessage(String message) { this.message = message; }}

package actions;import javax.servlet.http.*;import org.apache.struts.action.*;

public class HelloAction extends Action {

public ActionForward execute( ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { HelloForm input = (HelloForm) form; input.setMessage(MESSAGE); return mapping.findForward(SUCCESS); }

public static final String MESSAGE = "Hello World!"; public static final String SUCCESS = "success";

}

package actions;import com.opensymphony.xwork2.ActionSupport;

public class Hello extends ActionSupport {

public String execute() throws Exception { setMessage(MESSAGE); return SUCCESS; }

public static final String MESSAGE = "Hello World!";

private String message;

public String getMessage() { return message; }

public void setMessage(String message) { this.message = message; }}

Lets code it!

Do Action classes change too?

To convert our Hello World Action class 1) Update or remove imports.2) Move input property to the Action

● Remove obsolete ActionForm3) Extend ActionSupport 4) Reference input property directly5) Remove SUCCESS static

Do Action classes change too?

To convert our Hello World Action class 1) Update or remove imports.2) Move input property to the Action

● Remove obsolete ActionForm3) Extend ActionSupport4) Reference input property directly5) Remove SUCCESS static

Do Action classes change too?

To convert our Hello World Action class 1) Update or remove imports.2) Move input property to the Action

● Remove obsolete ActionForm3) Extend ActionSupport4) Reference input property directly5) Remove SUCCESS static

Do Action classes change too?

To convert our Hello World Action class 1) Update or remove imports.2) Move input property to the Action

● Remove obsolete ActionForm3) Extend ActionSupport4) Reference input property directly5) Remove SUCCESS static

Do Action classes change too?

To convert our Hello World Action class 1) Update or remove imports.2) Move input property to the Action

● Remove obsolete ActionForm3) Extend ActionSupport4) Reference input property directly5) Remove SUCCESS static

Do Action classes change too?

To convert our Hello World Action class 1) Update or remove imports.2) Move input property to the Action

● Remove obsolete ActionForm3) Extend ActionSupport4) Reference input property directly5) Remove SUCCESS static

Struts University Series

Do Action classes change too?

Can combine Struts 1 ********** Move ***** property to the ActionOr, use Plain Old Java ******Extend ******Support

Do Action classes change too?

Can combine Struts 1 ActionForm Move ***** property to the ActionOr, use Plain Old Java ******Extend ******Support

Do Action classes change too?

Can combine Struts 1 ActionForm Move input property to the ActionOr, use Plain Old Java ******Extend ******Support

Do Action classes change too?

Can combine Struts 1 ActionForm Move input property to the ActionOr, use Plain Old Java ObjectExtend ******Support

Do Action classes change too?

Can combine Struts 1 ActionForm Move input property to the ActionOr, use Plain Old Java ObjectExtend ActionSupport

Do Action classes change too?

Can combine Struts 1 ActionForm, Move input property to the ActionOr, use Plain Old Java ObjectExtend ActionSupport

What about the tags?

<%@ taglib prefix="bean" uri="http://struts.apache.org/tags-bean" %><html><head> <title>Hello World!</title></head><body> <h2><bean:write name="HelloForm" property="message"/></h2></body></html>

<%@ taglib prefix="s" uri="/struts-tags" %><html><head> <title>Hello World!</title></head><body> <h2><s:property value="message"/></h2></body></html>

<%@ taglib prefix="bean" uri="http://struts.apache.org/tags-bean" %><html><head> <title>Hello World!</title></head><body> <h2><bean:write name="HelloForm" property="message"/></h2></body></html>

<%@ taglib prefix="s" uri="/struts-tags" %><html><head> <title>Hello World!</title></head><body> <h2><s:property value="message"/></h2></body></html>

Lets code it!

What about the tags?

Replace the <%@ taglib @%> directiveChange <bean:write /> to <s:property />

What about the tags?

Replace the <%@ taglib @%> directiveChange <bean:write /> to <s:property />

What about the tags?

Replace the <%@ taglib @%> directiveChange <bean:write /> to <s:property />

What about the tags?

New tags streamline page <bean:write> needs ActionForm name <s:property> finds property automatically

What about the tags?

New tags streamline page <bean:write> needs ActionForm name <s:property> finds property automatically

What about the tags?

New tags streamline page <bean:write> needs ActionForm name <s:property> finds property automatically

Struts University Series

What about the tags?

Change <bean:write /> to <s:******** />New tags streamline page <bean:write> needs ActionForm name <s:********> finds ******** automatically

What about the tags?

Change <bean:write /> to <s:property />New tags streamline page <bean:write> needs ActionForm name <s:********> finds ******** automatically

What about the tags?

Change <bean:write /> to <s:property />New tags streamline page <bean:write> needs ActionForm name <s:property> finds property automatically

What about the tags?

Change <bean:write /> to <s:property />New tags streamline page <bean:write> needs ActionForm name <s:property> finds property automatically

Can we still localize messages?

Struts 2 uses standard message bundleExisting bundle set in struts.properties

struts.custom.i18n.resources = resources

resources.propertiesresources_es.propertiesresources_fr_CA.properties

Can we still localize messages?

Struts 2 uses standard message bundleExisting bundle set in struts.properties

struts.custom.i18n.resources = resources

resources.propertiesresources_es.propertiesresources_fr_CA.properties

Can we still localize messages?

Struts 2 uses standard message bundleExisting bundle set in struts.properties

struts.custom.i18n.resources = resources

resources.propertiesresources_es.propertiesresources_fr_CA.properties

resources.properties message = Hello World

resources_es.properties message = ¡Hola Mundo!

Can we still localize messages?

Lookup message by key in ActionSet localized message to property

Can we still localize messages?

Lookup message by key in ActionSet localized message to property

public class HelloAction extends Action {

public ActionForward execute( ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {

HelloForm input = (HelloForm) form;- input.setMessage(MESSAGE);+ input.setMessage(getResources(+ request).getMessage(MESSAGE)); return mapping.findForward(SUCCESS); }

- public static final String MESSAGE = "Hello World!";+ public static final String MESSAGE = "message"; public static final String SUCCESS = "success";}

public class Hello extends ActionSupport {

public String execute() throws Exception {- setMessage(MESSAGE);+ setMessage(getText(MESSAGE));

return SUCCESS; }

- public static final String MESSAGE = "Hello World!";+ public static final String MESSAGE = "message";}

Can we still localize messages?

Tags can output localized messages, based on a keyLet's change page title to look up message

Can we still localize messages?

Tags can output localized messages, based on a keyLet's change page title to look up message

<%@ taglib prefix="bean" uri="http://struts.apache.org/tags-bean" %> <html><head>- <title>Hello World!</title>+ <title><bean:message key="message"/></title> </head> <body> <h2><bean:write name="HelloForm" property="message"/></h2> </body></html>

<%@ taglib prefix="s" uri="/struts-tags" %> <html><head>- <title>Hello World!</title>+ <title><s:text name="message"/></title> </head> <body> <h2><s:property value="message"/></h2> </body></html>

Lets code it!

Can we still localize messages?

Lookup message by key in ActionSet localized message to propertyTags can output localized messages, based on a key

Can we still localize messages?

Lookup message by key in ActionSet localized message to propertyTags can output localized messages, based on a key

Can we still localize messages?

Lookup message by key in ActionSet localized message to propertyTags can output localized messages, based on a key

Struts University Series

Can we still localize messages?

(A)setMessage(getText(MESSAGE));(B)input.setMessage(

getResources(request).getMessage(MESSAGE));

The Struts 1 statement is shown as (A) / (B). The Struts 2 statement is shown as (A) / (B).

Can we still localize messages?

(A)setMessage(getText(MESSAGE));(B)input.setMessage(

getResources(request).getMessage(MESSAGE));

The Struts 1 statement is shown as (A) / (B). The Struts 2 statement is shown as (A) / (B).

Can we still localize messages?

struts.custom.i18n.********* = resources

Can we still localize messages?

struts.custom.i18n.resources = resources

How do we change Locales?

Locale is stored in user's session Struts 1 requires a special Action

How do we change Locales?

Locale is stored in user's session Struts 1 requires a special Action

How do we change Locales?

Locale is stored in user's session Struts 1 requires a special Action

public final class LocaleAction extends Action {

private static final String LANGUAGE = "language"; private static final String COUNTRY = "country"; private static final String SUCCESS = "success";

public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { String language = request.getParameter(LANGUAGE); String country = request.getParameter(COUNTRY); Locale locale = getLocale(request); if ((language != null && language.length() > 0) && (country != null && country.length() > 0)) { locale = new java.util.Locale(language, country); } else if (language != null && language.length() > 0) { locale = new java.util.Locale(language, ""); } setLocale(request, locale); return mapping.findForward(SUCCESS); }}

How do we change Locales?

Link to change Struts 1 locale Calls Action to change Locale

<li><html:link

action="/Locale?language=es">Español</html:link></li>

How do we change Locales?

Link to change Struts 2 locale No Action needed

<li> <s:url id="es" action="Hello"> <s:param name="request_locale">es</s:param> </s:url> <s:a href="%{es}">Español</s:a></li>

Lets code it!

How do we change Locales?

Pass "request_locale=es" as parameter1) "es" can be any standard locale code.2) Framework updates locale for session.

How do we change Locales?

Pass "request_locale=es" as parameter1) "es" can be any standard locale code.2) Framework updates locale for session.

How do we change Locales?

Pass "request_locale=es" as parameter1) "es" can be any standard locale code.2) Framework updates locale for session.

Struts University Series

How do we change Locales?(A) <li><html:link action="/Locale?language=es">Español </html:link></li>(B) <li><s:url id="es" action="Hello"> <s:param name="request_locale">es</s:param> </s:url><s:a href="%{es}">Español</s:a> </li>

The Struts 1 statement is shown as (A) / (B). The Struts 2 statement is shown as (A) / (B).

How do we change Locales?(A) <li><html:link action="/Locale?language=es">Español </html:link></li>(B) <li><s:url id="es" action="Hello"> <s:param name="request_locale">es</s:param> </s:url><s:a href="%{es}">Español</s:a> </li>

The Struts 1 statement is shown as (A) / (B). The Struts 2 statement is shown as (A) / (B).

Does Struts 2 use Commons Validator?

Struts 2 uses XWork ValidatorSimilar but differentLet's compare configurations

Does Struts 2 use Commons Validator?

Struts 2 uses XWork ValidatorSimilar but differentLet's compare configurations

Does Struts 2 use Commons Validator?

Struts 2 uses XWork ValidatorSimilar but differentLet's compare configurations

Does Struts 2 use Commons Validator?

Struts 2 uses XWork ValidatorSimilar but differentLet's compare configurations

<?xml version="1.0" encoding="ISO-8859-1" ?><!DOCTYPE form-validation PUBLIC "-//Apache Software Foundation//DTD Commons Validator Rules Configuration 1.3.0//EN" "http://jakarta.apache.org/commons/dtds/validator_1_3_0.dtd">

<form-validation> <formset> <form name="HelloForm"> <field property="message" depends="required"> <arg key="message"/> </field> </form> </formset></form-validation>

<?xml version="1.0" encoding="ISO-8859-1" ?><!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="message"> <field-validator type="requiredstring"> <message key="requiredstring"/> </field-validator> </field></validators>

Does Struts 2 use Commons Validator?

Struts 1 We validate data on input Add input form to set message

Rather than Action

Does Struts 2 use Commons Validator?

Struts 1 We validate data on input Add input form to set message

Rather than Action

Does Struts 2 use Commons Validator?

Struts 1 We validate data on input Add input form to set message

Rather than Action

Does Struts 2 use Commons Validator?

Struts 1 We validate data on input Add input form to set message

Rather than Action

<!-- ... --> <action-mappings>

<action path="/HelloInput" name="HelloForm" forward="/HelloInput.jsp" validate="false" />

<action path="/Hello" name="HelloForm" forward="/HelloPage.jsp" validate="true" input="/HelloInput.do" />

</action-mappings></struts-config>

<html:errors/><html:form action="/Hello" focus="message" onsubmit="return validateRegistrationForm(this);"><table> <tr><td> Message: </td><td> <html:text property="message" /> </td></tr></table></html:form>

Does Struts 2 use Commons Validator?

Struts 2 Can reuse the same mapping Input represented by an alternate method Methods can be called via wildcards

Does Struts 2 use Commons Validator?

Struts 2 Can reuse the same mapping Input represented by an alternate method Methods can be called via wildcards

Does Struts 2 use Commons Validator?

Struts 2 Can reuse the same mapping Input represented by an alternate method Methods can be called via wildcards

Does Struts 2 use Commons Validator?

Struts 2 Can reuse the same mapping Input represented by an alternate method Methods can be called via wildcards

<action name="Hello!*" method="{1}" class="actions.Hello"> <result>/Hello.jsp</result> <result name="input">/Hello!input.jsp</result></action>

<s:actionerrors/><s:form action="Hello"> <s:textfield label="Message" name="message"/></s:form>

<action name="Hello!*" method="{1}" class="actions.Hello"> <result>/Hello.jsp</result> <result name="input">/Hello!input.jsp</result></action>

<s:actionerrors/><s:form action="Hello"> <s:textfield label="Message" name="message"/></s:form>

Does Struts 2 use Commons Validator?

Struts 1 has wildcards tooStill need to set validate=false on inputS2 input method returns “input” symbolS2 validation skipped on input methodInput form posts back to execute (or save, or store, or submit)

Does Struts 2 use Commons Validator?

Struts 1 has wildcards tooStill need to set validate=false on inputS2 input method returns “input” symbolS2 validation skipped on input methodInput form posts back to execute (or save, or store, or submit)

Does Struts 2 use Commons Validator?

Struts 1 has wildcards tooStill need to set validate=false on inputS2 input method returns “input” symbolS2 validation skipped on input methodInput form posts back to execute (or save, or store, or submit)

Does Struts 2 use Commons Validator?

Struts 1 has wildcards tooStill need to set validate=false on inputS2 input method returns “input” symbolS2 validation skipped on input methodInput form posts back to execute (or save, or store, or submit)

Does Struts 2 use Commons Validator?

Struts 1 has wildcards tooStill need to set validate=false on inputS2 input method returns “input” symbolS2 validation skipped on input methodInput form posts back to execute (or save, or store, or submit)

Does Struts 2 use Commons Validator?

Struts 1 has wildcards tooStill need to set validate=false on inputS2 input method returns “input” symbolS2 validation skipped on input methodInput form posts back to execute (or save, or store, or submit)

resources.properties prompt = Enter Message message = Hello World! # Struts 1 errors.required={0} is required. # Struts 2 requiredstring = ${getText(fieldName)} is required. resources_es.properties prompt = Entre el mensaje message = ¡Hola Mundo! # Struts 1 errors.required={0} se requiere. # Struts 2 requiredstring = ${getText(fieldName)} se requiere.

Lets code it!

Does Struts 2 use Commons Validator?

Struts 1: numeric parameter errors.required= {0} is required

Struts 2: expression language requiredstring= ${getText(fieldName)} is required.

Does Struts 2 use Commons Validator?

Struts 1: numeric parameter errors.required= {0} is required

Struts 2: expression language requiredstring= ${getText(fieldName)} is required.

Struts University Series

Does Struts 2 use Commons Validator?

Struts 2 uses X**** ValidatorS2 validation skipped on ***** method

Does Struts 2 use Commons Validator?

Struts 2 uses XWork ValidatorS2 validation skipped on ***** method

Does Struts 2 use Commons Validator?

Struts 2 uses XWork ValidatorS2 validation skipped on input method

Does Struts 2 use Commons Validator?

Struts 2 uses XWork ValidatorS2 validation skipped on input method

Does Struts 2 use Commons Validator?

(A) errors.required = {0} is required(B) requiredstring = ${getText(fieldName)} is required.

The Struts 1 statement is shown as (A) / (B). The Struts 2 statement is shown as (A) / (B).

Does Struts 2 use Commons Validator?

(A) errors.required = {0} is required(B) requiredstring = ${getText(fieldName)} is required.

The Struts 1 statement is shown as (A) / (B). The Struts 2 statement is shown as (A) / (B).

Is that all there is?

Cover the basics mappings, Actions, tags, localization, and

validation. Converted Hello World to Struts 2 Moved one at a time Actions, pages, and

configuration elements Shared essential resources, like message

bundles, between the versions.

Is that all there is?

Cover the basics mappings, Actions, tags, localization, and

validation. Converted Hello World to Struts 2 Moved one at a time Actions, pages, and

configuration elements Shared essential resources, like message

bundles, between the versions.

Is that all there is?

Cover the basics mappings, Actions, tags, localization, and

validation. Converted Hello World to Struts 2 Moved one at a time Actions, pages, and

configuration elements Shared essential resources, like message

bundles, between the versions.

Is that all there is?

Struts 2 is a giant leap forward, but, for Struts 1 developers, it's a learning curve we can walk, one step at a time.

Struts University Series

Struts University Series

Struts University Series

Recommended