Migrating to Struts 2 A tutorial for Struts 1 Developers
Struts University Series
Migrating to Struts 2 A tutorial for Struts 1 Developers
Struts University Series
Ted? Who's Ted?
Apache Software Foundation Member Apache Struts & Apache iBATIS PMCs JUnit in Action, Struts in Action, and Professional JSP Site Design Consult with teams throughout the United States Still working with the OK DEQ
Ted? Who's Ted?
Apache Software Foundation Member Apache Struts & Apache iBATIS PMCs JUnit in Action, Struts in Action, and Professional JSP Site Design Consult with teams throughout the United States Still working with the OK DEQ
Ted? Who's Ted?
Apache Software Foundation Member Apache Struts & Apache iBATIS PMCs JUnit in Action, Struts in Action, and Professional JSP Site Design Consult with teams throughout the United States Still working with the OK DEQ
Ted? Who's Ted?
Apache Software Foundation Member Apache Struts & Apache iBATIS PMCs JUnit in Action, Struts in Action, and Professional JSP Site Design Consult with teams throughout the United States Still working with the OK DEQ
Ted? Who's Ted?
Apache Software Foundation Member Apache Struts & Apache iBATIS PMCs JUnit in Action, Struts in Action, and Professional JSP Site Design. Consult with teams throughout the United States Still working with the OK DEQ
Ted? Who's Ted?
Apache Software Foundation Member Apache Struts & Apache iBATIS PMCs JUnit in Action, Struts in Action, and Professional JSP Site Design. Consult with teams throughout the United States Still working with the OK DEQ
Migrating to Struts 2
Help Struts 1 developers become knowledgeable Struts 2 developers as quickly as possible Migrate Struts 1 application to Struts 2 Intended for experienced Java web developers
Migrating to Struts 2
Help Struts 1 developers become knowledgeable Struts 2 developers as quickly as possible Migrate Struts 1 application to Struts 2 Intended for experienced Java web developers
Migrating to Struts 2
Help Struts 1 developers become knowledgeable Struts 2 developers as quickly as possible Migrate Struts 1 application to Struts 2 Intended for experienced Java web developers
Migrating to Struts 2
Help Struts 1 developers become knowledgeable Struts 2 developers as quickly as possible Migrate Struts 1 application to Struts 2 Intended 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 Proven solution to common problems Elegant solution to difficult problems
Struts 1
Struts 2
What is Struts 2?
Free open-source framework
Two major versions Proven solution to common problems Elegant solution to difficult problems
Struts 1
Struts 2
What is Struts 2?
Free open-source framework
Two major versions Proven solution to common problems Elegant solution to difficult problems
Struts 1
Struts 2
What is Struts 2?
Free open-source framework
Two major versions Proven solution to common problems Elegant solution to difficult problems
Struts 1
Struts 2
How are Struts 1 and Struts 2 alike?
A "request" handler
maps Java classes to web application URIs maps logical names to web resources creates rich, form-based applications
A "response" handler
A tag library
How are Struts 1 and Struts 2 alike?
A "request" handler
maps Java classes to web application URIs maps logical names to web resources creates rich, form-based applications
A "response" handler
A tag library
How are Struts 1 and Struts 2 alike?
A "request" handler
maps Java classes to web application URIs maps logical names to web resources creates rich, form-based applications
A "response" handler
A tag library
How are Struts 1 and Struts 2 alike?
A "request" handler
maps Java classes to web application URIs maps logical names to web resources creates rich, form-based applications
A "response" handler
A tag library
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 community Thousands of applications in production New 1.3.5 release But, is it time to try Struts 2?
Is Struts 1 obsolete?
Robust and vibrant community Thousands of applications in production New 1.3.5 release But, is it time to try Struts 2?
Is Struts 1 obsolete?
Robust and vibrant community Thousands of applications in production New 1.3.5 release But, is it time to try Struts 2?
Is Struts 1 obsolete?
Robust and vibrant community Thousands of applications in production New 1.3.5 release But, is it time to try Struts 2?
Is Struts 1 obsolete?
Robust and vibrant community Thousands of applications in production New 1.3.5 release But, is it time to try Struts 2?
Is it difficult to migrate?
Not difficult but non-trivial Migrating Actions takes time and effort Stable: may not be worth the effort Growth: good investment
Is it difficult to migrate?
Not difficult but non-trivial Migrating Actions takes time and effort Stable: may not be worth the effort Growth: good investment
Is it difficult to migrate?
Not difficult but non-trivial Migrating Actions takes time and effort Stable: may not be worth the effort Growth: good investment
Is it difficult to migrate?
Not difficult but non-trivial Migrating Actions takes time and effort Stable: may not be worth the effort Growth: good investment
Is it difficult to migrate?
Not difficult but non-trivial Migrating Actions takes time and effort Stable: may not be worth the effort Growth: good investment
Is it difficult to migrate?
Not difficult but non-trivial Migrating Actions takes time and effort Stable: may not be worth the effort Growth: good investment Struts 2 is smarter, easier
Is it difficult to migrate?
Not difficult but non-trivial Migrating Actions takes time and effort Stable: may not be worth the effort Growth: good investment Struts 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 maps logical names to web resources creates rich, form-based applications
A "r*******" handler
A *** library
How are Struts 1 and Struts 2 alike?
A "request" handler
maps Java classes to web application URIs maps logical names to web resources creates rich, form-based applications
A "r*******" handler
A *** library
How are Struts 1 and Struts 2 alike?
A "request" handler
maps Java classes to web application URIs maps logical names to web resources creates rich, form-based applications
A "response" handler
A *** library
How are Struts 1 and Struts 2 alike?
A "request" handler
maps Java classes to web application URIs maps logical names to web resources creates rich, form-based applications
A "response" handler
A tag library
How are Struts 1 and Struts 2 alike?
A "request" handler
maps Java classes to web application URIs maps logical names to web resources creates rich, form-based applications
A "response" handler
A tag library
Where should a migration begin?
Add Struts 2 JARs Migrate application page at a time Use both versions at the same time Architectures can remain the same Remove Struts 1 red tape Swap in new tags
Where should a migration begin?
Add Struts 2 JARs Migrate application page at a time Use both versions at the same time Architectures can remain the same Remove Struts 1 red tape Swap in new tags
Where should a migration begin?
Add Struts 2 JARs Migrate application page at a time Use both versions at the same time Architectures can remain the same Remove Struts 1 red tape Swap in new tags
Where should a migration begin?
Add Struts 2 JARs Migrate application page at a time Use both versions at the same time Architectures can remain the same Remove Struts 1 red tape Swap in new tags
Where should a migration begin?
Add Struts 2 JARs Migrate application page at a time Use both versions at the same time Architectures can remain the same Remove Struts 1 red tape Swap in new tags
Where should a migration begin?
Add Struts 2 JARs Migrate application page at a time Use both versions at the same time Architectures can remain the same Remove Struts 1 red tape Swap in new tags
Where should a migration begin?
Add Struts 2 JARs Migrate application page at a time Use both versions at the same time Architectures can remain the same Remove Struts 1 red tape Swap in new tags
Where should a migration begin?
Migrate "Hello World" application Displays message in two languages Add input form for custom message Validate data entry
Where should a migration begin?
Migrate "Hello World" application Displays message in two languages Add input form for custom message Validate data entry
Where should a migration begin?
Migrate "Hello World" application Displays message in two languages Add input form for custom message Validate data entry
Where should a migration begin?
Migrate "Hello World" application Displays message in two languages Add input form for custom message Validate data entry
Where should a migration begin?
Update web.xml to add ...
1)The Struts 2 Filter 2)A filter mapping 3)The Spring Listener
Where should a migration begin?
Update web.xml to add ...
1)The Struts 2 Filter 2)A filter mapping 3)The Spring Listener
Where should a migration begin?
Update web.xml to add ...
1)The Struts 2 Filter 2)A filter mapping 3)The Spring Listener
Where should a migration begin?
Update web.xml to add ...
1)The Struts 2 Filter 2)A filter mapping 3)The Spring Listener
Where should a migration begin?
Update web.xml to add ...
1)The Struts 2 Filter 2)A filter mapping 3)The Spring Listener
struts2 org.apache.struts2.dispatcher.FilterDispatcher struts2 /* org.springframework.web.context.ContextLoaderListener
action org.apache.struts.action.ActionServlet config /WEB-INF/classes/struts-config.xml 2 action *.do
index.html
applicationContext.xml
Lets code it!
Where should a migration begin?
Struts 1 actions handle *.do Struts 2 actions handle *.action Migrate 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 *.action Migrate 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 *.action Migrate 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 *.action Migrate 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 streamlined Less to configure Element names are shorter Struts 1 is "struts-config.xml" Struts 2 is "struts.xml"
Is the Struts 2 configuration file different? Struts 2 configuration file is streamlined Less to configure Element names are shorter Struts 1 is "struts-config.xml" Struts 2 is "struts.xml"
Is the Struts 2 configuration file different? Struts 2 configuration file is streamlined Less to configure Element names are shorter Struts 1 is "struts-config.xml" Struts 2 is "struts.xml"
Is the Struts 2 configuration file different? Struts 2 configuration file is streamlined Less to configure Element names are shorter Struts 1 is "struts-config.xml" Struts 2 is "struts.xml"
Is the Struts 2 configuration file different? Struts 2 configuration file is streamlined Less to configure Element names are shorter Struts 1 is "struts-config.xml" Struts 2 is "struts.xml"
Is the Struts 2 configuration file different? Struts 2 configuration file is streamlined Less to configure Element names are shorter Struts 1 is "struts-config.xml" Struts 2 is "struts.xml"
/Hello.jsp
Lets code it!
To migrate configuration
Replace the DTD Change
to Add Remove the element Change to Update each element
To migrate configuration
Replace the DTD Change to Add Remove the element Change to Update each element
To migrate configuration
Replace the DTD Change to Add Remove the element Change to Update each element
To migrate configuration
Replace the DTD Change to Add Remove the element Change to Update each element
To migrate configuration
Replace the DTD Change to Add Remove the element Change to Update each element
To migrate configuration
Replace the DTD Change to Add Remove the element Change to Update each element
To migrate configuration
Replace the DTD Change to Add Remove the element Change to Update each element
To migrate configuration
To update each element, we
1)Remove from the "name" attribute 2)Change the "path" attribute to "name", and "type" to "class" 3)Change the element into a element.
To migrate configuration
To update each element, we
1)Remove from the "name" attribute 2)Change the "path" attribute to "name", and "type" to "class" 3)Change the element into a element.
To migrate configuration
To update each element, we
1)Remove from the "name" attribute 2)Change the "path" attribute to "name", and "type" to "class" 3)Change the element into a element.
To migrate configuration
To update each element, we
1)Remove from the "name" attribute 2)Change the "path" attribute to "name", and "type" to "class" 3)Change the element into a element.
Struts University Series
Is the Struts 2 configuration file different? Struts 2 configuration file is (less/more) to configure Struts 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 configure Struts 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 configure Struts 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 configure Struts 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 configure Struts 2 configuration file is named "struts.xml" Most Struts 2 configurations include the “struts-default.xml”
To migrate configuration
Replace the DTD ****** to Add ****** the element Change to ****** each element
To migrate configuration
Replace the DTD Change to Add ****** the element Change to ****** each element
To migrate configuration
Replace the DTD Change to Add Remove the element Change to ****** each element
To migrate configuration
Replace the DTD Change to Add Remove the element Change to Update each element
To migrate configuration
Replace the DTD Change to Add Remove the element Change to Update each element
Why so many changes?
Obsolesce
We just plain don't need them anymore Attribute names are applied consistently Concise and easier to understand
Consistency
Comprehension
Why so many changes?
Obsolesce
We just plain don't need them anymore Attribute names are applied consistently Concise and easier to understand
Consistency
Comprehension
Why so many changes?
Obsolesce
We just plain don't need them anymore Attribute names are applied consistently Concise and easier to understand
Consistency
Comprehension
Why so many changes?
Obsolesce
We just plain don't need them anymore Attribute names are applied consistently Concise and easier to understand
Consistency
Comprehension
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
/Hello.jsp
The name defaults to "success" The content defaults to "location" (e.g. path) The type defaults to "dispatch"
Intelligent Defaults
/Hello.jsp
The name defaults to "success" The content defaults to "location" (e.g. path) The type defaults to "dispatch"
Intelligent Defaults
/Hello.jsp
The name defaults to "success" The content defaults to "location" (e.g. path) The type defaults to "dispatch"
Intelligent Defaults
/Welcome.jsp
Changing the result name and type
Struts University Series
Why so many changes?
O********
We just plain don't need them anymore Attribute names are uniform Concise and easier to understand
C**********
C************
Why so many changes?
Obsolesce
We just plain don't need them anymore Attribute names are uniform Concise and easier to understand
C**********
C************
Why so many changes?
Obsolesce
We just plain don't need them anymore Attribute names are uniform Concise and easier to understand
Consistency
C************
Why so many changes?
Obsolesce
We just plain don't need them anymore Attribute names are uniform Concise and easier to understand
Consistency
Comprehension
Why so many changes?
Obsolesce
We just plain don't need them anymore Attribute names are uniform Concise and easier to understand
Consistency
Comprehension
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 ActionForm
3) Extend ActionSupport 4) Reference input property directly 5) 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 ActionForm
3) Extend ActionSupport 4) Reference input property directly 5) 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 ActionForm
3) Extend ActionSupport 4) Reference input property directly 5) 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 ActionForm
3) Extend ActionSupport 4) Reference input property directly 5) 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 ActionForm
3) Extend ActionSupport 4) Reference input property directly 5) 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 ActionForm
3) Extend ActionSupport 4) Reference input property directly 5) Remove SUCCESS static
Struts University Series
Do Action classes change too?
Can combine Struts 1 ********** Move ***** property to the Action Or, use Plain Old Java ****** Extend ******Support
Do Action classes change too?
Can combine Struts 1 ActionForm Move ***** property to the Action Or, use Plain Old Java ****** Extend ******Support
Do Action classes change too?
Can combine Struts 1 ActionForm Move input property to the Action Or, use Plain Old Java ****** Extend ******Support
Do Action classes change too?
Can combine Struts 1 ActionForm Move input property to the Action Or, use Plain Old Java Object Extend ******Support
Do Action classes change too?
Can combine Struts 1 ActionForm Move input property to the Action Or, use Plain Old Java Object Extend ActionSupport
Do Action classes change too?
Can combine Struts 1 ActionForm, Move input property to the Action Or, use Plain Old Java Object Extend ActionSupport
What about the tags?
<%@ taglib prefix="bean" uri="http://struts.apache.org/tags-bean" %> Hello World!
<%@ taglib prefix="s" uri="/struts-tags" %> Hello World!
<%@ taglib prefix="bean" uri="http://struts.apache.org/tags-bean" %> Hello World!
<%@ taglib prefix="s" uri="/struts-tags" %> Hello World!
Lets code it!
What about the tags?
Replace the <%@ taglib @%> directive Change to
What about the tags?
Replace the <%@ taglib @%> directive Change to
What about the tags?
Replace the <%@ taglib @%> directive Change to
What about the tags?
New tags streamline page
needs ActionForm name finds property automatically
What about the tags?
New tags streamline page
needs ActionForm name finds property automatically
What about the tags?
New tags streamline page
needs ActionForm name finds property automatically
Struts University Series
What about the tags?
Change to New tags streamline page
needs ActionForm name finds ******** automatically
What about the tags?
Change to New tags streamline page
needs ActionForm name finds ******** automatically
What about the tags?
Change to New tags streamline page
needs ActionForm name finds property automatically
What about the tags?
Change to New tags streamline page
needs ActionForm name finds property automatically
Can we still localize messages?
Struts 2 uses standard message bundle Existing bundle set in struts.properties
struts.custom.i18n.resources = resources
resources.properties resources_es.properties resources_fr_CA.properties
Can we still localize messages?
Struts 2 uses standard message bundle Existing bundle set in struts.properties
struts.custom.i18n.resources = resources
resources.properties resources_es.properties resources_fr_CA.properties
Can we still localize messages?
Struts 2 uses standard message bundle Existing bundle set in struts.properties
struts.custom.i18n.resources = resources
resources.properties resources_es.properties resources_fr_CA.properties
resources.properties
message = Hello World
resources_es.properties
message = ¡Hola Mundo!
Can we still localize messages?
Lookup message by key in Action Set localized message to property
Can we still localize messages?
Lookup message by key in Action Set 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 key Let's change page title to look up message
Can we still localize messages?
Tags can output localized messages, based on a key Let's change page title to look up message
<%@ taglib prefix="bean" uri="http://struts.apache.org/tags-bean" %> Hello World! +
<%@ taglib prefix="s" uri="/struts-tags" %> Hello World! +
Lets code it!
Can we still localize messages?
Lookup message by key in Action Set localized message to property Tags can output localized messages, based on a key
Can we still localize messages?
Lookup message by key in Action Set localized message to property Tags can output localized messages, based on a key
Can we still localize messages?
Lookup message by key in Action Set localized message to property Tags 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
Español
How do we change Locales?
Link to change Struts 2 locale
No Action needed
es Español
Lets code it!
How do we change Locales?
Pass "request_locale=es" as parameter
1) "es" can be any standard locale code. 2) Framework updates locale for session.
How do we change Locales?
Pass "request_locale=es" as parameter
1) "es" can be any standard locale code. 2) Framework updates locale for session.
How do we change Locales?
Pass "request_locale=es" as parameter
1) "es" can be any standard locale code. 2) Framework updates locale for session.
Struts University Series
How do we change Locales?
(A) Español (B) es Español
The Struts 1 statement is shown as (A) / (B). The Struts 2 statement is shown as (A) / (B).
How do we change Locales?
(A) Español (B) es Español
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 Validator Similar but different Let's compare configurations
Does Struts 2 use Commons Validator? Struts 2 uses XWork Validator Similar but different Let's compare configurations
Does Struts 2 use Commons Validator? Struts 2 uses XWork Validator Similar but different Let's compare configurations
Does Struts 2 use Commons Validator? Struts 2 uses XWork Validator Similar but different Let's compare configurations
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
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
/Hello.jsp /Hello!input.jsp
/Hello.jsp /Hello!input.jsp
Does Struts 2 use Commons Validator? Struts 1 has wildcards too Still need to set validate=false on input S2 input method returns “input” symbol S2 validation skipped on input method Input form posts back to execute
(or save, or store, or submit)
Does Struts 2 use Commons Validator? Struts 1 has wildcards too Still need to set validate=false on input S2 input method returns “input” symbol S2 validation skipped on input method Input form posts back to execute
(or save, or store, or submit)
Does Struts 2 use Commons Validator? Struts 1 has wildcards too Still need to set validate=false on input S2 input method returns “input” symbol S2 validation skipped on input method Input form posts back to execute
(or save, or store, or submit)
Does Struts 2 use Commons Validator? Struts 1 has wildcards too Still need to set validate=false on input S2 input method returns “input” symbol S2 validation skipped on input method Input form posts back to execute
(or save, or store, or submit)
Does Struts 2 use Commons Validator? Struts 1 has wildcards too Still need to set validate=false on input S2 input method returns “input” symbol S2 validation skipped on input method Input form posts back to execute
(or save, or store, or submit)
Does Struts 2 use Commons Validator? Struts 1 has wildcards too Still need to set validate=false on input S2 input method returns “input” symbol S2 validation skipped on input method Input 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 requiredstring= ${getText(fieldName)} is required.
Struts 2: expression language
Does Struts 2 use Commons Validator? Struts 1: numeric parameter
errors.required= {0} is required requiredstring= ${getText(fieldName)} is required.
Struts 2: expression language
Struts University Series
Does Struts 2 use Commons Validator? Struts 2 uses X**** Validator S2 validation skipped on ***** method
Does Struts 2 use Commons Validator? Struts 2 uses XWork Validator S2 validation skipped on ***** method
Does Struts 2 use Commons Validator? Struts 2 uses XWork Validator S2 validation skipped on input method
Does Struts 2 use Commons Validator? Struts 2 uses XWork Validator S2 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