Return-Path: X-Original-To: archive-asf-public-internal@cust-asf2.ponee.io Delivered-To: archive-asf-public-internal@cust-asf2.ponee.io Received: from cust-asf.ponee.io (cust-asf.ponee.io [163.172.22.183]) by cust-asf2.ponee.io (Postfix) with ESMTP id 10CFA200D3A for ; Wed, 15 Nov 2017 08:49:26 +0100 (CET) Received: by cust-asf.ponee.io (Postfix) id 0EE76160BF4; Wed, 15 Nov 2017 07:49:26 +0000 (UTC) Delivered-To: archive-asf-public@cust-asf.ponee.io Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by cust-asf.ponee.io (Postfix) with SMTP id 25093160BEA for ; Wed, 15 Nov 2017 08:49:21 +0100 (CET) Received: (qmail 72574 invoked by uid 500); 15 Nov 2017 07:49:21 -0000 Mailing-List: contact commits-help@struts.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@struts.apache.org Delivered-To: mailing list commits@struts.apache.org Received: (qmail 72565 invoked by uid 99); 15 Nov 2017 07:49:21 -0000 Received: from ec2-52-202-80-70.compute-1.amazonaws.com (HELO gitbox.apache.org) (52.202.80.70) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 15 Nov 2017 07:49:21 +0000 Received: by gitbox.apache.org (ASF Mail Server at gitbox.apache.org, from userid 33) id 128E081B3E; Wed, 15 Nov 2017 07:49:20 +0000 (UTC) Date: Wed, 15 Nov 2017 07:49:20 +0000 To: "commits@struts.apache.org" Subject: [struts-site] branch asf-site updated: Updates production by Jenkins MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit Message-ID: <151073215999.28579.16443986315957904028@gitbox.apache.org> From: git-site-role@apache.org X-Git-Host: gitbox.apache.org X-Git-Repo: struts-site X-Git-Refname: refs/heads/asf-site X-Git-Reftype: branch X-Git-Oldrev: 4a8325a7a195baf5fff72b7b2f0c64404ff715c2 X-Git-Newrev: 4f66d957020aaa20ea967e3616fc0d278af9876b X-Git-Rev: 4f66d957020aaa20ea967e3616fc0d278af9876b X-Git-NotificationType: ref_changed_plus_diff X-Git-Multimail-Version: 1.5.dev Auto-Submitted: auto-generated archived-at: Wed, 15 Nov 2017 07:49:26 -0000 This is an automated email from the ASF dual-hosted git repository. git-site-role pushed a commit to branch asf-site in repository https://gitbox.apache.org/repos/asf/struts-site.git The following commit(s) were added to refs/heads/asf-site by this push: new 4f66d95 Updates production by Jenkins 4f66d95 is described below commit 4f66d957020aaa20ea967e3616fc0d278af9876b Author: jenkins AuthorDate: Wed Nov 15 07:49:18 2017 +0000 Updates production by Jenkins --- content/getting-started/annotations.html | 103 ++++++++---- content/getting-started/coding-actions.html | 19 ++- content/getting-started/control-tags.html | 23 ++- content/getting-started/debugging-struts.html | 83 +++++++--- content/getting-started/exception-handling.html | 29 ++-- content/getting-started/exclude-parameters.html | 83 +++++++--- content/getting-started/form-tags.html | 39 +++-- .../getting-started/form-validation-using-xml.html | 39 +++-- content/getting-started/form-validation.html | 91 +++++++---- .../getting-started/hello-world-using-struts2.html | 146 +++++++++++------ .../how-to-create-a-struts2-web-application.html | 58 ++++--- content/getting-started/http-session.html | 6 +- .../getting-started/introducing-interceptors.html | 127 +++++++++------ .../getting-started/jasper-reports-tutorial.html | 13 +- .../getting-started/message-resource-files.html | 176 ++++++++++++++------- content/getting-started/preperable-interface.html | 65 ++++++-- content/getting-started/processing-forms.html | 20 ++- content/getting-started/spring.html | 122 ++++++++------ content/getting-started/themes.html | 153 +++++++++++------- content/getting-started/unit-testing.html | 102 ++++++++---- content/getting-started/using-tags.html | 110 +++++++++---- .../getting-started/wildcard-method-selection.html | 26 ++- 22 files changed, 1124 insertions(+), 509 deletions(-) diff --git a/content/getting-started/annotations.html b/content/getting-started/annotations.html index 84faa57..57b6d0b 100644 --- a/content/getting-started/annotations.html +++ b/content/getting-started/annotations.html @@ -119,23 +119,41 @@
- << back to Getting Started Edit on GitHub -

Annotations

+ + << back to Getting started + +

Annotations

-

The example code for this tutorial, annotations, is available for checkout at https://github.com/apache/struts-examples

+ -

Introduction

+

The example code for this tutorial, annotations, is available for checkout at struts-examples

-

In our previous tutorials we’ve been using an XML file (struts.xml) to configure our applications. The XML file wires up the action names (register), with ActionSupport classes (RegisterAction.java), and with the result to render back to the browser (register.jsp). Struts 2 provides an alternative to using XML to configure your application by using standard naming conve [...] +

Introduction

-

This tutorial assumes you understand how to apply annotations to Java classes and methods. If you’re not familiar with annotations, consult the Java online tutorial.

+

In our previous tutorials we’ve been using an XML file (struts.xml) to configure our applications. The XML file wires +up the action names (register), with ActionSupport classes (RegisterAction.java), and with the result to render back +to the browser (register.jsp). Struts 2 provides an alternative to using XML to configure your application by using +standard naming conventions and annotations for your action names, ActionSupport classes, and results.

-

The Struts 2 user mailing list is an excellent place to get help. If you are having a problem getting the tutorial example applications to work search the Struts 2 mailing list. If you don’t find an answer to your problem, post a question on the mailing list.

+

This tutorial assumes you understand how to apply annotations to Java classes and methods. If you’re not familiar with +annotations, consult the Java online tutorial.

-

Struts 2 Convention Plugin

+

The Struts 2 user mailing list is an excellent place to get help. If you are having +a problem getting the tutorial example applications to work search the Struts 2 mailing list. If you don’t find an answer +to your problem, post a question on the mailing list.

-

Struts 2 enables the use of standard naming conventions and annotations when you include the Convention plugin in your application’s class path. If you’re using Maven you’ll need to add a dependency:

+

Struts 2 Convention Plugin

+ +

Struts 2 enables the use of standard naming conventions and annotations when you include the Convention plugin in your +application’s class path. If you’re using Maven you’ll need to add a dependency:

Convention Plugin Dependency

@@ -146,27 +164,50 @@ </dependency> -

If you’re using Ant then copy the struts2-convention-plugin jar file from the Struts 2 download to your WEB-INF/lib folder.

-

The convention plugin provide several different ways you can configure your Struts 2 application without using XML. Consult the Convention Plugin documentation for complete details. This tutorial only examines one simple way of following the conventions provided by the Convention plugin.

+

If you’re using Ant then copy the struts2-convention-plugin jar file from the Struts 2 download to your WEB-INF/lib folder.

+ +

The convention plugin provide several different ways you can configure your Struts 2 application without using XML. +Consult the Convention Plugin documentation for complete details. This tutorial only examines +one simple way of following the conventions provided by the Convention plugin.

-

When you run the example application you’ll see on the index.jsp page a link to Get your hello. This URL for the link is hello.action. When you click on this link, the execute method of class HelloAction.java (which is a Struts 2 ActionSupport class) is run. The view page rendered back to the browser after the execute method returns success is index.jsp page a link to Get your hello. This URL for the link +is hello.action. When you click on this link, the execute method of class HelloAction.java (which is a Struts 2 ActionSupport class) +is run. The view page rendered back to the browser after the execute method returns success is hello-success.jsp.

-

None of the above is wired up using XML but rather happens because the application follows the standard naming conventions expected by the Convention plugin. The first convention is that the ActionSupport class, HelloAction.java, is in package org.apache.struts.struts2annotations.action. One of the Convention plugin’s defaults is to look for ActionSupport classes that are in package structure that ends in action. The next convention the applicati [...] +

None of the above is wired up using XML but rather happens because the application follows the standard naming conventions +expected by the Convention plugin. The first convention is that the ActionSupport class, HelloAction.java, is in package +org.apache.struts.struts2annotations.action. One of the Convention plugin’s defaults is to look for ActionSupport +classes that are in package structure that ends in action. The next convention the application follows is that HelloAction.java +extends the ActionSupport class and defines an execute method. The link is hello.action. When the Struts 2 filter sees +a request for hello.action it will map that request to the HelloAction class’s execute method due to the Convention +plugin being used.

-

So a link of hello.action causes the execute method of class HelloAction to be run. That method returns “success.” Because the application is using the Convention plugin, Struts 2 will render back to the browser a view page named hello-success.jsp that is located in WEB-INF/content (by default the Convention plugin expects all view pages to be in this location). If the execute method returns “input” or “error” then the view page rendered would ha [...] +

So a link of hello.action causes the execute method of class HelloAction to be run. That method returns success. +Because the application is using the Convention plugin, Struts 2 will render back to the browser a view page named +hello-success.jsp that is located in WEB-INF/content (by default the Convention plugin expects all view pages to be +in this location). If the execute method returns “input” or “error” then the view page rendered would have been +hello-input.jsp or hello-error.jsp.

-

Struts 2 Configuration Plugin

+

Struts 2 Configuration Plugin

-

In a previous tutorial we reviewed how to use the Struts 2 Configuration plugin to view the details of how Struts 2 has configured your application. When using the Convention plugin, it’s very handy to also use the Configuration plugin during development. On the example application’s home page is a link to the application’s configuration. Click on that link and then the hello link on the left menu (under Actions in default). You’ll see the configura [...] +

In a previous tutorial we reviewed how to use the Struts 2 Configuration plugin to view the details +of how Struts 2 has configured your application. When using the Convention plugin, it’s very handy to also use +the Configuration plugin during development. On the example application’s home page is a link to the application’s configuration. +Click on that link and then the hello link on the left menu (under Actions in default). You’ll see the configuration +for the hello action including it’s Action class, result, and view page.

Screen shot 2010-10-24 at 10.51.45 AM.png

-

Annotations

+

Annotations

-

If you want to go beyond the simple naming conventions provided by the Convention plugin, you can use the Struts 2 annotations also provided by the plugin. For example, a common work-flow for a Struts 2 application is to first execute the ActionSupport class’s input method to setup form field default values and then to run the execute method of the same ActionSupport class when the form is submitted (to validate and save the user’s input).

+

If you want to go beyond the simple naming conventions provided by the Convention plugin, you can use the Struts 2 +annotations also provided by the plugin. For example, a common work-flow for a Struts 2 application is to first execute +the ActionSupport class’s input method to setup form field default values and then to run the execute method of the same +ActionSupport class when the form is submitted (to validate and save the user’s input).

-

The link to Register for the drawing on the example application’s home page follows this work flow. The link value is register-input.action. If you examine the RegisterAction.java class you’ll find the input method with an Action annotation.

+

The link to Register for the drawing on the example application’s home page follows this work flow. The link value +is register-input.action. If you examine the RegisterAction.java class you’ll find the input method with an Action annotation.

Action Annotation

@@ -179,13 +220,18 @@ -

The Action annotation tells Struts 2 to execute the annotated method when the action link value equals the Action annotation’s value (“register-input”). So a link of register-input.action will call the input method of class RegisterAction. On the example application’s home page is a link to Register for the drawing with a URL of register-input.action.

+

The Action annotation tells Struts 2 to execute the annotated method when the action link value equals the Action +annotation’s value (register-input). So a link of register-input.action will call the input method of class RegisterAction. +On the example application’s home page is a link to Register for the drawing with a URL of register-input.action.

-

The input method above returns “input”. By the standards of the Convention plugin, the view page rendered will be register-input.jsp (from WEB-INF/content). On that view page is a Struts 2 form tag with an action attribute value of register. When submitting the form, the execute method of class RegisterAction will be run. Since the execute method returns success, the view page rendered is register-success.jsp.

+

The input method above returns “input”. By the standards of the Convention plugin, the view page rendered will be +register-input.jsp (from WEB-INF/content). On that view page is a Struts 2 form tag with an action attribute value +of register. When submitting the form, the execute method of class RegisterAction will be run. Since the execute method +returns success, the view page rendered is register-success.jsp.

-

Struts 2 Configuration Values

+

Struts 2 Configuration Values

-

In previous examples, we included in struts.xml values for some of the Struts 2 configuration parameters.

+

In previous examples, we included in struts.xml values for some of the Struts 2 configuration parameters.

struts.xml parameter configuration

@@ -193,13 +239,13 @@ -

When we don’t use a struts.xml file, we can set the value of these Struts 2 parameters by using filter parameters in web.xml:

+

When we don’t use a struts.xml file, we can set the value of these Struts 2 parameters by using filter parameters in web.xml:

Struts 2 Parameter Configuration web.xml

<filter>
     <filter-name>struts2</filter-name>
-    <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
+    <filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
     <init-param>
         <param-name>struts.devMode</param-name>
         <param-value>true</param-value>
@@ -208,9 +254,12 @@
 
-

Summary

+

Summary

-

We’ve just scratched the surface of what the Struts 2 convention plugin provides to reduce or eliminate the need to use an XML file to configure your Struts 2 application. The Struts 2 Convention plugin provides ways to map multiple actions to the same method, map results to different view pages, map errors to view pages, and much more. Be sure to read through the Convention Plugin documentation for alternative ways to conf [...] +

We’ve just scratched the surface of what the Struts 2 convention plugin provides to reduce or eliminate the need to use +an XML file to configure your Struts 2 application. The Struts 2 Convention plugin provides ways to map multiple actions +to the same method, map results to different view pages, map errors to view pages, and much more. Be sure to read through +the Convention Plugin documentation for alternative ways to configure your Struts 2 application.

diff --git a/content/getting-started/coding-actions.html b/content/getting-started/coding-actions.html index 176be82..2dd4949 100644 --- a/content/getting-started/coding-actions.html +++ b/content/getting-started/coding-actions.html @@ -119,11 +119,22 @@
- << back to Getting Started Edit on GitHub -

Coding actions

- -

This tutorial assumes you’ve completed the Using Struts 2 Tags tutorial and have a working using-tags project. The example code for this tutorial, coding-actions, is available for checkout from the Struts 2 GitHub repository: https://github.com/apache/struts-examples.

+ + << back to Getting started + +

Coding actions

+ + + +

This tutorial assumes you’ve completed the Using Struts 2 Tags tutorial and have a working using-tags +project. The example code for this tutorial, coding-actions, is available for checkout from the Struts 2 GitHub repository +struts-examples.

Introduction

diff --git a/content/getting-started/control-tags.html b/content/getting-started/control-tags.html index 5dfd579..4a21d8f 100644 --- a/content/getting-started/control-tags.html +++ b/content/getting-started/control-tags.html @@ -119,19 +119,28 @@
- << back to Getting Started Edit on GitHub -

Control Tags

+ + << back to Getting started + +

Control Tags

-

The example code for this tutorial, control_tags, is available at https://github.com/apache/struts-examples

+ -

Introduction

+

The example code for this tutorial, control-tags, is available at struts-examples

+ +

Introduction

Struts 2 has several control tags that can be used in the view. This tutorial will discuss and show examples of how to use the Struts 2 if and iterator tags. For more information about these and other control tags visit tags reference.

The Struts 2 user mailing list is an excellent place to get help. If you are having a problem getting the tutorial example applications to work search the Struts 2 mailing list. If you don’t find an answer to your problem, post a question on the mailing list.

-

Struts 2 if Tag

+

Struts 2 if Tag

In the example application’s thankyou.jsp is this markup.

@@ -163,7 +172,7 @@

The purpose of the above markup is to use either “Car model” or “Car models” depending on how many car models the user selected on the edit page. So the value for the test attribute of this iterator tag gets the length of the carModels String array and compares that to 1. If it’s greater then 1, the correct grammar is “Car models” else the correct grammar is “Car model”.

-

Struts iterator Tag

+

Struts iterator Tag

The Struts iterator tag is used to generate a loop that iterates over each item in a collection. In the thankyou.jsp is this markup.

@@ -193,7 +202,7 @@

The value of the iterator tag is states, which causes the Struts 2 framework to call the getStates method of the Action class (EditAction.java). The getStates method returns a List of State objects. The State class has two fields stateAbbr and stateName, both having the appropriate get method. The iterator will loop over each State object stored in the collection. Each time through the loop, the Struts 2 framework will have a reference to the cur [...] -

Additional Iterator Attributes

+

Additional Iterator Attributes

The Struts 2 iterator tag has additional attributes you can use to control the begin and end values for specifying that the iterator tag should only loop over a part of the collection. See the iterator tag reference for more information.

diff --git a/content/getting-started/debugging-struts.html b/content/getting-started/debugging-struts.html index 830561c..d6140ac 100644 --- a/content/getting-started/debugging-struts.html +++ b/content/getting-started/debugging-struts.html @@ -119,23 +119,42 @@
- << back to Getting Started Edit on GitHub -

Debugging Struts

+ + << back to Getting started + +

Debugging Struts

-

The example code for this tutorial, debugging_struts, is available for checkout at https://github.com/apache/struts-examples.

+ -

Introduction

+

The example code for this tutorial, debugging-struts, is available for checkout at struts-examples.

-

During development of a Struts 2 web application you may want to view the information being managed by the Struts 2 framework. This tutorial will cover two tools you can use to see how Struts 2 views your web application. One tool is the Struts 2 configuration plugin and the other is the debugging interceptor. This article also discusses how to set the log level to see more or fewer log messages.

+

Introduction

-

The Struts 2 user mailing list is an excellent place to get help. If you are having a problem getting the tutorial example applications to work search the Struts 2 mailing list. If you don’t find an answer to your problem, post a question on the mailing list.

+

During development of a Struts 2 web application you may want to view the information being managed by the Struts 2 framework. +This tutorial will cover two tools you can use to see how Struts 2 views your web application. One tool is the Struts 2 +configuration plugin and the other is the debugging interceptor. This article also discusses how to set the log level +to see more or fewer log messages.

-

Configuration Plugin

+

The Struts 2 user mailing list is an excellent place to get help. If you are having +a problem getting the tutorial example applications to work search the Struts 2 mailing list. If you don’t find an answer +to your problem, post a question on the mailing list.

-

The Struts 2 config browser plugin provides details about the configuration the Struts 2 framework is using to run your application. To use the plugin your application needs to have the struts2-config-browser-plugin-x.x.x.x.jar in your application’s class path. The config-browser-plugin jar is part of the Struts 2 download and is also available in the Maven repository.

+

Configuration Plugin

-

To use the plugin in your application, just call index.action in namespace config-browser. For example you could have the following link on your admin page (or just anywhere during your development).

+

The Struts 2 config browser plugin provides details about the configuration the Struts 2 framework is using to run your +application. To use the plugin your application needs to have the struts2-config-browser-plugin-x.x.x.x.jar in your +application’s class path. The config-browser-plugin jar is part of the Struts 2 download and is also available in the Maven +repository.

+ +

To use the plugin in your application, just call index.action in namespace config-browser. For example you could have +the following link on your admin page (or just anywhere during your development).

<a href='<s:url action="index" namespace="config-browser" />'>Launch the configuration browser</a>
 
@@ -147,35 +166,55 @@

debuging-struts-1.png

-

You can click on an action link to see the configuration for that action. For example clicking on the register action link results in this.

+

You can click on an action link to see the configuration for that action. For example clicking on the register action +link results in this.

debuging-struts-2.png

-

You may then click on one of the tabs (Results, Exception Mappings, Interceptors, Properties, Validators) to get more information about how the Struts 2 framework is has configured that specific action.

+

You may then click on one of the tabs (Results, Exception Mappings, Interceptors, Properties, Validators) to get more +information about how the Struts 2 framework is has configured that specific action.

-

On the left side of the page is the config browser plugin menu. Clicking on constants will render a view that lists all the Struts 2 constants and their properties for your application. Clicking on beans shows the beans Struts 2 is managing. Clicking on Jars shows the jars your application is using.

+

On the left side of the page is the config browser plugin menu. Clicking on constants will render a view that lists all +the Struts 2 constants and their properties for your application. Clicking on beans shows the beans Struts 2 is managing. +Clicking on Jars shows the jars your application is using.

-

Using the Debugging Interceptor

+

Using the Debugging Interceptor

-

If you have set devMode to true (in the example application see struts.xml) then one of the interceptors that is activated when Struts 2 processes an action is the DebuggingInterceptor. The DebuggingInterceptor looks for a query string appended to the action URL with a name of debug and a value of xml, console, command, or browser.

+

If you have set devMode to true (in the example application see struts.xml) then one of the interceptors that is +activated when Struts 2 processes an action is the DebuggingInterceptor. The DebuggingInterceptor looks for a query +string appended to the action URL with a name of debug and a value of xml, console, command, or browser.

-

If the DebuggingInterceptor finds that query string then it will halt further execution of the action and instead return to the browser debugging information. The format of the returned information depends on the value of the debug query parameter. See DebuggingInterceptor for more detail.

+

If the DebuggingInterceptor finds that query string then it will halt further execution of the action and instead return +to the browser debugging information. The format of the returned information depends on the value of the debug query parameter. +See DebuggingInterceptor for more detail.

-

In the example application on the index.jsp is a link for displaying debugging information. This link includes the query string debug=browser. If you click on this link you’ll see a table with columns that can be expanded and collapsed. The table contains the various objects and their state being managed by the Struts 2 framework.

+

In the example application on the index.jsp is a link for displaying debugging information. This link includes the query +string debug=browser. If you click on this link you’ll see a table with columns that can be expanded and collapsed. +The table contains the various objects and their state being managed by the Struts 2 framework.

debuging-struts-3.png

-

Note that to enable the correct display and interaction of the expand/collapse links on the debugging information web page you need to include the s:head tag in your JSP’s head section (see index.jsp in the example application) and also include the Struts 2 dojo plugin in your application’s class path. The Struts 2 dojo plugin is available as part of the Struts 2 download and from the Maven repository.

+

Note that to enable the correct display and interaction of the expand/collapse links on the debugging information web page +you need to include the s:head tag in your JSP’s head section (see index.jsp in the example application) and also include +the Struts 2 dojo plugin in your application’s class path. The Struts 2 dojo plugin is available as part of the Struts 2 +download and from the Maven repository.

-

Struts 2 Logging

+

Struts 2 Logging

-

The Struts 2 framework will write to a log a great deal of information if you’ve configured the log properties to log at the debug level. In the example application, view log4j.xml. The two major packages involved in the Struts 2 framework, com.opensymphony and org.apache.struts2, are configured to write debug and above log messages. When you run the application view th [...] +

The Struts 2 framework will write to a log a great deal of information if you’ve configured the log properties to log at +the debug level. In the example application, view log4j2.xml. The two major packages involved in the Struts 2 framework, +com.opensymphony and org.apache.struts2, are configured to write debug and above log messages. When you run the application +view the standard out for your Servlet container to see all the information written to the log. Please check Logging +page for other options.

-

Summary

+

Summary

-

Using the configuration browser plugin and the debugging interceptor can assist you in trouble shooting a problem with a Struts 2 web application. These tools should only be used in development.

+

Using the configuration browser plugin and the debugging interceptor can assist you in trouble shooting a problem with +a Struts 2 web application. These tools should only be used in development.

-

Prior to creating your war file for deployment to production you should change devmode to false and remove the debugging links. You may also want to adjust the level of logging in your log properties file to a higher level (info or warn) to reduce the number of log messages.

+

Prior to creating your war file for deployment to production you should change devMode to false and remove the debugging +links. You may also want to adjust the level of logging in your log properties file to a higher level (info or warn) to reduce +the number of log messages.

diff --git a/content/getting-started/exception-handling.html b/content/getting-started/exception-handling.html index b00d941..a6faab4 100644 --- a/content/getting-started/exception-handling.html +++ b/content/getting-started/exception-handling.html @@ -119,19 +119,30 @@
- << back to Getting Started Edit on GitHub -

Exception Handling

+ + << back to Getting started + +

Exception Handling

-

The code for this tutorial, exception_handling, is available for checkout at https://github.com/apache/struts-examples.

+ -

Introduction

+

The code for this tutorial, exception-handling, is available for checkout at struts-examples.

+ +

Introduction

In this tutorial we’ll explore how to enable the Struts 2 framework to handle any uncaught exceptions generated by a web application. Struts 2 provides robust exception handling, including the ability to automatically log any uncaught exceptions and redirect the user to a error web page.

The Struts 2 user mailing list is an excellent place to get help. If you are having a problem getting the tutorial example applications to work search the Struts 2 mailing list. If you don’t find an answer to your problem, post a question on the mailing list.

-

Global Exception Handling

+

Global Exception Handling

Using the Struts 2 framework you can specify in the struts.xml how the framework should handle uncaught exceptions. The handling logic can apply to all actions (global exception handling) or to a specific action. Let’s first discuss how to enable global exception handling.

@@ -153,7 +164,7 @@

The global results mapping node relates the result value to a specific view page. For example the result “securityerror” will cause the framework to redirect the user’s browser to the securityerror.jsp view page.

-

Exception Handling Per Action

+

Exception Handling Per Action

If you need to handle an exception in a specific way for a certain action you can use the exception-mapping node within the action node.

@@ -169,7 +180,7 @@

You can see that an action-specific exception mapping will take precedence if the same exception is also mapped globally.

-

Logging Exceptions

+

Logging Exceptions

You can configure the Struts 2 framework to log any uncaught exceptions. To enable logging of the exceptions being handled by the Struts 2 framework you must specify some parameter values in struts.xml. If you examine the ExceptionMappingInterceptor class API there are three parameter values you can set to enable logging (logEnabled), the log level to [...] @@ -196,7 +207,7 @@

In the example applications, the logging is just to the Servlet container’s console (see the log4j.xml file for the log settings).

-

Display Exception Information In Browser

+

Display Exception Information In Browser

You can display information about the exception in the browser if you want by using s:property tags with a value of exception and exceptionStack. For example in error.jsp is this markup.

@@ -212,7 +223,7 @@

When the exception interceptor is triggered it adds to the fields available for display the exception message and the exception’s stack trace.

-

Summary

+

Summary

Struts 2 provides a easy to use configuration for handling uncaught exceptions and redirecting users to appropriate view pages. You can configure exception handling to be global for all actions or to just for a specific action. You can also enable the Struts 2 framework to log the uncaught exceptions.

diff --git a/content/getting-started/exclude-parameters.html b/content/getting-started/exclude-parameters.html index 6489bab..56b9d6e 100644 --- a/content/getting-started/exclude-parameters.html +++ b/content/getting-started/exclude-parameters.html @@ -119,21 +119,42 @@
- << back to Getting Started Edit on GitHub -

Exclude Parameters

- -

The example code for this tutorial, exclude_parameters, is available at https://github.com/apache/struts-examples.

- -

Introduction

- -

When Struts development mode is set to true (also see Debugging Struts) the framework writes many informative messages to the log file. These messages include ones that indicate whether or not a specific parameter will be handled by the parameter interceptor and made available to the Action class. These log messages can be helpful in clearly identifying parameters that you do not want the [...] - -

The Struts 2 user mailing list is an excellent place to get help. If you are having a problem getting the tutorial example applications to work search the Struts 2 mailing list. If you don’t find an answer to your problem, post a question on the mailing list.

- -

Processing Request Parameters

- -

Most request parameters are by default processed by the parameter interceptor and Struts 2 will attempt to modify the state of those Action class fields that match up to a parameter name by calling a corresponding public set method. For example if the request includes a parameter of lastName with a value of Phillips, Struts 2 will try to call a public method with a signature of setLastName(String lastName). However, there may be request parameters that you do not want Struts 2 to try [...] + + << back to Getting started + +

Exclude Parameters

+ + + +

The example code for this tutorial, exclude-parameters, is available at struts-examples.

+ +

Introduction

+ +

When Struts development mode is set to true (also see Debugging Struts) +the framework writes many informative messages to the log file. These messages include ones that indicate whether +or not a specific parameter will be handled by the parameter interceptor and made available to the Action class. +These log messages can be helpful in clearly identifying parameters that you do not want the parameter interceptor +to process for security or other reasons. This article discusses how to exclude parameters from being handled by +the parameter interceptor.

+ +

The Struts 2 user mailing list is an excellent place to get help. If you are having +a problem getting the tutorial example applications to work search the Struts 2 mailing list. If you don’t find an answer +to your problem, post a question on the mailing list.

+ +

Processing Request Parameters

+ +

Most request parameters are by default processed by the parameter interceptor and Struts 2 will attempt to modify the state +of those Action class fields that match up to a parameter name by calling a corresponding public set method. For example +if the request includes a parameter of lastName with a value of Phillips, Struts 2 will try to call a public method with +a signature of setLastName(String lastName). However, there may be request parameters that you do not want Struts 2 +to try to set the value of in the Action class.

Consider this code which creates a form:

@@ -154,7 +175,8 @@ -

The s:submit tag will create a submit button with a name of submit. Since the Action class probably doesn’t have a setSubmit(String name) method you will see the following log messages (only if Struts development mode is set to true):

+

The s:submit tag will create a submit button with a name of submit. Since the Action class probably doesn’t have +a setSubmit(String name) method you will see the following log messages (only if Struts development mode is set to true):

Log Messages

@@ -168,11 +190,14 @@ Unexpected Exception caught setting 'submit' on 'class org.apache.struts.edit.ac -

Excluding Request Parameters From Struts 2 Processing

+

Excluding Request Parameters From Struts 2 Processing

If you’re not familiar with setting up a custom interceptor stack for your Struts 2 application review Introducing Interceptors.

-

To exclude specific parameters from being processed by the Struts 2 framework you need to add those parameter names to the list of excluded parameters. One way to do this is by adding those parameter names to the collection of excludedParams for the Parameters interceptor. You can do this by modifying the Parameters interceptor in setting up the stack of interceptors used by your Struts 2 application. For example:

+

To exclude specific parameters from being processed by the Struts 2 framework you need to add those parameter names +to the list of excluded parameters. One way to do this is by adding those parameter names to the collection of excludedParams +for the Parameters interceptor. You can do this by modifying the Parameters interceptor in setting up the stack of interceptors +used by your Struts 2 application. For example:

Setup Interceptor Stack To Exclude submit Parameter

@@ -190,19 +215,29 @@ Unexpected Exception caught setting 'submit' on 'class org.apache.struts.edit.ac -

The value of node <param name="params.excludeParams"> is a comma-delimited list of regular expressions or simple Strings that identify request parameters that should NOT be processed by the Parameters interceptor. To exclude the submit parameter (which is the name of the submit button in the form code above), I just added submit to the list.

+

The value of node <param name="params.excludeParams"> is a comma-delimited list of regular expressions or simple +Strings that identify request parameters that should NOT be processed by the Parameters interceptor. To exclude +the submit parameter (which is the name of the submit button in the form code above), I just added submit to the list.

-

See the Basic Stack of Interceptors described here to view the initial set of parameter names/regular expressions to exclude. Be sure to copy over the list of parameters already being excluded and then add your own parameters to the end separated by commas.

+

See the Basic Stack of Interceptors described here to view the initial +set of parameter names/regular expressions to exclude. Be sure to copy over the list of parameters already being excluded +and then add your own parameters to the end separated by commas.

-

Example Application

+

Example Application

-

Download the example application, Exclude_Params_Struts2_Mvn that demonstrates excluding a request parameter. The download is a zipped Maven project. You should be able to unzip it and import the project into any Maven-aware Java IDE. See the project’s README.txt file for how to build and run the application.

+

Download the example application, exclude-params +that demonstrates excluding a request parameter. See the project’s README.txt file for how to build and run the application.

-

To see the log messages written when not excluding the submit parameter remove the “,submit” from the list of excluded parameter values in the struts.xml file. Then rebuild and redeploy the application and view the console when running the application.

+

To see the log messages written when not excluding the submit parameter remove the ,submit from the list of excluded +parameter values in the struts.xml file. Then rebuild and redeploy the application and view the console when running the application.

-

Summary

+

Summary

-

It’s a nice feature of the Struts 2 framework that it logs during development which request parameters will and will not be processed. During development of a Struts 2 web application it’s a good practice to review these log messages to determine if there are any parameters that the framework should not process. For those parameters the Struts 2 framework should not process add the parameter name (or a regular expression that can be used to identify multiple parameter names) to the co [...] +

It’s a nice feature of the Struts 2 framework that it logs during development which request parameters will and will not +be processed. During development of a Struts 2 web application it’s a good practice to review these log messages to determine +if there are any parameters that the framework should not process. For those parameters the Struts 2 framework should +not process add the parameter name (or a regular expression that can be used to identify multiple parameter names) +to the comma-delimited list that is the value for the <param name="params.excludeParams"> node.

diff --git a/content/getting-started/form-tags.html b/content/getting-started/form-tags.html index 4bd3cc2..1e6e558 100644 --- a/content/getting-started/form-tags.html +++ b/content/getting-started/form-tags.html @@ -119,13 +119,26 @@
- << back to Getting Started Edit on GitHub -

Form Tags

- -

The example code for this tutorial, form_tags, can be checked out from https://github.com/apache/struts-examples.

- -

Introduction

+ + << back to Getting started + +

Form Tags

+ + + +

The example code for this tutorial, form-tags, can be checked out from struts-examples.

+ +

Introduction

In this tutorial we’ll explore some of the other Struts 2 form controls. In our previous tutorials that explained how to use Struts 2 forms (Processing forms, Form validation, @@ -137,7 +150,7 @@ form controls.

having a problem getting the tutorial example applications to work search the Struts 2 mailing list. If you don’t find an answer to your problem, post a question on the mailing list.

-

Example Application

+

Example Application

The example application that supports this tutorial shows how to use Struts 2 form tags so that a user can edit his information. The information that can be edited is encapsulated in an object of class Person. A Person object knows @@ -154,7 +167,7 @@ of the Person object.

The first and last names are shown on the form (see edit.jsp) using the Struts 2 textfield tag, which we’ve discussed in previous tutorials.

-

Struts 2 Select Tag

+

Struts 2 Select Tag

A user can select one favorite sport from several choices. The example application uses the Struts 2 select tag to provide the list of options for the select box.

@@ -212,7 +225,7 @@ head section.

Since the personBean’s getSport method returns “baskeball”, the basketball option value is marked as selected.

-

Struts 2 Radio Tag

+

Struts 2 Radio Tag

The Struts 2 radio tag—like its standard HTML counterpart—is used to display 2 or more choices, only one of which can be selected by the user. Here is the code for the Struts 2 radio button from the example application.

@@ -251,7 +264,7 @@ The Array of String objects returned are used to create the individual radio but

Also just like the Struts 2 select tag the result returned by calling the personBean object’s getGender method is used to determine which of the radio buttons is checked.

-

Struts 2 Select Tag - Object Backed

+

Struts 2 Select Tag - Object Backed

You may need to create a Struts 2 select tag where the options displayed to the user each have their own value that is different then what is displayed. In the example application, the user’s residency is stored as a two-letter @@ -294,7 +307,7 @@ displayed to the user. So the above Struts 2 select tag code results in this HTM tags is marked as selected. In our example, since getResidency returns “KS”, the option tag whose value attribute equals “KS” is marked as selected.

-

Struts 2 Checkbox Tag

+

Struts 2 Checkbox Tag

The Struts 2 checkbox tag is used to create the HTML input type equals checkbox tag. The value for the key attribute tells the framework what method to call to determine if the checkbox is checked or not checked. The method called should @@ -333,7 +346,7 @@ hidden form field to check if the associated checkbox field exists in the posted doesn’t exist then the Struts 2 framework will know to update the value of the personBean object’s over21 instance variable to false.

-

Struts 2 checkboxlist Tag

+

Struts 2 checkboxlist Tag

The Struts 2 framework provides a unique form field control that creates a series of associated check boxes, one or more of which can be checked. In the example application, the Person class has an Array of Strings, which is used to store @@ -383,7 +396,7 @@ the checkbox will be checked.

-

Summary

+

Summary

There are several other Struts 2 form controls you should explore. If you need more information about the Struts 2 form tags consult the Struts 2 documentation at http://struts.apache.org.

diff --git a/content/getting-started/form-validation-using-xml.html b/content/getting-started/form-validation-using-xml.html index b560c61..901ec93 100644 --- a/content/getting-started/form-validation-using-xml.html +++ b/content/getting-started/form-validation-using-xml.html @@ -119,19 +119,32 @@
- << back to Getting Started Edit on GitHub -

Form Validation Using XML

- -

The example code for this tutorial, form_xml_validation, is available for checkout at https://github.com/apache/struts-examples

- -

Introduction

+ + << back to Getting started + +

Form Validation Using XML

+ + + +

The example code for this tutorial, form-xml-validation, is available for checkout at struts-examples

+ +

Introduction

In this tutorial we’ll cover how to validate a user’s input in form fields using Struts 2’s XML validation methodology. In the Form Validation tutorial we discussed validating a user’s input using the validate method in the Action class. Using a separate XML validation file gives you the ability to use validators built-in to the Struts 2 framework.

The Struts 2 user mailing list is an excellent place to get help. If you are having a problem getting the tutorial example applications to work search the Struts 2 mailing list. If you don’t find an answer to your problem, post a question on the mailing list.

-

Example Application

+

Example Application

The example application that supports this tutorial shows how to use Struts 2’s XML validation methodology. The information that can be edited is encapsulated in an object of class Person.

@@ -141,7 +154,7 @@

When the user submits the form, we want to validate his entries into the form fields.

-

Validation Using XML

+

Validation Using XML

To validate a user’s form field entries you can use a separate XML file that contains your validation rules. The XML file that contains the validation rules must be named as ActionClassName-validation.xml. In the example application, the XML validation file is named EditAction-validation.xml (see src/main/resources/org/apache/struts/edit/action).

@@ -149,7 +162,7 @@

In the above form, we want to ensure the user enters a first name. To have the Struts 2 framework enforce that rule we can used the Struts 2 requiredstring validator. This validator checks that the user has entered a string value in the form field.

-

XML Validator Format

+

XML Validator Format

In the XML validation file (for this example that is EditAction-validation.xml), is this XML:

@@ -180,7 +193,7 @@

form-validation-2.png

-

Validating An Email Address

+

Validating An Email Address

You can use the Struts 2 email validator to validate the user’s input in the email field. Here is the validator node that is in the EditAction-validation.xml file.

@@ -199,7 +212,7 @@

Note that in the example, we are requiring the user to enter an email address and then validating the email address the user entered.

-

Validating A User’s Input Using A Regular Expression

+

Validating A User’s Input Using A Regular Expression

The Struts 2 framework provides a powerful way to validate a user’s form field input by using the regex validator . In the example application, we want to ensure the user enters the phone number in the format 999-999-9999. We can use a regular expression and the regex validator to enforce this rule.

@@ -219,7 +232,7 @@

The param name=”expression” node is used to specify the regular expression that will be applied to the user’s input. Note how the regular expression is contained within a CDATA section.

-

Validating A User’s Input Using An OGNL Expression

+

Validating A User’s Input Using An OGNL Expression

In the example application, we want to ensure the user checks at least one of the car model check boxes. To enforce this rule we can use the fieldexpression validator . Here’s the XML for that validator node.

@@ -243,7 +256,7 @@

The fieldexpression validator is useful when doing conditional validation of a user’s input. If the OGNL expression doesn’t evaluate to true then the user’s input won’t be allowed.

-

Summary

+

Summary

The Struts 2 framework provides easy-to-use validation methodologies. You can add a validate method to the Action class or have a separate XML file with validation rules or you can use a combination of both methodologies.

diff --git a/content/getting-started/form-validation.html b/content/getting-started/form-validation.html index 2367c0f..3c4c38b 100644 --- a/content/getting-started/form-validation.html +++ b/content/getting-started/form-validation.html @@ -119,25 +119,40 @@
- << back to Getting Started Edit on GitHub -

Form Validation

+ + << back to Getting started + +

Form Validation

-

This tutorial assumes you’ve completed the Processing Forms tutorial and have a working form_processing project. The example code for this tutorial, form_validation, is available for checkout from the Struts 2 GitHub repository: https://github.com/apache/struts-examples.

+ -
+

This tutorial assumes you’ve completed the Processing Forms tutorial and have a working +form-processing project. The example code for this tutorial, form-validation, is available for checkout from +the Struts 2 GitHub repository struts-examples.

-
+

Introduction

-

Introduction

+

In this tutorial we’ll explore using Struts 2 to validate the user’s input on a form. There are two ways you can use Struts 2 +to do form validation. This tutorial will cover the more basic method, where the validation is included in the Struts 2 Action class.

-

In this tutorial we’ll explore using Struts 2 to validate the user’s input on a form. There are two ways you can use Struts 2 to do form validation. This tutorial will cover the more basic method, where the validation is included in the Struts 2 Action class.

+

The Struts 2 user mailing list is an excellent place to get help. If you are having +a problem getting the tutorial example applications to work search the Struts 2 mailing list. If you don’t find an answer +to your problem, post a question on the mailing list.

-

The Struts 2 user mailing list is an excellent place to get help. If you are having a problem getting the tutorial example applications to work search the Struts 2 mailing list. If you don’t find an answer to your problem, post a question on the mailing list.

+

Add validate Method

-

Add validate Method

- -

To enable the Struts 2 Action class to validate a user’s input on a Struts 2 form, you must define a validate method in your Action class. Using the example from Processing Forms tutorial, let’s say that we have these business rules:

+

To enable the Struts 2 Action class to validate a user’s input on a Struts 2 form, you must define a validate method +in your Action class. Using the example from Processing Forms tutorial, let’s say that we have +these business rules:

  1. User must provide a first name
  2. @@ -145,11 +160,14 @@
  3. User younger then 18 cannot register
-

If you recall from the Processing Forms tutorial the user’s input into the form fields is placed by Struts 2 into the Java model class personBean. So a user’s input into the firstName field would end up as the value for personBean’s firstName instance field (via the personBean.setFirstName method).

+

If you recall from the Processing Forms tutorial the user’s input into the form fields is placed +by Struts 2 into the Java model class personBean. So a user’s input into the firstName field would end up as the value +for personBean’s firstName instance field (via the personBean.setFirstName method).

-

In the validate method we can refer to get the values of personBean’s instance fields by using the appropriate get methods. Once we have the values we can employ logic to enforce our business rules.

+

In the validate method we can refer to get the values of personBean’s instance fields by using the appropriate get methods. +Once we have the values we can employ logic to enforce our business rules.

-

Add the following validate method to Register.java (the Action class).

+

Add the following validate method to Register.java (the Action class).

validate method

@@ -169,15 +187,19 @@ -

When the user presses the submit button on the register form, Struts 2 will transfer the user’s input to the personBean’s instance fields. Then Struts 2 will automatically execute the validate method. If any of the if statements are true, Struts 2 will call its addFieldError method (which our Action class inherited by extending ActionSupport).

+

When the user presses the submit button on the register form, Struts 2 will transfer the user’s input to the personBean’s +instance fields. Then Struts 2 will automatically execute the validate method. If any of the if statements are true, +Struts 2 will call its addFieldError method (which our Action class inherited by extending ActionSupport).

-

If any errors have been added then Struts 2 will not proceed to call the execute method. Rather the Struts 2 framework will return “input” as the result of calling the action.

+

If any errors have been added then Struts 2 will not proceed to call the execute method. Rather the Struts 2 framework +will return input as the result of calling the action.

-

Handle Input Being Returned

+

Handle Input Being Returned

-

So what should we do if Struts 2 returns “input” indicating that the user’s input in the form is not valid? In most cases we will want to redisplay the web page that has the form and include in the form error messages to inform the user what is wrong.

+

So what should we do if Struts 2 returns input indicating that the user’s input in the form is not valid? In most cases +we will want to redisplay the web page that has the form and include in the form error messages to inform the user what is wrong.

-

To handle the return value of “input” we need to add the following result to our action node in struts.xml.

+

To handle the return value of input we need to add the following result to our action node in struts.xml.

<result name="input">/register.jsp</result>
 
@@ -185,35 +207,44 @@

The above result node goes just after the success result node for the register action and before the closing of the action node.

-

Error Messages

+

Error Messages

-

So when validation fails and Struts 2 returns input, the Struts 2 framework will redisplay the register.jsp. Since we used Struts 2 form tags, automatically Struts 2 will add the error messages. These error messages are the ones we specified in the addFieldError method call. The addFieldError method takes two arguments. The first is the form field name to which the error applies and the second is the error message to display above that form field.

+

So when validation fails and Struts 2 returns input, the Struts 2 framework will redisplay the register.jsp. Since we +used Struts 2 form tags, automatically Struts 2 will add the error messages. These error messages are the ones we specified +in the addFieldError method call. The addFieldError method takes two arguments. The first is the form field name to which +the error applies and the second is the error message to display above that form field.

-

So the following addFieldError method call:

+

So the following addFieldError method call:

addFieldError("personBean.firstName", "First name is required.")
 
-

will cause the message “First name is required” to be displayed above the firstName field on the form.

+

will cause the message First name is required to be displayed above the firstName field on the form.

-

If you have made the above changes to the Processing Forms tutorial or you have downloaded from Google Code either the Form_Validation_Struts2_Ant or Form_Validation_Struts2_Mvn projects run the application (see the README.txt in the project root folder). Click on the Please register link. On the registration form, just click the submit button and you should see:

+

If you have made the above changes to the Processing Forms tutorial or you have downloaded +from form-validation +run the application (see the README.txt in the project root folder). Click on the Please register link. On the registration +form, just click the submit button and you should see:

form_errors.png

-

Struts 2 called the validate method, validation failed, the register.jsp was displayed with the error messages.

+

Struts 2 called the validate method, validation failed, the register.jsp was displayed with the error messages.

-

Styling The Error Messages

+

Styling The Error Messages

-

The Struts 2 s:head tag can be used to provide CSS that includes a style for the error message. Add <s:head /> to register.jsp before the closing HTML </head> tag. Go through the same steps as above and you should see:

+

The Struts 2 s:head tag can be used to provide CSS that includes a style for the error message. Add <s:head /> +to register.jsp before the closing HTML </head> tag. Go through the same steps as above and you should see:

form_errors_styled.png

-

Summary

+

Summary

-

This tutorial covered validating a user’s form input by adding a validate method to an Action class. There is another more sophisticated way to validate user input using XML. If you want to learn more about using XML for validation in Struts 2 see Validation .

+

This tutorial covered validating a user’s form input by adding a validate method to an Action class. There is another +more sophisticated way to validate user input using XML. If you want to learn more about using XML for validation +in Struts 2 see Validation .

-

Up Next

+

Up Next

In our next tutorial we’ll cover how to use message resource files to separate out the text from the view page.

diff --git a/content/getting-started/hello-world-using-struts2.html b/content/getting-started/hello-world-using-struts2.html index b8d501d..0f7bcab 100644 --- a/content/getting-started/hello-world-using-struts2.html +++ b/content/getting-started/hello-world-using-struts2.html @@ -119,35 +119,56 @@
- << back to Getting Started Edit on GitHub -

Hello World Using Struts 2

+ + << back to Getting started + +

Hello World Using Struts 2

+ + -

When you click on a hyperlink or submit an HTML form in a Struts 2 web application, the input is not sent to another server page, but to a Java class that you provide. These classes are called Actions. After the Action fires, a Result selects a resource to render the response. The resource is generally a server page, but it can also be a PDF file, an Excel spreadsheet, or a Java applet window.

+

Description

-

Suppose you want to create a simple “Hello World” example that displays a welcome message. After setting up an empty basic Struts 2 web application (see How To Create A Struts 2 Web Application), to create a “Hello World” example, you need to do four things:

+

When you click on a hyperlink or submit an HTML form in a Struts 2 web application, the input is not sent to another +server page, but to a Java class that you provide. These classes are called Actions. After the Action fires, a Result +selects a resource to render the response. The resource is generally a server page, but it can also be a PDF file, +an Excel spreadsheet, or a Java applet window.

+ +

Suppose you want to create a simple “Hello World” example that displays a welcome message. After setting up an empty +basic Struts 2 web application (see How To Create A Struts 2 Web Application), +to create a “Hello World” example, you need to do four things:

    -
  1. -

    Create a class to store the welcome message (the model)

    -
  2. -
  3. -

    Create a server page to present the message (the view)

    -
  4. -
  5. -

    Create an Action class to control the interaction between the user, the model, and the view (the controller)

    -
  6. -
  7. -

    Create a mapping (struts.xml) to couple the Action class and view

    -
  8. +
  9. Create a class to store the welcome message (the model)
  10. +
  11. Create a server page to present the message (the view)
  12. +
  13. Create an Action class to control the interaction between the user, the model, and the view (the controller)
  14. +
  15. Create a mapping (struts.xml) to couple the Action class and view
-

By creating these components, we are separating the work flow into three well-known concerns: the View, the Model, and the Controller. Separating concerns makes it easier to manage applications as they become more complex.

+

By creating these components, we are separating the work flow into three well-known concerns: the View, the Model, +and the Controller. Separating concerns makes it easier to manage applications as they become more complex.

-

Let’s look at an example model class, Action, server page, and mapping. If you like, fire up your Java IDE, and enter the code as we go.

+

Let’s look at an example model class, Action, server page, and mapping. If you like, fire up your Java IDE, and enter +the code as we go.

-

This tutorial assumes you’ve completed the How To Create A Struts 2 Web Application tutorial and have a working basic Struts project. The example code for this tutorial, helloworld, is available for checkout from the Struts 2 GitHub repository at https://github.com/apache/struts-examples. The example projects use Maven to manage the artifact dependencies and to build th [...] +

This tutorial assumes you’ve completed the How To Create A Struts 2 Web Application +tutorial and have a working basic Struts project. The example code for this tutorial, helloworld, is available for checkout +from the Struts 2 GitHub repository at struts-examples. The example projects +use Maven to manage the artifact dependencies and to build the .war files.

The Code

@@ -162,7 +183,9 @@
-

The Struts 2 user mailing list is an excellent place to get help. If you are having a problem getting this application to work search the Struts 2 mailing list. If you don’t find an answer to your problem, post a question on the mailing list.

+

The Struts 2 user mailing list is an excellent place to get help. If you are +having a problem getting this application to work search the Struts 2 mailing list. If you don’t find an answer +to your problem, post a question on the mailing list.

Step 1 - Create The Model Class MessageStore.java

@@ -191,11 +214,15 @@
-

Note the use of the public getter method to allow access to the private message String attribute. The Struts 2 framework requires that objects you want to expose to the view (HelloWorld.jsp) follow the JavaBean-style conventions.

+

Note the use of the public getter method to allow access to the private message String attribute. The Struts 2 framework +requires that objects you want to expose to the view (HelloWorld.jsp) follow the JavaBean-style conventions.

Step 2 - Create The Action Class HelloWorldAction.java

-

We need an Action class to act as the Controller. The Action class responds to a user action (in this example that action will be clicking an HTML hyperlink and sending a specific URL to the Servlet container). One or more of the Action class’s methods are executed and a String result is returned. Based on the value of the result, a specific view page (in this example that view page is HelloWorld.jsp) is rendered.

+

We need an Action class to act as the Controller. The Action class responds to a user action (in this example that action +will be clicking an HTML hyperlink and sending a specific URL to the Servlet container). One or more of the Action class’s +methods are executed and a String result is returned. Based on the value of the result, a specific view page (in this +example that view page is HelloWorld.jsp) is rendered.

Note the package and import statements below.

@@ -223,15 +250,19 @@ -

The Struts 2 framework will create an object of the HelloWorldAction class and call the execute method in response to a user’s action (clicking on a hyperlink that sends a specific URL to the Servlet container).

+

The Struts 2 framework will create an object of the HelloWorldAction class and call the execute method in response to +a user’s action (clicking on a hyperlink that sends a specific URL to the Servlet container).

In this example, the execute method creates an object of class MessageStore and then returns the String constant SUCCESS.

-

Note also the public getter method for the private MessageStore object. Since we want to make the MessageStore object available to the view page, HelloWorld.jsp, we need to follow the JavaBean-style of providing getter and setter methods where needed.

+

Note also the public getter method for the private MessageStore object. Since we want to make the MessageStore object +available to the view page, HelloWorld.jsp, we need to follow the JavaBean-style +of providing getter and setter methods where needed.

Step 3 - Create The View HelloWorld.jsp

-

We need a server page to present the message that is stored in the model class MessageStore. Create the below JSP in the src/main/webapp folder.

+

We need a server page to present the message that is stored in the model class MessageStore. Create the below JSP in +the src/main/webapp folder.

HelloWorld.jsp

@@ -249,17 +280,25 @@ </html> -

The taglib directive tells the Servlet container that this page will be using the Struts 2 tags and that these tags will be preceded by an s.

+

The taglib directive tells the Servlet container that this page will be using the Struts 2 tags and that these tags will +be preceded by an s.

-

The <s:property> tag displays the value returned by calling the method getMessageStore of the HelloWorldAction controller class. That method returns a MessageStore object. By adding the .message onto the messageStore part of the value attribute we are telling the Struts 2 framework to ca [...] +

The <s:property> tag displays the value returned by calling the method getMessageStore of the HelloWorldAction  +controller class. That method returns a MessageStore object. By adding the .message onto the messageStore part of +the value attribute we are telling the Struts 2 framework to call the getMessage method of that MessageStore object. +The getMessage method of class MessageStore returns a String. It is that String that will be displayed by the <s:property> tag.

We’ll learn more about tags in the next tutorial. See the Struts Tags for more information about tags.

Step 4 - Add The Struts Configuration In struts.xml

-

We need a mapping to tie the URL, the HelloWorldAction class (controller), and the HelloWorld.jsp (the view) together. The mapping tells the Struts 2 framework which class will respond to the user’s action (the URL), which method of that class will be executed, and what view to render based on the String result that method returns.

+

We need a mapping to tie the URL, the HelloWorldAction class (controller), and the HelloWorld.jsp (the view) together. +The mapping tells the Struts 2 framework which class will respond to the user’s action (the URL), which method of that +class will be executed, and what view to render based on the String result that method returns.

-

Edit the struts.xml file (in the Mvn project that file is in the src/main/resources folder) to add the action mapping. Place the action node (action name=”hello”) between the opening and closing package node, just after the action mapping with the name=”index”. Your complete struts.xml should look like:

+

Edit the struts.xml file (in the Mvn project that file is in the src/main/resources folder) to add the action mapping. +Place the action node (action name="hello") between the opening and closing package node, just after the action mapping +with the name="index". Your complete struts.xml should look like:

struts.xml

@@ -285,9 +324,11 @@

Step 5 - Create The URL Action

-

Let’s add an Action URL inside index.jsp (see src/main/webapp folder) so the user can click on a link to tell the Struts 2 framework to run the execute method of the HelloWorldAction class and render the HelloWorld.jsp view.

+

Let’s add an Action URL inside index.jsp (see src/main/webapp folder) so the user can click on a link to tell the Struts 2 +framework to run the execute method of the HelloWorldAction class and render the HelloWorld.jsp view.

-

First add the taglib directive at the top of the jsp <%@ taglib prefix="s" uri="/struts-tags" %>. Next add this p tag <p><a href="<s:url action='hello'/>">Hello World</a></p> after the h1 tag. Your new index.jsp should look like:

+

First add the taglib directive at the top of the jsp <%@ taglib prefix="s" uri="/struts-tags" %>. Next add this p tag +<p><a href="<s:url action='hello'/>">Hello World</a></p> after the h1 tag. Your new index.jsp should look like:

index.jsp

@@ -306,13 +347,17 @@ </html> -

The Struts url tag creates the URL with an action of hello. The hello action was mapped to the HelloWorldAction class and its execute method. When the user clicks on the above URL it will cause the Struts 2 framework to run the execute method of the HelloWorldAction class. After that method returns the String success (constant The Struts url tag creates the URL with an action of hello. The hello action was mapped to the HelloWorldAction class +and its execute method. When the user clicks on the above URL it will cause the Struts 2 framework to run the execute +method of the HelloWorldAction class. After that method returns the String success (constant SUCCESS), the view +page HelloWorld.jsp will be rendered.

Step 6 - Build the WAR File and Run The Application

Execute mvn jetty:run to run the application.

-

Go to this URL http://localhost:8080/helloworld/index.action where you should see the following:

+

Go to this URL http://localhost:8080/helloworld/index.action where you +should see the following:

helloworld_index_action.png

@@ -325,28 +370,29 @@

Your browser sends to the web server a request for the URL http://localhost:8080/helloworld/hello.action.

    -
  1. -

    The container receives from the web server a request for the resource hello.action. According to the settings loaded from the web.xml , the container finds that all requests are being routed to org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter, including the *.action requests. The StrutsPrepareAndEx [...] -

  2. -
  3. -

    The framework looks for an action mapping named “hello”, and it finds that this mapping corresponds to the class “HelloWorldAction”. The framework instantiates the Action and calls the Action’s execute method.

    -
  4. -
  5. -

    The execute method creates the MessageStore object and returns SUCCESS (="success"). The framework checks the action mapping to see what page to load if SUCCESS is returned. The framework tells the container to render as the response to the request, the resource HelloWorld.jsp.

    -
  6. -
  7. -

    As the page HelloWorld.jsp is being processed, the <s:property value="messageStore.message" /> tag calls the getter getMessageStore of the HelloWorld Action and then calls the getMessage of the MessageStore object returned by getMessageStore. Th [...] -

  8. -
  9. -

    A pure HTML response is sent back to the browser.

    -
  10. +
  11. The container receives from the web server a request for the resource hello.action. According to the settings loaded f + rom the web.xml, the container finds that all requests are being routed to + org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter, including the *.action requests. + The StrutsPrepareAndExecuteFilter is the entry point into the framework.
  12. +
  13. The framework looks for an action mapping named “hello”, and it finds that this mapping corresponds to the class + HelloWorldAction. The framework instantiates the Action and calls the Action’s execute method.
  14. +
  15. The execute method creates the MessageStore object and returns SUCCESS (="success"). The framework checks + the action mapping to see what page to load if SUCCESS is returned. The framework tells the container to render + as the response to the request, the resource HelloWorld.jsp.
  16. +
  17. As the page HelloWorld.jsp is being processed, the <s:property value="messageStore.message" /> tag calls the getter + getMessageStore of the HelloWorld Action and then calls the getMessage of the MessageStore object returned + by getMessageStore. The tag merges the value of the message attribute into the response.
  18. +
  19. A pure HTML response is sent back to the browser.

What to Remember

-

The framework uses Actions to process HTML forms and other requests. The Action class returns a result-name such as SUCCESS, ERROR or INPUT. Based on the mappings loaded from the struts.xml, a given result-name may select a page (as in this example), another action, or some other web resource [...] +

The framework uses Actions to process HTML forms and other requests. The Action class returns a result-name such +as SUCCESS, ERROR or INPUT. Based on the mappings loaded from the struts.xml, a given result-name may select +a page (as in this example), another action, or some other web resource (image, PDF).

-

When a server page is rendered, most often it will include dynamic data provided by the Action. To make it easy to display dynamic data, the framework provides a set of tags that can be used along with HTML markup to create a server page.

+

When a server page is rendered, most often it will include dynamic data provided by the Action. To make it easy to display +dynamic data, the framework provides a set of tags that can be used along with HTML markup to create a server page.

diff --git a/content/getting-started/how-to-create-a-struts2-web-application.html b/content/getting-started/how-to-create-a-struts2-web-application.html index 135e2c3..70aaed6 100644 --- a/content/getting-started/how-to-create-a-struts2-web-application.html +++ b/content/getting-started/how-to-create-a-struts2-web-application.html @@ -119,36 +119,54 @@
- << back to Getting Started Edit on GitHub -

How To Create A Struts 2 Web Application

+ + << back to Getting started + +

How To Create A Struts 2 Web Application

+ +

This tutorial walks through installing the framework and creating a simple application.

-
+

While the Struts 2 framework is simple to use, creating non-trivial applications assumes a working knowledge +of many J2EE technologies, including:

-While the Struts 2 framework is simple to use, creating non-trivial applications assumes a working knowledge -of many J2EE technologies, including: +
    +
  • Java
  • +
  • Filters, JSP, and Tag Libraries
  • +
  • JavaBeans
  • +
  • HTML and HTTP
  • +
  • Web Containers (such as Tomcat)
  • +
  • XML
  • +
-
    -
  • Java
  • -
  • Filters, JSP, and Tag Libraries
  • -
  • JavaBeans
  • -
  • HTML and HTTP
  • -
  • Web Containers (such as Tomcat)
  • -
  • XML
  • -
- -
- -

For more about supporting technologies, -see the Key Technologies Primer.

+
+

For more about supporting technologies, see the Key Technologies Primer.

+
-

Java Requirements

+

Java Requirements

Struts 2 requires Servlet API 2.4 or higher, JSP 2.0 or higher, and Java 7 or higher.

-

Our First Application

+

Our First Application

To get started using Struts 2 we will create a web application using Maven to manage the artifact dependencies. You can checkout all the example applications from the Struts 2 GitHub repository diff --git a/content/getting-started/http-session.html b/content/getting-started/http-session.html index c8d6c27..1435c42 100644 --- a/content/getting-started/http-session.html +++ b/content/getting-started/http-session.html @@ -119,8 +119,10 @@

- << back to Getting Started Edit on GitHub + + << back to Getting started +

HTTP Session

-

The example code for this tutorial, http_session, is available at [https://github.com/apache/struts-examples].

+

The example code for this tutorial, http-session, is available at struts-examples.

Introduction

diff --git a/content/getting-started/introducing-interceptors.html b/content/getting-started/introducing-interceptors.html index 734b0bf..7d3df1b 100644 --- a/content/getting-started/introducing-interceptors.html +++ b/content/getting-started/introducing-interceptors.html @@ -119,63 +119,76 @@
- << back to Getting Started Edit on GitHub -

Introducing Interceptors

+ + << back to Getting started + +

Introducing Interceptors

-

The example code for this tutorial, interceptors, is available at https://github.com/apache/struts-examples

+ -

Introduction

+

The example code for this tutorial, interceptors, is available at struts-examples

-

So far our tutorials have not delved into the inner workings of the Struts 2 framework. But in this tutorial we’ll introduce a key set of classes the Struts 2 framework relies upon to do most of the work whenever an Action is executed. In this tutorial’s example project there is a register link that is mapped in the Struts XML configuration file (struts.xml) to the execute method of class Register. Before that execute method is called much work is done behind the scenes by the Struts [...] +

Introduction

+ +

So far our tutorials have not delved into the inner workings of the Struts 2 framework. But in this tutorial we’ll +introduce a key set of classes the Struts 2 framework relies upon to do most of the work whenever an Action is executed. +In this tutorial’s example project there is a register link that is mapped in the Struts XML configuration file (struts.xml) +to the execute method of class Register. Before that execute method is called much work is done behind the scenes +by the Struts 2 framework. For example:

    -
  1. -

    Handling any exceptions generated

    -
  2. -
  3. -

    Converting the String request parameters to the Register class’s instance fields where the name values match

    -
  4. -
  5. -

    Calling the validate method and/or the external XML validation

    -
  6. +
  7. Handling any exceptions generated
  8. +
  9. Converting the String request parameters to the Register class’s instance fields where the name values match
  10. +
  11. Calling the validate method and/or the external XML validation

After the execute method is completed more work must be done

    -
  1. -

    Handling any exceptions generated

    -
  2. -
  3. -

    Converting the Register class’s instance fields to String values for display in the view page

    -
  4. -
  5. -

    Forwarding to the correct view page depending on the result String returned by the execute method

    -
  6. +
  7. Handling any exceptions generated
  8. +
  9. Converting the Register class’s instance fields to String values for display in the view page
  10. +
  11. Forwarding to the correct view page depending on the result String returned by the execute method

The above list of tasks are not complete - several other tasks are done before and after the execution of the Action.

-

The benefit of using Struts 2 is all this work happens automatically. You can focus on the logic of the controller (the Struts 2 ActionSupport class), the Service layer, the data access layer, your domain models, etc.

+

The benefit of using Struts 2 is all this work happens automatically. You can focus on the logic of the controller +(the Struts 2 ActionSupport class), the Service layer, the data access layer, your domain models, etc.

-

The Struts 2 user mailing list is an excellent place to get help. If you are having a problem getting the tutorial example applications to work search the Struts 2 mailing list. If you don’t find an answer to your problem, post a question on the mailing list.

+

The Struts 2 user mailing list is an excellent place to get help. If you are +having a problem getting the tutorial example applications to work search the Struts 2 mailing list. If you don’t find +an answer to your problem, post a question on the mailing list.

-

Introducing Interceptors

+

Introducing Interceptors

-

The tasks that are done by the Struts 2 framework before and after an Action is executed are done by Struts 2 interceptors. Interceptors are standard Java classes included in the Struts 2 core jar which are executed in a specific order.

+

The tasks that are done by the Struts 2 framework before and after an Action is executed are done by Struts 2 interceptors. +Interceptors are standard Java classes included in the Struts 2 core jar which are executed in a specific order.

-

In our example application there is a package node in struts.xml. The package node has an attribute of extends with a value of “struts-default.” The value “struts-default” identifies to the framework the specific stack of interceptors that will be executed before and after the Actions in that package.

+

In our example application there is a package node in struts.xml. The package node has an attribute of extends with +a value of “struts-default.” The value “struts-default” identifies to the framework the specific stack of interceptors +that will be executed before and after the Actions in that package.

-

If you want to learn more about the inner workings of interceptors, what interceptors belong to the struts default stack, and what are all the interceptors included with Struts 2, visit Understanding Interceptors .

+

If you want to learn more about the inner workings of interceptors, what interceptors belong to the struts default stack, +and what are all the interceptors included with Struts 2, visit Understanding Interceptors .

-

Sometime the Struts 2 default stack of interceptors are not exactly what you need for a particular action. You may want to use interceptors that are not part of the Struts 2 default stack. For an individual Action or for the entire package of Actions, you can specify a different stack of interceptors that the Action or package should use. Below is how you would specify that the register Action should use both the logger an [...] +

Sometime the Struts 2 default stack of interceptors are not exactly what you need for a particular action. You may want +to use interceptors that are not part of the Struts 2 default stack. For an individual Action or for the entire +package of Actions, you can specify a different stack of interceptors that the Action or package should use. Below is +how you would specify that the register Action should use both the logging +and timer interceptors in addition to the interceptors provided by the default stack.

Specify Specific Interceptors For An Action

<action name="register" class="org.apache.struts.register.action.Register" method="execute">
     <interceptor-ref name="timer" />
-    <interceptor-ref name="logger" />
+    <interceptor-ref name="logging" />
     <interceptor-ref name="defaultStack">
         <param name="exception.logEnabled">true</param>
         <param name="exception.logLevel">ERROR</param>
@@ -186,19 +199,31 @@
 
-

The logger interceptor logs the start and end of the execution of an Action. The timer interceptor logs the amount of time (in milliseconds) for execution of the Action. These two interceptors used together can provide developers useful feedback.

+

The logger interceptor logs the start and end of the execution of an Action. The timer interceptor logs the amount +of time (in milliseconds) for execution of the Action. These two interceptors used together can provide developers +useful feedback.

-

In the code example above note the three interceptor-ref nodes. Each one has a value for the name attribute. For the register Action we are instructing the framework to use the timer, logger, and defaultStack interceptors. The defaultStack are all the interceptors normally executed for an Action.

+

In the code example above note the three interceptor-ref nodes. Each one has a value for the name attribute. For the register +Action we are instructing the framework to use the timer, logging, and defaultStack interceptors. The defaultStack +are all the interceptors normally executed for an Action.

-

How did I know to use the value of timer for the name attribute and even that there is a timer interceptor? On the Interceptors web page in the Struts 2 documentation are a list of interceptors that come with the Struts 2 framework and what the name value is for each interceptor.

+

How did I know to use the value of timer for the name attribute and even that there is a timer interceptor? +On the Interceptors web page in the Struts 2 documentation are a list of interceptors +that come with the Struts 2 framework and what the name value is for each interceptor.

-

How did I know that the timer interceptor isn’t part of the defaultStack of interceptors already? Again on the Interceptors documentation web page is a list of which interceptors belong to the defaultStack.

+

How did I know that the timer interceptor isn’t part of the defaultStack of interceptors already? Again on the Interceptors +documentation web page is a list of which interceptors belong to the defaultStack.

-

Note the param nodes. These nodes are used to provide a value to the setLogEnabled and setLogLevel methods of the Exception Interceptor . Providing the values of true and ERROR will cause the Struts 2 framework to log any exceptions not caught by the application’s code and to log those exceptions at the ERROR level.

+

Note the param nodes. These nodes are used to provide a value to the setLogEnabled and setLogLevel methods +of the Exception Interceptor. Providing the values of true and ERROR +will cause the Struts 2 framework to log any exceptions not caught by the application’s code and to log those exceptions +at the ERROR level.

-

Run The Example

+

Run The Example

-

In the example application follow the README instructions to build, deploy, and run the application. View the output sent to the JVM console to see the log messages generated by the logger and timer interceptors. You should see log messages similar to the following:

+

In the example application follow the README instructions to build, deploy, and run the application. View the output +sent to the JVM console to see the log messages generated by the logger and timer interceptors. You should see log messages +similar to the following:

INFO: Starting execution stack for action //register
 Nov 20, 2010 9:55:48 AM com.opensymphony.xwork2.util.logging.jdk.JdkLogger info
@@ -208,7 +233,8 @@ INFO: Executed action /register!execute took 177 ms.
 
-

If you wanted to have the logger and timer interceptors executed for all Actions in a package you would use the following in struts.xml:

+

If you wanted to have the logger and timer interceptors executed for all Actions in a package you would use +the following in struts.xml:

Specify Specific Interceptors For A Package

@@ -216,7 +242,7 @@ INFO: Executed action /register!execute took 177 ms. <interceptors> <interceptor-stack name="appDefault"> <interceptor-ref name="timer" /> - <interceptor-ref name="logger" /> + <interceptor-ref name="logging" /> <interceptor-ref name="defaultStack" /> </interceptor-stack> </interceptors> @@ -229,17 +255,26 @@ INFO: Executed action /register!execute took 177 ms. -

In the code above we use the interceptors node to define a new stack of interceptors that includes the timer, logger, and defaultStack interceptors. We give this new interceptor stack a name of appDefault. Then we use the default-interceptor-ref node to specify that for all Actions defined inside this package node the appDefault stack of interceptors are to be used. Thus the timer and logger interceptor will be executed for each Action in this package.

+

In the code above we use the interceptors node to define a new stack of interceptors that includes the timer, logging, +and defaultStack interceptors. We give this new interceptor stack a name of appDefault. Then we use the default-interceptor-ref +node to specify that for all Actions defined inside this package node the appDefault stack of interceptors are to be used. +Thus the timer and logging` interceptor will be executed for each Action in this package.

-

Note that in both examples we are still executing all the other interceptors by including the defaultStack as one of the interceptor-ref nodes. When you specify what interceptors you want to use for an Action or a package then only those interceptors are executed. So if in the example we had left out the interceptor-ref for defaultStack only the logger and timer interceptors would have executed.

+

Note that in both examples we are still executing all the other interceptors by including the defaultStack as one +of the interceptor-ref nodes. When you specify what interceptors you want to use for an Action or a package then only +those interceptors are executed. So if in the example we had left out the interceptor-ref for defaultStack only +the logging and timer interceptors would have executed.

-

Create Your Own Interceptor

+

Create Your Own Interceptor

-

In addition to specifying your own stack of interceptors, you can also write your own new interceptor and add it to the stack that is executed. The Struts Writing Interceptors guide explains how to do this. For example, you could create your own interceptor to handle authentication and authorization.

+

In addition to specifying your own stack of interceptors, you can also write your own new interceptor and add it to +the stack that is executed. The Struts Writing Interceptors guide explains +how to do this. For example, you could create your own interceptor to handle authentication and authorization.

-

Summary

+

Summary

-

Interceptors provide the Struts 2 framework with both power and flexibility. Developers may add additional interceptors (either ones provided by Struts 2 or ones they create) to the stack of interceptors executed when an Action class is called.

+

Interceptors provide the Struts 2 framework with both power and flexibility. Developers may add additional interceptors +(either ones provided by Struts 2 or ones they create) to the stack of interceptors executed when an Action class is called.

diff --git a/content/getting-started/jasper-reports-tutorial.html b/content/getting-started/jasper-reports-tutorial.html index 2c93fbc..2575daf 100644 --- a/content/getting-started/jasper-reports-tutorial.html +++ b/content/getting-started/jasper-reports-tutorial.html @@ -123,7 +123,18 @@ << back to Getting started -

JasperReports Tutorial

+

JasperReports Tutorial

+ +

The Struts 2 JasperReports plugin is a bridge from Struts 2 to JasperReports and does not include JasperReports itself, which must be downloaded separately.

diff --git a/content/getting-started/message-resource-files.html b/content/getting-started/message-resource-files.html index c391db4..1cc20ef 100644 --- a/content/getting-started/message-resource-files.html +++ b/content/getting-started/message-resource-files.html @@ -119,27 +119,49 @@
- << back to Getting Started Edit on GitHub -

Message Resource Files

+ + << back to Getting started + +

Message Resource Files

-

This tutorial assumes you’ve completed the Form Validation tutorial and have a working form_validation project. The example code for this tutorial, message_resource, is available for checkout from the

+ -

Struts 2 GitHub repository at https://github.com/apache/struts-examples.

+

This tutorial assumes you’ve completed the Form Validation tutorial and have a working +form-validation project. The example code for this tutorial, message-resource, is available for checkout from the +Struts 2 GitHub repository at struts-examples.

-

Introduction

+

Introduction

-

In this tutorial we’ll explore using Struts 2 message resource capabilities (also called resource bundles). Message resources provide a simple way to put text in a view page that is the same through out your application, to create form field labels, and to change text to a specific language based on the user’s locale (i18n).

+

In this tutorial we’ll explore using Struts 2 message resource capabilities (also called resource bundles). Message +resources provide a simple way to put text in a view page that is the same through out your application, to create form +field labels, and to change text to a specific language based on the user’s locale (i18n).

-

The Struts 2 user mailing list is an excellent place to get help. If you are having a problem getting the tutorial example applications to work search the Struts 2 mailing list. If you don’t find an answer to your problem, post a question on the mailing list.

+

The Struts 2 user mailing list is an excellent place to get help. If you are having +a problem getting the tutorial example applications to work search the Struts 2 mailing list. If you don’t find an answer +to your problem, post a question on the mailing list.

-

Message Resource Property Files

+

Message Resource Property Files

-

In a Struts 2 web application you may associate a message resource property file with each Struts 2 Action class by creating a properties file with the same name as the Action class and having the .properties extension. This properties file must go in the same package as the Action class. For our tutorial example, let’s say we want to place the form field labels into a separate file where we can easily change them and also provide the capability to display the labels in other languages.

+

In a Struts 2 web application you may associate a message resource property file with each Struts 2 Action class by creating +a properties file with the same name as the Action class and having the .properties extension. This properties file must +go in the same package as the Action class. For our tutorial example, let’s say we want to place the form field labels +into a separate file where we can easily change them and also provide the capability to display the labels in other languages.

-

If you’re doing this tutorial after completing Form Validation then you can make these changes to that tutorial’s example application.

+

If you’re doing this tutorial after completing Form Validation then you can make these changes +to that tutorial’s example application.

-

Put the text below in a file named Register.properties in the org.apache.struts.register.action package in the src/resources/java folder.

+

Put the text below in a file named Register.properties in the org.apache.struts.register.action package +in the src/resources/java folder.

Register.properties

@@ -151,11 +173,15 @@ thankyou=Thank you for registering %{personBean.firstName}. -

The above is just a standard Java properties file. The key is to the left of the = sign and the value for the key is to the right. When the Register action is executed these properties will be available to the view page by referencing the key name.

+

The above is just a standard Java properties file. The key is to the left of the = sign and the value for the key +is to the right. When the Register action is executed these properties will be available to the view page by referencing +the key name.

-

Struts 2 Key Attribute

+

Struts 2 Key Attribute

-

The Struts 2 key attribute can be used in the textfield tag to instruct the framework what value to use for the textfield’s name and label attributes. Instead of providing those attributes and their values directly, you can just use the key attribute.

+

The Struts 2 key attribute can be used in the textfield tag to instruct +the framework what value to use for the textfield’s name and label attributes. Instead of providing those attributes +and their values directly, you can just use the key attribute.

If you open register.jsp from the Form Validation tutorial you’ll see this Struts 2 textfield tag:

@@ -173,9 +199,14 @@ thankyou=Thank you for registering %{personBean.firstName}. -

The value for the key attribute instructs the Struts 2 framework to use the same value for the name attribute (personBean.firstName). For the label attribute’s value the value of the key attribute is used by the Struts 2 framework to find a key in a properties file with the same value. So in our example, Struts 2 will look in Register.properties for a key with a value of personBean.firstName. The value of that key (First name) will be used as the label attribute’s value.

+

The value for the key attribute instructs the Struts 2 framework to use the same value for the name attribute +(personBean.firstName). For the label attribute’s value the value of the key attribute is used by the Struts 2 framework +to find a key in a properties file with the same value. So in our example, Struts 2 will look in Register.properties +for a key with a value of personBean.firstName. The value of that key (First name) will be used as the label attribute’s value.

-

To enable the key attribute to find the properties file, the display of the view page must be the result of executing a Struts 2 Action class. Right now if you examine index.jsp from the Form Validation tutorial the link to the register.jsp page is a standard URL.

+

To enable the key attribute to find the properties file, the display of the view page must be the result of executing +a Struts 2 Action class. Right now if you examine index.jsp from the Form Validation tutorial +the link to the register.jsp page is a standard URL.

link to register.jsp

@@ -183,7 +214,8 @@ thankyou=Thank you for registering %{personBean.firstName}. -

We need to change the above link so that it goes through the Register.java Struts 2 Action class. Replace the above with this markup.

+

We need to change the above link so that it goes through the Register.java Struts 2 Action class. Replace the above with +this markup.

link to Register Action class

@@ -192,7 +224,8 @@ thankyou=Thank you for registering %{personBean.firstName}. -

We use the Struts 2 url tag to create a link to action registerInput. We then use that link as the value for the href attribute of the anchor tag. We must define the registerInput action in struts.xml. Add the following to struts.xml.

+

We use the Struts 2 url tag to create a link to action registerInput. We then use that link as the value for the href +attribute of the anchor tag. We must define the registerInput action in struts.xml. Add the following to struts.xml

registerInput action node for struts.xml

@@ -202,21 +235,30 @@ thankyou=Thank you for registering %{personBean.firstName}. -

The above action node instructs the Struts 2 framework to execute class Register’s input method in response to action registerInput. The input method is inherited by class Register from class ActionSupport. The default behavior of the inherited input method is to return the String input. The result node above specifies that if the returned result is “input” then render the view register.jsp.

+

The above action node instructs the Struts 2 framework to execute class Register’s input method in response to action +registerInput. The input method is inherited by class Register from class ActionSupport. The default behavior of +the inherited input method is to return the String input. The result node above specifies that if the returned result +is input then render the view register.jsp.

-

By doing the above the view page register.jsp will have access to the properties defined in Register.properties. The Struts 2 framework will make those properties defined in Register.properties available to the view page since the view page was rendered after Register.java (the Struts 2 Action class) was executed.

+

By doing the above the view page register.jsp will have access to the properties defined in Register.properties. +The Struts 2 framework will make those properties defined in Register.properties available to the view page since +the view page was rendered after Register.java (the Struts 2 Action class) was executed.

-

Follow the instructions (README.txt) in the project to create the war file and copy the war file to your servlet container. Open a web browser and navigate to the home page specified in the README.txt file (index.action). You should see a link to registerInput.action when mousing over the hyperlink Please Register.

+

Follow the instructions (README.txt) in the project to create the war file and copy the war file to your servlet container. +Open a web browser and navigate to the home page specified in the README.txt file (index.action). You should see a link +to registerInput.action when mousing over the hyperlink Please Register.

registerInput.png

-

When you click on the Please Register link your browser should display the register.jsp. The form field labels should be the key values from the Register.properties file.

+

When you click on the Please Register link your browser should display the register.jsp. The form field labels should +be the key values from the Register.properties file.

register.png

-

Struts 2 Text Tag

+

Struts 2 Text Tag

-

We can also use the Struts 2 text tag to display values from a properties file. In thankyou.jsp add this text tag instead of the h3 tag that is in thankyou.jsp.

+

We can also use the Struts 2 text tag to display values from a properties file. In thankyou.jsp add this text tag +instead of the h3 tag that is in thankyou.jsp.

text tag

@@ -224,11 +266,13 @@ thankyou=Thank you for registering %{personBean.firstName}. -

Since thankyou.jsp is also rendered after executing the Register.java Action class, the key thankyou and its value will be available to the view page.

+

Since thankyou.jsp is also rendered after executing the Register.java Action class, the key thankyou and its value +will be available to the view page.

thankyou.png

-

How did the value entered for the first name input field get displayed on thankyou.jsp? Look back at the value for the thankyou key in the Register.properties file.

+

How did the value entered for the first name input field get displayed on thankyou.jsp? Look back at the value +for the thankyou key in the Register.properties file.

Register.properties

@@ -236,13 +280,17 @@ thankyou=Thank you for registering %{personBean.firstName}. -

The markup %{personBean.firstName} tells Struts 2 to replace this part with the result of calling getPersonBean, which returns a Person object. Then call the getFirstName method which returns a String (the value the user inputted into the personBean.firstName form field on register.jsp).

+

The markup %{personBean.firstName} tells Struts 2 to replace this part with the result of calling getPersonBean, +which returns a Person object. Then call the getFirstName method which returns a String (the value the user inputted +into the personBean.firstName form field on register.jsp).

-

Package Level Properties

+

Package Level Properties

-

What if you want a properties file with keys and values that can be referenced from multiple view pages and those view pages are rendered after executing different Action classes? Struts 2 has the ability to use multiple property files provided the property file is found in the package hierarchy.

+

What if you want a properties file with keys and values that can be referenced from multiple view pages and those view +pages are rendered after executing different Action classes? Struts 2 has the ability to use multiple property files +provided the property file is found in the package hierarchy.

-

Place the following in a file named package.properties and save that file in package org.apache.struts in src/main/resources.

+

Place the following in a file named package.properties and save that file in package org.apache.struts in src/main/resources.

package.properties

@@ -250,7 +298,9 @@ thankyou=Thank you for registering %{personBean.firstName}. -

Now any view rendered by an Action that is in the hierarchy org.apache.struts… can use a Struts 2 text tag with a name attribute value of “greeting” to display the value of the greeting property key. For example add the following markup to helloworld.jsp before the h2 tag.

+

Now any view rendered by an Action that is in the hierarchy org.apache.struts... can use a Struts 2 text tag with +a name attribute value of greeting to display the value of the greeting property key. For example add the following +markup to helloworld.jsp before the h2 tag.

Using properties set in package.properties

@@ -258,15 +308,18 @@ thankyou=Thank you for registering %{personBean.firstName}. -

Then rebuild the war file and deploy it to your servlet container. Go to index.action and click on the link for Hello World. You should see:

+

Then rebuild the war file and deploy it to your servlet container. Go to index.action and click on the link for Hello World. +You should see:

hellogreeting.png

-

The property keys and values defined in package.properties are available to any view that is rendered after executing an Action class that is the package hierarchy that includes package.properties.

+

The property keys and values defined in package.properties are available to any view that is rendered after executing +an Action class that is the package hierarchy that includes package.properties.

-

Global Properties

+

Global Properties

-

You can also specify a global property file in struts.xml. The keys and values defined in that property file will be available to all the view pages that are rendered after executing an Action class.

+

You can also specify a global property file in struts.xml. The keys and values defined in that property file will be +available to all the view pages that are rendered after executing an Action class.

Add the following to a file named global.properties (note the name doesn’t have to be global).

@@ -276,9 +329,10 @@ thankyou=Thank you for registering %{personBean.firstName}. -

Save the global.properties file in the src/main/resources folder.

+

Save the global.properties file in the src/main/resources folder.

-

To inform the Struts 2 framework about the global.properties file add the follow node to struts.xml after the constant name=”struts.devmode” node.

+

To inform the Struts 2 framework about the global.properties file add the follow node to struts.xml after the constant +name="struts.devmode" node.

Specify Global Property File In struts.xml

@@ -286,7 +340,7 @@ thankyou=Thank you for registering %{personBean.firstName}. -

To use the contact key in a view page, add the following markup to index.jsp just before the closing body tag.

+

To use the contact key in a view page, add the following markup to index.jsp just before the closing body tag.

Using contact property

@@ -295,19 +349,26 @@ thankyou=Thank you for registering %{personBean.firstName}. -

Rebuild the war file, deploy it to your Servlet container, and then go to index.action. You should see:

+

Rebuild the war file, deploy it to your Servlet container, and then go to index.action. You should see:

contact.png

-

Struts 2 will look for a property key of contact in all the property files starting with the property file that matches the Action class, then in the property files that are in the package hierarchy of the Action class, and then in any property files specified in struts.xml. For this example Struts 2 will find the contact key in global.properties. The value of the contact key will be displayed where we have put the text tag.

+

Struts 2 will look for a property key of contact in all the property files starting with the property file that matches +the Action class, then in the property files that are in the package hierarchy of the Action class, and then in any property +files specified in struts.xml. For this example Struts 2 will find the contact key in global.properties. The value +of the contact key will be displayed where we have put the text tag.

You can add the text tag above to all the JSPs in this example.

-

Internationalization (i18n)

+

Internationalization (i18n)

-

Using message resource files (resource bundles) also enables you to provide text in different languages. By default, Struts 2 will use the user’s default locale. If that locale is en for English then the property files used will be the ones without a locale specification (for example Register.properties). If the locale is not English but say Spanish (es) then Struts 2 will look for a properties file named Register_ [...] +

Using message resource files (resource bundles) also enables you to provide text in different languages. By default, +Struts 2 will use the user’s default locale. If that locale is en for English then the property files used will be +the ones without a locale specification (for example Register.properties). If the locale is not English but +say Spanish (es) then Struts 2 will look for a properties file named Register_es.properties.

-

To provide an example of Struts 2 support for i18n create a file named Register_es.properties and in that file add the following Spanish translations.

+

To provide an example of Struts 2 support for i18n create a file named Register_es.properties and in that file add +the following Spanish translations.

Register_es.properties

@@ -319,17 +380,14 @@ thankyou=Gracias por registrarse, %{personBean.firstName}.
-
- - - - - -
My apologies to Spanish language speakers for any mistakes in the Spanish translations.
+
+

My apologies to Spanish language speakers for any mistakes in the Spanish translations.

+

Save the Register_es.properties file in the same package as Register.properties.

-

In our example application, we need to tell Struts 2 to use a locale value of es (since we’re not in a Spanish locale) instead of the default locale value of our location (which is en). Add the following markup to index.jsp.

+

In our example application, we need to tell Struts 2 to use a locale value of es (since we’re not in a Spanish locale) +instead of the default locale value of our location (which is en). Add the following markup to index.jsp.

Specify The Locale As a URL Parameter

@@ -341,17 +399,25 @@ thankyou=Gracias por registrarse, %{personBean.firstName}. -

In the above markup we’ve added a parameter named request_locale to the URL. The value of that parameter is es. The Action class that responds to this URL (Register.java) will see that the locale is es and will look for property files with _es (for example Register_es.properties). It will use those property files to find the values of the property keys referenced by the view page (e.g. personBean.firstName).

+

In the above markup we’ve added a parameter named request_locale to the URL. The value of that parameter is es. +The Action class that responds to this URL (Register.java) will see that the locale is es and will look for property +files with _es (for example Register_es.properties). It will use those property files to find the values of the property +keys referenced by the view page (e.g. personBean.firstName).

After clicking on the above link you should see the same form as before but with the form field labels in Spanish.

spanishform.png

-

If we implement the same concept by creating _es.properties versions of global.properties (global_es.properties) and package.properties (package_es.properties) then we can create a complete registration web page in Spanish. Download the finished example application for this tutorial from Google Code - global.properties (global_es.properties) +and package.properties (package_es.properties) then we can create a complete registration web page in Spanish. +Download the finished example application for this tutorial from Github - message-resource +to see those property files and run the complete example with the registration form in Spanish.

-

Summary

+

Summary

-

We’ve covered how to use message resources (resource bundles) in Struts 2 and also introduced how Struts 2 enables internationalization (i18n) in this tutorial. To fully understand these concepts and learn more about Struts 2 consult the main Struts 2 documentation available at http://struts.apache.org.

+

We’ve covered how to use message resources (resource bundles) in Struts 2 and also introduced how Struts 2 enables +internationalization (i18n) in this tutorial. To fully understand these concepts and learn more about Struts 2 consult +the main Struts 2 documentation available at http://struts.apache.org.

diff --git a/content/getting-started/preperable-interface.html b/content/getting-started/preperable-interface.html index 8884ee3..9719c3e 100644 --- a/content/getting-started/preperable-interface.html +++ b/content/getting-started/preperable-interface.html @@ -119,31 +119,58 @@
- << back to Getting Started Edit on GitHub -

Preparable Interface

+ + << back to Getting started + +

Preparable Interface

-

The example code for this tutorial, preparable_interface, is available at https://github.com/apache/struts-examples.

+ -

Introduction

+

The example code for this tutorial, preparable-interface, is available at struts-examples.

-

Often the data used to populate a form control is dynamically generated, perhaps from a database. When the user submits the form, the Struts 2 validation interceptor attempts to validate the user’s form input. If validation fails the Struts 2 framework returns the value “input” but the “input” action is not re-executed. Rather the view associated with the “input” result is rendered to the user. Usually this view is the page that displayed the original form.

+

Introduction

-

This work-flow can cause a problem if one or more of the form fields or some other data displayed depends on a dynamic look-up that that is accomplished in the Action class’s input method. Since the Action class’s input method is not re-executed when validation fails, the view page may no longer have access to the correct information to create the form or other display information.

+

Often the data used to populate a form control is dynamically generated, perhaps from a database. When the user submits +the form, the Struts 2 validation interceptor attempts to validate the user’s form input. If validation fails the Struts 2 +framework returns the value input but the input action is not re-executed. Rather the view associated with the input +result is rendered to the user. Usually this view is the page that displayed the original form.

-

The Struts 2 user mailing list is an excellent place to get help. If you are having a problem getting the tutorial example applications to work search the Struts 2 mailing list. If you don’t find an answer to your problem, post a question on the mailing list.

+

This work-flow can cause a problem if one or more of the form fields or some other data displayed depends on a dynamic +look-up that that is accomplished in the Action class’s input method. Since the Action class’s input method is not +re-executed when validation fails, the view page may no longer have access to the correct information to create the form +or other display information.

-

Preparable Interface

+

The Struts 2 user mailing list is an excellent place to get help. If you are having +a problem getting the tutorial example applications to work search the Struts 2 mailing list. If you don’t find an answer +to your problem, post a question on the mailing list.

-

Struts 2 provides the Preparable interface^[http://struts.apache.org/2.3.1/xwork-core/apidocs/com/opensymphony/xwork2/Preparable.html] to overcome this problem. An Action class that implements this interface must override the prepare method. The prepare method will always be called by the Struts 2 framework’s prepare interc [...] +

Preparable Interface

-

In the prepare method you should put any statements that must be executed no matter what other Action class method will be called and also statements that should be executed if validation fails. Usually statements in the prepare method set the value for Action class instance fields that will be used to populate form controls and get the values that will be used to set the initial form field values.

+

Struts 2 provides the Preparable interface +to overcome this problem. An Action class that implements this interface must override the prepare method. The prepare +method will always be called by the Struts 2 framework’s prepare interceptor +whenever any method is called for the Action class and also when validation fails before the view is rendered.

-

In addition to automatically running the prepare method the prepare interceptor will also call a method named prepare[ActionMethodName]. For example, define a prepare method and a prepareInput method in the Action class that implements preparable. When the Struts 2 framework calls the input method, the prepare interceptor will call the prepareInput and the prepare methods before calling the input method.

+

In the prepare method you should put any statements that must be executed no matter what other Action class method will +be called and also statements that should be executed if validation fails. Usually statements in the prepare method set +the value for Action class instance fields that will be used to populate form controls and get the values that will be +used to set the initial form field values.

-

Example Application

+

In addition to automatically running the prepare method the prepare interceptor +will also call a method named prepare<ActionMethodName>. For example, define a prepare method and a prepareInput method +in the Action class that implements Preparable. When the Struts 2 framework calls the input method, the prepare interceptor +will call the prepareInput and the prepare methods before calling the input method.

-

If you examine class EditAction in the example application (see above) you’ll see that it implements the Preparable Interface. In the prepare method is this code:

+

Example Application

+ +

If you examine class EditAction in the example application (see above) you’ll see that it implements the Preparable interface. +In the prepare method is this code:

EditAction.java prepare Method

@@ -153,13 +180,17 @@ -

The above statements get the car model values used to populate the car model check boxes displayed in the form and also get the information about the Person object being edited.

+

The above statements get the car model values used to populate the car model check boxes displayed in the form and also +get the information about the Person object being edited.

-

When you run the example application, look in the log to see when the prepare method is called in relation to the input and execute methods. Running the example application and examining the log should help you understand the impact of implementing the Preparable Interface and the prepare method.

+

When you run the example application, look in the log to see when the prepare method is called in relation to the input +and execute methods. Running the example application and examining the log should help you understand the impact of implementing +the Preparable interface and the prepare method.

-

Summary

+

Summary

-

When your application requires specific statements to be executed no matter which method of the Action class is called or when validation fails, you should implement the Preparable interface and override the prepare method.

+

When your application requires specific statements to be executed no matter which method of the Action class is called +or when validation fails, you should implement the Preparable interface and override the prepare method.

diff --git a/content/getting-started/processing-forms.html b/content/getting-started/processing-forms.html index 49d363b..18d875c 100644 --- a/content/getting-started/processing-forms.html +++ b/content/getting-started/processing-forms.html @@ -119,13 +119,25 @@
- << back to Getting Started Edit on GitHub -

Processing Forms

+ + << back to Getting started + +

Processing Forms

+ +

This tutorial assumes you’ve completed the Coding Struts 2 Actions tutorial and have a working -coding-actions project. The example code for this tutorial, form-processing, is available for checkout from -the Struts 2 GitHub subversion repository: https://github.com/apache/struts-examples.

+coding-actions project. The example code for this tutorial, form-processing, is available for checkout from +the Struts 2 GitHub repository struts-examples.

Introduction

diff --git a/content/getting-started/spring.html b/content/getting-started/spring.html index e46d9f2..3a50eb4 100644 --- a/content/getting-started/spring.html +++ b/content/getting-started/spring.html @@ -119,30 +119,41 @@
- << back to Getting Started Edit on GitHub -

Spring and Struts 2

+ + << back to Getting started + +

Spring and Struts 2

-

The example code for this tutorial, spring_struts, is available for checkout at https://github.com/apache/struts-examples

+ -

Introduction

+

The example code for this tutorial, spring-struts, is available for checkout at struts-examples

-

In the execute method of many Struts 2 ActionSupport classes are statements that create objects and then have those objects execute methods that perform needed tasks. Whenever one class creates an object of another class that introduces a dependency between the two classes. The Spring framework makes it easier for the application developer to manage these dependencies and helps make the application more flexible and maintainable. This tutorial will show you how to use Struts 2 and Spr [...] +

Introduction

+ +

In the execute method of many Struts 2 ActionSupport classes are statements that create objects and then have those +objects execute methods that perform needed tasks. Whenever one class creates an object of another class that introduces +a dependency between the two classes. The Spring framework makes it easier for the application developer to manage these +dependencies and helps make the application more flexible and maintainable. This tutorial will show you how to use Struts 2 +and Spring together to manage the dependencies between your ActionSupport classes and other classes in your application.

-

This tutorial assumes you understand how to use the Spring framework to manage dependencies between classes. You can learn more about Spring by reading the documentation at http://www.springsource.org/documentation

+

This tutorial assumes you understand how to use the Spring framework to manage dependencies between classes. You can +learn more about Spring by reading the documentation at https://spring.io/docs

-

The Struts 2 user mailing list is an excellent place to get help. If you are having a problem getting the tutorial example applications to work search the Struts 2 mailing list. If you don’t find an answer to your problem, post a question on the mailing list.

+

The Struts 2 user mailing list is an excellent place to get help. If you are having +a problem getting the tutorial example applications to work search the Struts 2 mailing list. If you don’t find an answer +to your problem, post a question on the mailing list.

-
- - - - -
- -

If you examine the example application for the Struts 2 Themes tutorial you’ll see this code in the EditAction ActionSupport class

+

If you examine the example application for the Struts 2 Themes tutorial you’ll see this code in the +EditAction class

EditAction Class Hard-Coded Dependency

@@ -150,18 +161,19 @@ -

The above statement hard-codes a dependency between the EditAction class and the EditServiceInMemory class. This is poor design for two reasons.

+

The above statement hard-codes a dependency between the EditAction class and the EditServiceInMemory class. This is +poor design for two reasons.

    -
  1. -

    If I need to replace the EditServiceInMemory with another class that implements the EditService interface I’ll have to hunt down and replace all statements where I hard-coded the dependency.

    -
  2. -
  3. -

    I cannot test EditAction without using the EditServiceInMemory class. I cannot isolate EditAction by using a stub implementation of EditService when writing my test case because the use of EditServiceInMemory is hard-coded.

    -
  4. +
  5. If I need to replace the EditServiceInMemory with another class that implements the EditService interface I’ll + have to hunt down and replace all statements where I hard-coded the dependency.
  6. +
  7. I cannot test EditAction without using the EditServiceInMemory class. I cannot isolate EditAction by using + a stub implementation of EditService when writing my test case because the use of EditServiceInMemory is hard-coded.
-

Spring provides a mechanism to manage dependencies by injecting them at run time. Struts 2 ActionSupport classes—like any other Java class—can be injected with a dependent object by the Spring framework. So instead of having the above code, I would have this statement in EditAction.

+

Spring provides a mechanism to manage dependencies by injecting them at run time. Struts 2 ActionSupport classes—like +any other Java class—can be injected with a dependent object by the Spring framework. So instead of having the above code, +I would have this statement in EditAction.

EditAction Class No Hard-Coded Dependency

@@ -171,21 +183,25 @@

At run time the Spring framework will provide an object of a class that implements the EditService interface.

-

Struts 2 Spring Plugin

+

Struts 2 Spring Plugin

-

Struts 2 provides a plugin that enables Spring to inject into the ActionSupport classes any dependent objects you’ve specified in the Spring configuration file. Consult Spring Plugin documentation for more information about how the plugin works.

+

Struts 2 provides a plugin that enables Spring to inject into the ActionSupport classes any dependent objects you’ve +specified in the Spring configuration file. Consult Spring Plugin documentation for more information +about how the plugin works.

-

For a Maven application you’ll need to add a dependency to the struts2-spring-plugin jar (see the Maven example application for this tutorial). The plugin’s pom.xml includes transitive dependencies to the Spring jar files.

+

For a Maven application you’ll need to add a dependency to the struts2-spring-plugin jar (see the Maven example application +for this tutorial). The plugin’s pom.xml includes transitive dependencies to the Spring jar files.

- - - - - -
The current version (2.5.10.1) of the Struts 2 Spring plugin has transitive dependencies to the Spring 4.1.6.RELEASE version. If you want to use the latest version of Spring, then you should exclude the transitive dependencies in your pom.xml for the Struts 2 Spring plugin and then declare dependency nodes to the current version of the Spring jar files. If you are using Ant and explicitly including th [...] -
+
+

The current version (2.5.10.1) of the Struts 2 Spring plugin has transitive dependencies to the Spring 4.1.6.RELEASE version. +If you want to use the latest version of Spring, then you should exclude the transitive dependencies in your pom.xml +for the Struts 2 Spring plugin and then declare dependency nodes to the current version of the Spring jar files. +If you are using Ant and explicitly including the jar files in your application, then just include the latest version +of the Spring jar files.

+
-

In your ActionSupport class you must have a set method for the dependent object that follows standard Java bean naming conventions. If you examine the EditAction class for this tutorial’s application you’ll see this set method.

+

In your ActionSupport class you must have a set method for the dependent object that follows standard Java bean naming +conventions. If you examine the EditAction class for this tutorial’s application you’ll see this set method.

public void setEditService(EditService editService) {
     this.editService = editService;
@@ -205,11 +221,14 @@
 
-

The above code will activate the Spring framework when the application is started up by the Servlet container. By default Spring will look for a configuration file name applicationContext.xml in WEB-INF (consult the Spring documentation for how you can change where Spring looks and what the configuration file name is).

+

The above code will activate the Spring framework when the application is started up by the Servlet container. By default +Spring will look for a configuration file name applicationContext.xml in WEB-INF (consult the Spring documentation for +how you can change where Spring looks and what the configuration file name is).

-

Spring Configuration File

+

Spring Configuration File

-

In the Spring configuration file we create a bean node for those objects we want Spring to create an instance of and inject into our ActionSupport class. In the example application is this applicationContext.xml.

+

In the Spring configuration file we create a bean node for those objects we want Spring to create an instance of and inject +into our ActionSupport class. In the example application is this applicationContext.xml.

Spring Configuration File

@@ -226,13 +245,20 @@ -

Note the id value above. By default the Spring plugin works with Spring to autowire the dependencies of the ActionClass by “name.” Spring will create an object of class EditServiceMemory and provide that object to any ActionSupport class that has a setEditService method with an argument of type EditService. Consult the Spring Plugin documentation for how to change the default autowire method.

+

Note the id value above. By default the Spring plugin works with Spring to autowire the dependencies of the ActionClass +by name. Spring will create an object of class EditServiceMemory and provide that object to any ActionSupport class +that has a setEditService method with an argument of type EditService. Consult the Spring Plugin +documentation for how to change the default autowire method.

-

The editService bean created by Spring will have a scope of singleton since that is the default scope. Consult section 3.5 of the Spring documentation for how to configure the bean definition to use a different scope (e.g. request or session).

+

The editService bean created by Spring will have a scope of singleton since that is the default scope. Consult section +3.5 of the Spring documentation for how to configure the bean definition to use a different +scope (e.g. request or session).

-

Alternative - Have Spring Manage Creation Of ActionSupport Class

+

Alternative - Have Spring Manage Creation Of ActionSupport Class

-

Using the above methodology, the Struts 2 framework will still manage the creation of the ActionSupport class. If you prefer you can configure the application so that Spring will create the ActionSupport class also. To support this technique you need to add a bean node to the Spring configuration file for the ActionSupport class.

+

Using the above methodology, the Struts 2 framework will still manage the creation of the ActionSupport class. If you +prefer you can configure the application so that Spring will create the ActionSupport class also. To support this technique +you need to add a bean node to the Spring configuration file for the ActionSupport class.

Spring Configuration For ActionSupport Class Managed By Spring

@@ -253,9 +279,13 @@ -

Note in the above that there is an editAction bean and its editService property is set to the editService bean. Since we are having Spring manage the EditAction class we must specify any properties of EditAction that we want Spring to inject. Please remember that actions must be created on eac [...] +

Note in the above that there is an editAction bean and its editService property is set to the editService bean. +Since we are having Spring manage the EditAction class we must specify any properties of EditAction that we want Spring +to inject. Please remember that actions must be created on each request, they cannot be singletons- this is the default +scope that’s why it must be changed to prototype.

-

In the struts.xml configuration file you must specify the Spring id value for the class attribute of the action node. This tells Struts to get a bean with that id value from Spring for the Action class.

+

In the struts.xml configuration file you must specify the Spring id value for the class attribute of the action node. +This tells Struts to get a bean with that id value from Spring for the Action class.

Struts Configuration For Spring Managed ActionSupport Class

@@ -265,9 +295,11 @@ -

Summary

+

Summary

-

In this tutorial we reviewed how to use the Struts 2 Spring plugin to integrate Spring and Struts. By using the Struts 2 Spring plugin you can have Spring manage the dependencies of your ActionSupport classes. Of course you can also take advantage of the many other benefits (AOP, Spring JDBC) that the Spring framework provides.

+

In this tutorial we reviewed how to use the Struts 2 Spring plugin to integrate Spring and Struts. By using the Struts 2 +Spring plugin you can have Spring manage the dependencies of your ActionSupport classes. Of course you can also take +advantage of the many other benefits (AOP, Spring JDBC) that the Spring framework provides.

diff --git a/content/getting-started/themes.html b/content/getting-started/themes.html index 4f3397f..f6e2296 100644 --- a/content/getting-started/themes.html +++ b/content/getting-started/themes.html @@ -119,17 +119,32 @@
- << back to Getting Started Edit on GitHub -

Themes

+ + << back to Getting started + +

Themes

-

The example code for this tutorial, themes, is available for checkout at https://github.com/apache/struts-examples

+ -

Introduction

+

The example code for this tutorial, themes, is available for checkout at struts-examples

-

When you use a Struts 2 tag such as s:select in your web page, the Struts 2 framework generates HTML that styles the appearance and controls the layout of the select control. The style and layout is determined by which Struts 2 theme is set for the tag. Struts 2 comes with three built-in themes: simple, xhtml, and css_xhtml. If you don’t specify a theme, then Struts 2 will use the xhtml theme by default.

+

Introduction

-

The Struts 2 user mailing list is an excellent place to get help. If you are having a problem getting the tutorial example applications to work search the Struts 2 mailing list. If you don’t find an answer to your problem, post a question on the mailing list.

+

When you use a Struts 2 tag such as s:select in your web page, the Struts 2 framework generates HTML that styles +the appearance and controls the layout of the select control. The style and layout is determined by which Struts 2 theme +is set for the tag. Struts 2 comes with three built-in themes: simple, xhtml, and css_xhtml. If you don’t specify +a theme, then Struts 2 will use the xhtml theme by default.

+ +

The Struts 2 user mailing list is an excellent place to get help. If you are having +a problem getting the tutorial example applications to work search the Struts 2 mailing list. If you don’t find an answer +to your problem, post a question on the mailing list.

For example, this Struts 2 select tag:

@@ -158,51 +173,53 @@ -

Notice how the HTML generated uses table tags to control the layout of the label and select HTML. There is also a class, tdLabel, applied to the table column where the label tag is rendered. Since no theme was specified for the Struts 2 select tag the default xhmtl theme was used.

+

Notice how the HTML generated uses table tags to control the layout of the label and select HTML. There is also a class, +tdLabel, applied to the table column where the label tag is rendered. Since no theme was specified for the Struts 2 s +elect tag the default xhmtl theme was used.

-

Specifying The Theme Struts 2 Should Use

+

Specifying The Theme Struts 2 Should Use

-

The Struts 2 tags have a theme attribute you can use to specify which Struts 2 theme should be used when creating the HTML for that tag. The values for the theme attribute are simple, xhtml, css_xhtml, and ajax. To learn more about these themes visit Themes and Templates Documentation . This tutorial will review the xhtml, css_xhtml, and the simple themes. The ajax theme is a special theme used for ajax operations (see < [...] +

The Struts 2 tags have a theme attribute you can use to specify which Struts 2 theme should be used when creating the HTML +for that tag. The values for the theme attribute are simple, xhtml, css_xhtml, and ajax. To learn more about these themes +visit Themes and Templates Documentation. This tutorial will review +the xhtml, css_xhtml, and the simple themes. The ajax theme is a special theme used for ajax operations +(see Ajax Theme in the documentation).

-

You can specify the theme on a per Struts 2 tag basis or you can use one of the following methods to specify what theme Struts 2 should use:

+

You can specify the theme on a per Struts 2 tag basis or you can use one of the following methods to specify what theme +Struts 2 should use:

    -
  1. -

    The theme attribute on the specific tag

    -
  2. -
  3. -

    The theme attribute on a tag’s surrounding form tag

    -
  4. -
  5. -

    The page-scoped attribute named “theme”

    -
  6. -
  7. -

    The request-scoped attribute named “theme”

    -
  8. -
  9. -

    The session-scoped attribute named “theme”

    -
  10. -
  11. -

    The application-scoped attribute named “theme”

    -
  12. -
  13. -

    The struts.ui.theme property in struts.properties (defaults to xhtml)

    -
  14. +
  15. The theme attribute on the specific tag
  16. +
  17. The theme attribute on a tag’s surrounding form tag
  18. +
  19. The page-scoped attribute named theme
  20. +
  21. The request-scoped attribute named theme
  22. +
  23. The session-scoped attribute named theme
  24. +
  25. The application-scoped attribute named theme
  26. +
  27. The struts.ui.theme constant in struts.xml (defaults to xhtml)
-

Consult Selecting Themes for how to set the theme using the above approaches.

+

Consult Selecting Themes for how to set the theme using the above approaches.

-

In the example application, examine edit.jsp. The theme attribute of the form tag is set to xhtml. Run the application (see the readme.txt file) and view the source for edit.jsp after it is rendered in your browser. You should see the form HTML tags layout controlled by table tags.

+

In the example application, examine edit.jsp. The theme attribute of the form tag is set to xhtml. Run the application +(see the readme.txt file) and view the source for edit.jsp after it is rendered in your browser. You should see the form +HTML tags layout controlled by table tags.

-

Change the theme to simple for the form’s theme attribute and redeploy the application. Examine the source for edit.jsp after it is rendered in the browser. You should see that there are no table tags controlling the layout and also there are no label tags for the text fields.

+

Change the theme to simple for the form’s theme attribute and redeploy the application. Examine the source for edit.jsp +after it is rendered in the browser. You should see that there are no table tags controlling the layout and also there +are no label tags for the text fields.

-

Change the theme to css_xhtml for the form’s theme attribute and redeploy the application. Examine the source for edit.jsp after it is rendered in the browser. The layout of the form tags is now controlled by div tags and the label tags are back.

+

Change the theme to css_xhtml for the form’s theme attribute and redeploy the application. Examine the source for edit.jsp +after it is rendered in the browser. The layout of the form tags is now controlled by div tags and the label tags are back.

-

Specifying The CSS Used By The Struts 2 Tag

+

Specifying The CSS Used By The Struts 2 Tag

-

Change the theme attribute for the form tag back to xhtml. Notice when you view the source of edit.jsp after it is rendered in the browser that there is a class named tdLabel applied to the table column that contains the label. This CSS class controls the position of the label in the table column. The tdLabel style is defined in /themes/struts/xhtml/styles.css. The link to this style sheet was included in edit.jsp’s head section when you add the s:head< [...] +

Change the theme attribute for the form tag back to xhtml. Notice when you view the source of edit.jsp after it is rendered +in the browser that there is a class named tdLabel applied to the table column that contains the label. This CSS class +controls the position of the label in the table column. The tdLabel style is defined in /themes/struts/xhtml/styles.css. +The link to this style sheet was included in edit.jsp’s head section when you add the s:head tag to edit.jsp.

-

Load this style sheet in your browser (in the example application the link is http://localhost:8080/themes/struts/xhtml/styles.css if your Servlet container is running on localhost, port 8080). You’ll see the following:

+

Load this style sheet in your browser (in the example application the link is http://localhost:8080/themes/struts/xhtml/styles.css +if your Servlet container is running on localhost, port 8080). You’ll see the following:

styles.css

@@ -216,9 +233,11 @@
-

So the .label selector renders the label tag’s text in italic. The .tdLabel tag specifies that the text should align to the right and top of the table column.

+

So the .label selector renders the label tag’s text in italic. The .tdLabel tag specifies that the text should align +to the right and top of the table column.

-

You can override the above selectors by including the same selectors in your page’s head section. For example add the following to the head section of edit.jsp.

+

You can override the above selectors by including the same selectors in your page’s head section. For example add +the following to the head section of edit.jsp.

Override Label Style

@@ -230,19 +249,31 @@

Now the label tag will render the text in blue, bold, normal (not italics) style.

-

Creating And Applying Your Own Themes For Struts 2 Tags

+

Creating And Applying Your Own Themes For Struts 2 Tags

-

The theme templates (simple, xhtml, css_xhtml) can be found in the Struts 2 core jar file. If you expand (extract the files) the Struts 2 core jar file you’ll find folders named template.css_xhtml, template.simple, and template.xhtml. Those folders contain the templates for the three default Struts 2 themes. In each folder is a file for each Struts 2 tag. For example if you expand the template.xhtml folder you’ll see the select.ftl file.

+

The theme templates (simple, xhtml, css_xhtml) can be found in the Struts 2 core jar file. If you expand (extract the files) +the Struts 2 core jar file you’ll find folders named template.css_xhtml, template.simple, and template.xhtml. +Those folders contain the templates for the three default Struts 2 themes. In each folder is a file for each Struts 2 tag. +For example if you expand the template.xhtml folder you’ll see the select.ftl file.

-

The Struts 2 framework uses the FreeMarker template engine to generate the HTML for the Struts 2 tags. That’s why the file extension is “.ftl”. You can learn more about FreeMarker by visiting http://freemarker.sourceforge.net/.

+

The Struts 2 framework uses the FreeMarker template engine to generate the HTML for the Struts 2 tags. That’s why the file +extension is .ftl. You can learn more about FreeMarker by visiting https://freemarker.apache.org/.

-

Also in the template.xhmtl folder is the styles.css file. This is the styles.css file that your application will use if it uses the xhtml theme.

+

Also in the template.xhmtl folder is the styles.css file. This is the styles.css file that your application will use +if it uses the xhtml theme.

-

Let’s say we wanted to create our own theme that will change how the Struts 2 checkboxlist tag displays the checkboxes and their labels.

+

Let’s say we wanted to create our own theme that will change how the Struts 2 checkboxlist tag displays the checkboxes +and their labels.

-

In the example application I’ve extended the default XHMTL theme (see file theme.properties under src/main/resources/template/KUTheme).  The checkboxlist.ftl theme that is part of the XHTML theme only includes a space between each label and the next checkbox (see checkboxlist.ftl in the template/simple folder in Struts 2 core). That is why all the checkboxes are displayed across the width of the browser window. For my custom checkboxlist theme I [...] +

In the example application I’ve extended the default XHMTL theme (see file theme.properties under src/main/resources/template/KUTheme). +The checkboxlist.ftl theme that is part of the XHTML theme only includes a space between each label and the next checkbox +(see checkboxlist.ftl in the template/simple folder in Struts 2 core). That is why all the checkboxes are displayed +across the width of the browser window. For my custom checkboxlist theme I want to have a break tag after each label tag +so that each checkbox and its label will be on their own line.

-

In the example application there is a folder named src/main/resources/template/KUTheme. In that folder is a checkboxlist.ftl, the contents of which I originally copied from the checkboxlist.ftl that is in the templates.xhtml folder from the struts 2 core jar.

+

In the example application there is a folder named src/main/resources/template/KUTheme. In that folder is a checkboxlist.ftl, +the contents of which I originally copied from the checkboxlist.ftl that is in the templates.xhtml folder from +the Struts 2 core jar.

I then modified the checkboxlist.ftl in the KUTheme folder to be:

@@ -257,21 +288,37 @@

Be sure to note the change to the second line—using KUTheme_simple in the path.

-

Then in the example application I created a KUTheme_simple folder under src/main/resources/template (optionally you can place it under webapp, e.g. src/main/webapp/template ). In that folder I created checkboxlist.ftl and copied the contents from template.simple checkboxlist.ftl (again found i [...] +

Then in the example application I created a KUTheme_simple folder under src/main/resources/template (optionally you +can place it under webapp, e.g. src/main/webapp/template ). In that folder I created checkboxlist.ftl and copied +the contents from template.simple checkboxlist.ftl (again found in the Struts 2 core jar). After copying the contents +to checkboxlist.ftl that is in KUTheme_simple folder, I modified checkboxlist.ftl so that the label tag has a style +of red bold text and I added a break tag after each label so that each check box and label will be on its own line.

-

Since the XHTML theme is the default theme and I have a theme.properties file defined with parent = xhtml, the KUTheme will inherit all the themes from xhmtl exempt for the theme for the checkboxlist tag since my KUTheme includes a definition for that tag’s layout.  In the struts.xml file (src/main/resources) you will see that the I’ve specified the default theme to be KUTheme.

+

Since the XHTML theme is the default theme and I have a theme.properties file defined with parent = xhtml, +the KUTheme will inherit all the themes from xhmtl exempt for the theme for the checkboxlist tag since my KUTheme includes +a definition for that tag’s layout. In the struts.xml file (src/main/resources) you will see that the I’ve specified +the default theme to be KUTheme.

-

In the deployed web application, Struts 2 will first look for a tag’s template on the application’s class path and if it doesn’t find the template there it will use the default template that is part of the Struts 2 core jar. Since we’ve added a template folder to the application’s web root, now when Struts 2 creates the HTML to display the checkboxlist tag it will use the template that is in the KUTheme folder (which tells it to use the checkboxlist.ftl [...] +

In the deployed web application, Struts 2 will first look for a tag’s template on the application’s class path and if it +doesn’t find the template there it will use the default template that is part of the Struts 2 core jar. Since we’ve +added a template folder to the application’s web root, now when Struts 2 creates the HTML to display the checkboxlist +tag it will use the template that is in the KUTheme folder (which tells it to use the checkboxlist.ftl file that is +in the KUTheme_simple folder instead of the one in the template.simple folder that is part of the Struts 2 core Jar).

After redeploying the application the check boxes for the Car Models Owned should appear like:

Screen shot 2010-09-11 at 12.37.12 PM.png

-

Summary

+

Summary

-

You can easily override the default theme used by Struts 2 to control the appearance and layout of a Struts 2 tag. Each Struts 2 tag has an associated template file (e.g. select.ftl) that is in a folder named after the theme (e.g. xhtml). By default the Struts framework will look in the Struts 2 core Jar file for the theme folder and templates. However, if you include your own theme folder (e.g. KUTheme) under webapp/template (or WebContent/templ [...] +

You can easily override the default theme used by Struts 2 to control the appearance and layout of a Struts 2 tag. Each +Struts 2 tag has an associated template file (e.g. select.ftl) that is in a folder named after the theme (e.g. xhtml). +By default the Struts framework will look in the Struts 2 core Jar file for the theme folder and templates. However, +if you include your own theme folder (e.g. KUTheme) under webapp/template (or WebContent/template in the Ant version) +and specify that folder name (e.g. KUTheme) as the value for the theme attribute, then the Struts 2 framework will look +in that theme folder for the tag’s template.

-

To learn more about how to use the Struts 2 themes and how you can override them, visit Themes and Templates Documentation .

+

To learn more about how to use the Struts 2 themes and how you can override them, visit Themes and Templates Documentation .

diff --git a/content/getting-started/unit-testing.html b/content/getting-started/unit-testing.html index 5319faa..549bbca 100644 --- a/content/getting-started/unit-testing.html +++ b/content/getting-started/unit-testing.html @@ -119,31 +119,56 @@
- << back to Getting Started Edit on GitHub -

Unit Testing

+ + << back to Getting started + +

Unit Testing

+ + -

The example code for this tutorial, unit_testing, is available at https://github.com/apache/struts-examples

+

The example code for this tutorial, unit-testing, is available at struts-examples

-

Introduction

+

Introduction

-

Struts 2 supports running unit tests of methods in the Struts Action class with the Struts 2 JUnit plugin. The JUnit plugin allows you to test methods of an Action class from within the Struts 2 framework. The Struts Servlet filter and interceptors fire just as if your application was running on a Servlet container.

+

Struts 2 supports running unit tests of methods in the Struts Action class with the Struts 2 JUnit plugin. +The JUnit plugin allows you to test methods of an Action class from within the Struts 2 framework. The Struts Servlet +-filter and interceptors fire just as if your application was running on a Servlet container.

-

The Struts 2 user mailing list is an excellent place to get help. If you are having a problem getting the tutorial example applications to work search the Struts 2 mailing list. If you don’t find an answer to your problem, post a question on the mailing list.

+

The Struts 2 user mailing list is an excellent place to get help. If you are having +a problem getting the tutorial example applications to work search the Struts 2 mailing list. If you don’t find an answer +to your problem, post a question on the mailing list.

-

Setup

+

Setup

-

The Struts 2 JUnit plugin jar file must be on your application’s class path. In the example application (see info above) the pom.xml includes a dependency for the struts2-junit-plugin. There are numerous transitive dependencies, including to JUnit and the Spring framework.

+

The Struts 2 JUnit plugin jar file must be on your application’s class path. In the example application (see info above) +the pom.xml includes a dependency for the struts2-junit-plugin. There are numerous transitive dependencies, including +to JUnit and the Spring framework.

-

Writing A Unit Test

+

Writing A Unit Test

-

In the example application, the Register Action class includes using the validate method. This method is automatically executed by the Struts 2 framework prior to the execute method. Additionally, this method needs the values from the user’s input on the form to already have been provided to the instance fields of the Action class (this work is done by another Struts 2 interceptor). So it would be difficult to test the validate method without the overall Struts 2 framework running.

+

In the example application, the Register Action class includes using the validate method. This method is automatically +executed by the Struts 2 framework prior to the execute method. Additionally, this method needs the values from the user’s +input on the form to already have been provided to the instance fields of the Action class (this work is done by another +Struts 2 interceptor). So it would be difficult to test the validate method without the overall Struts 2 framework running.

-

To use the Struts 2 plugin to ensure the Strut 2 framework runs as part of the test, you need to have your JUnit test class extend StrutsTestCase (see RegisterTest class in the example application).

+

To use the Struts 2 plugin to ensure the Strut 2 framework runs as part of the test, you need to have your JUnit test +class extend StrutsTestCase (see RegisterTest class in the example application).

-

Note that the Struts 2 JUnit plugin can be used to design unit tests of other Action class methods such as the input method and also to test methods of a custom interceptor you add to the interceptor stack. Also in this example, the test is for validation performed in the validate method . But the same type of test would work if the validation was done using XML file validation.

+

Note that the Struts 2 JUnit plugin can be used to design unit tests of other Action class methods such as the input method +and also to test methods of a custom interceptor you add to the interceptor stack. Also in this example, the test is +for validation performed in the validate method . But the same type of test would work if the validation +was done using XML file validation.

-

To test the validate method we want Struts to call the Struts action that will cause the Action class’s validate and execute methods to be run. In the example application this action is register.

+

To test the validate method we want Struts to call the Struts action that will cause the Action class’s validate and execute +methods to be run. In the example application this action is register.

struts.xml

@@ -154,15 +179,22 @@
-

Remember the validate method will be called automatically by the framework before calling the execute method. If validation fails the Struts framework will return “input”. If there are no validation errors then the framework will call the execute method and return whatever String the execute method returns.

+

Remember the validate method will be called automatically by the framework before calling the execute method. If validation +fails the Struts framework will return input. If there are no validation errors then the framework will call the execute +method and return whatever String the execute method returns.

-

Test Validation Should Pass

+

Test Validation Should Pass

-

For our first test we’ll test that there should be no validation errors. In the normal flow of this application the user would first enter the form data shown on the register.jsp page.

+

For our first test we’ll test that there should be no validation errors. In the normal flow of this application the user +would first enter the form data shown on the register.jsp page.

Screen shot 2012-02-17 at 2.46.54 PM.png

-

The input fields for the form have the following name values: personBean.firstName, personBean.lastName, personBean.email, and personBean.age. When the user fills out those fields Struts will take the values and provide them to the appropriate set methods of the personBean object. So as part of the test I need to simulate the user filling out these form fields. The StrutsTestCase provides a request object (of type MockHttpServletRequest) that I can use to set these values in the reque [...] +

The input fields for the form have the following name values: personBean.firstName, personBean.lastName, +personBean.email, and personBean.age. When the user fills out those fields Struts will take the values and provide +them to the appropriate set methods of the personBean object. So as part of the test I need to simulate the user filling +out these form fields. The StrutsTestCase provides a request object (of type MockHttpServletRequest) that I can use +to set these values in the request scope.

testExecuteValidationPasses method from RegisterTest class

@@ -185,21 +217,33 @@ -

The first statements in the test method use the request object to set the values of any request parameters. These simulate the values the user would enter into the form fields. Note how the first argument to setParameter is the same as the value of the name attribute in the Struts textfield tag in the register.jsp page.

+

The first statements in the test method use the request object to set the values of any request parameters. These simulate +the values the user would enter into the form fields. Note how the first argument to setParameter is the same as the value +of the name attribute in the Struts textfield tag in the register.jsp page.

-

In this test I’m providing good data for each form field as I want to ensure my validation doesn’t register an error if the user provides correct information.

+

In this test I’m providing good data for each form field as I want to ensure my validation doesn’t register an error if +the user provides correct information.

-

I then call the getActionProxy method with the argument of “/register.action” and then the getAction method to get a reference to the Struts action class for this test. This will ensure that the Struts 2 JUnit plugin will know which Action class should be used–the class associated with the register action in the struts.xml. In this case that class is the Register class.

+

I then call the getActionProxy method with the argument of “/register.action” and then the getAction method to get a reference +to the Struts action class for this test. This will ensure that the Struts 2 JUnit plugin will know which Action class +should be used–the class associated with the register action in the struts.xml. In this case that class is the Register class.

-

The assertNotNull test ensures that argument to getActionProxy reference an Action class identified in struts.xml. It’s a way of checking that you’ve correctly setup struts.xml for this action.

+

The assertNotNull test ensures that argument to getActionProxy reference an Action class identified in struts.xml. It’s +a way of checking that you’ve correctly setup struts.xml for this action.

-

After that I can call actionProxy.execute(). This causes the Struts 2 framework to go through its normal process of Servlet fileter and interceptors for the action identified by the actionProxy (in this case that is the register.action). The validate method of class Register will get called and if it doesn’t create any field or action errors then Struts 2 will call the execute method. In this example, the execute method should return “success”.

+

After that I can call actionProxy.execute(). This causes the Struts 2 framework to go through its normal process of Servlet +filter and interceptors for the action identified by the actionProxy (in this case that is the register.action). The validate +method of class Register will get called and if it doesn’t create any field or action errors then Struts 2 will call +the execute method. In this example, the execute method should return success.

So in the next statement, I check that success was returned.

-

Test Validation Should Fail

+

Test Validation Should Fail

-

To test that validation should fail, I just need to have a test method that doesn’t provide input for a form field. For example, in the validate method of the Register Action class, is a test to ensure the user has entered some information for the personBean.firstName input field. In the test method I would just not use the request object to set a parameter for that field.

+

To test that validation should fail, I just need to have a test method that doesn’t provide input for a form field. +For example, in the validate method of the Register Action class, is a test to ensure the user has entered some information +for the personBean.firstName input field. In the test method I would just not use the request object to set a parameter +for that field.

testExecuteValidationFailsMissingFirstName method from RegisterTest class

@@ -222,11 +266,15 @@ -

In the last assertEquals statement my test checks that the Struts 2 framework returned “input” as that is what the Struts 2 framework will return if the validation adds a field or action error.

+

In the last assertEquals statement my test checks that the Struts 2 framework returned input as that is what the Struts 2 +framework will return if the validation adds a field or action error.

-

Summary

+

Summary

-

There is much more you can do with the Struts 2 JUnit plugin to help you test the methods of your Action class in conjunction with the Struts 2 framemwork. If your Struts 2 application uses Spring to inject dependencies into the Action class then the Struts 2 JUnit Plugin has a StrutsSpringTestCase that your test class should extend. Please read Testing Actions to learn more.

+

There is much more you can do with the Struts 2 JUnit plugin to help you test the methods of your Action class in conjunction +with the Struts 2 framework. If your Struts 2 application uses Spring to inject dependencies into the Action class then +the Struts 2 JUnit Plugin has a StrutsSpringTestCase that your test class should extend. Please read Testing Actions +to learn more.

diff --git a/content/getting-started/using-tags.html b/content/getting-started/using-tags.html index 86d1391..ee97fe8 100644 --- a/content/getting-started/using-tags.html +++ b/content/getting-started/using-tags.html @@ -119,26 +119,44 @@
- << back to Getting Started Edit on GitHub -

Using Tags

- -

This tutorial assumes you’ve completed the Hello World tutorial and have a working helloworld project. The example code for this tutorial, using_tags, is available for checkout from the Struts 2 GitHub repository at https://github.com/apache/struts-examples. The example projects use Maven to manage the artifact dependencies and to build the .war files.

- -

In the Hello World lesson, we added a Struts 2 url tag to the index.jsp to create a hyperlink to the hello.action. This tutorial will explore the url and other Struts 2 tags further.

- -

Web applications differ from conventional websites in that web applications can create a dynamic response. To make it easier to reference dynamic data from a page, the Struts 2 framework offers a set of tags. Some of the tags mimic standard HTML tag while providing added value. Other tags create non-standard, but useful controls.

- -

To use the Struts 2 tags on the view page, you must include a tag library directive. Typically, the taglib directive is <%@ taglib prefix="s" uri="/struts-tags" %\>. So the prefix for all the Struts 2 tags will be “s”. + + << back to Getting started + +

Using Tags

+ + + +

This tutorial assumes you’ve completed the Hello World tutorial and have a working +helloworld project. The example code for this tutorial, using-tags, is available for checkout from the Struts 2 GitHub +repository at struts-examples. The example projects use Maven to manage +the artifact dependencies and to build the .war files.

+ +

In the Hello World lesson, we added a Struts 2 url tag to the index.jsp to create +a hyperlink to the hello.action. This tutorial will explore the url and other Struts 2 tags further.

+ +

Web applications differ from conventional websites in that web applications can create a dynamic response. To make it +easier to reference dynamic data from a page, the Struts 2 framework offers a set of tags. Some of the tags mimic standard +HTML tag while providing added value. Other tags create non-standard, but useful controls.

+ +

To use the Struts 2 tags on the view page, you must include a tag library directive. Typically, the taglib directive +is <%@ taglib prefix="s" uri="/struts-tags" %\>. So the prefix for all the Struts 2 tags will be s. If you want to actually read the Struts 2 tag TLD file, you’ll find it in the `META-INF folder of the Struts 2 core jar.

Struts 2 url Tag

One use of the Struts 2 Tags is to create links to other web resources, especially to other resources in the local application.

-

While HTML provides a simple a tag for creating hyperlinks, the HTML tag often requires us to include redundant information. Also the HTML tag cannot easily access dynamic data provided by the framework.

+

While HTML provides a simple a tag for creating hyperlinks, the HTML tag often requires us to include redundant information. +Also the HTML tag cannot easily access dynamic data provided by the framework.

-

A very common use case in web applications is linking to other pages. In the Hello World tutorial we added a link to hello.action inside the index.jsp using the Struts 2 url tag. Please refer to the url documentation for more information about the url tag.

+

A very common use case in web applications is linking to other pages. In the Hello World +tutorial we added a link to hello.action inside the index.jsp using the Struts 2 url tag. Please refer +to the url documentation for more information about the url tag.

index.jsp

@@ -158,7 +176,8 @@ If you want to actually read the Struts 2 tag TLD file, you’ll find it in the -

When you run the Hello World tutorial in your Servlet container and then mouse over the Hello World hyperlink created by the Struts 2 url tag you’ll see that the URL created is hello.action (relative to the web context’s root folder).

+

When you run the Hello World tutorial in your Servlet container and then mouse over the Hello World hyperlink created +by the Struts 2 url tag you’ll see that the URL created is hello.action (relative to the web context’s root folder).

Examine the struts.xml configuration in the Hello World tutorial and you will find this:

@@ -172,9 +191,11 @@ If you want to actually read the Struts 2 tag TLD file, you’ll find it in the -

The action node above maps the hello.action to the execute method of class HelloWorldAction. If the execute method returns success, the view page HelloWorld.jsp (in web context root folder) will be returned to the user.

+

The action node above maps the hello.action to the execute method of class HelloWorldAction. If the execute method +returns success, the view page HelloWorld.jsp (in web context root folder) will be returned to the user.

-

A common use case is that the URL also needs to include a value for a query string parameter such as userName. To add a query string parameter and its value use the Struts 2 param tag, nested inside the url tag.

+

A common use case is that the URL also needs to include a value for a query string parameter such as userName. To add +a query string parameter and its value use the Struts 2 param tag, nested inside the url tag.

For the Using Tags tutorial add the following to index.jsp just after the link for Hello World.

@@ -189,17 +210,23 @@ If you want to actually read the Struts 2 tag TLD file, you’ll find it in the ... -

Rather then put the url tag as the value for the anchor tag’s href attribute, we’ve separated out the s:url tag into its own code block. Nested inside the url tag is the Struts 2 param tag. This tag lets you specify a parameter name (e.g. userName) and a value for that parameter (e.g. Bruce Phillips).

+

Rather then put the url tag as the value for the anchor tag’s href attribute, we’ve separated out the s:url tag into +its own code block. Nested inside the url tag is the Struts 2 param tag. This tag lets you specify a parameter name ( +e.g. userName) and a value for that parameter (e.g. Bruce Phillips).

-

Notice the use of the var attribute. The value of the var attribute is a reference we can use later in our code to refer to the url created.

+

Notice the use of the var attribute. The value of the var attribute is a reference we can use later in our code to refer +to the url created.

-

Examine the anchor tag above. Notice the value of the href attribute is ${helloLink}. The view page will substitute the hyperlink we created using the url tag for the ${helloLink} reference. Note that the query string parameter will be properly URL-encoded (Bruce+Phillips).

+

Examine the anchor tag above. Notice the value of the href attribute is ${helloLink}. The view page will substitute +the hyperlink we created using the url tag for the ${helloLink} reference. Note that the query string parameter will +be properly URL-encoded (Bruce+Phillips).

In the next tutorial we’ll cover how Struts can access the query string parameter value.

Struts 2 Form Tag

-

Most applications will use several data entry forms. The Struts 2 tags make creating input forms easy. Consult the Form Tags Reference for all the details about the Struts 2 form tags.

+

Most applications will use several data entry forms. The Struts 2 tags make creating input forms easy. +Consult the Form Tags Reference for all the details about the Struts 2 form tags.

Each of the Struts 2 form tags has numerous attributes to mimic the normal HTML form tag attributes.

@@ -217,7 +244,8 @@ If you want to actually read the Struts 2 tag TLD file, you’ll find it in the </s:form> -

The Struts 2 textfield tag provides a input html tag of tag text and the submit tag creates a submit button. When the index page is return by the server to the browser you should see:

+

The Struts 2 textfield tag provides a input html tag of tag text and the submit tag creates a submit button. When the index +page is return by the server to the browser you should see:

Struts Form.png

@@ -243,7 +271,10 @@ If you want to actually read the Struts 2 tag TLD file, you’ll find it in the -

Note how Struts 2 created a table inside the form to position the form elements. In later tutorials you’ll learn how to specify the layout (table, CSS). The Struts 2 textfield tag created an HTML input tag of type text with a name value that matches the name value of the textfield tag. Struts 2 also created a label HTML tag based on the label value of the textfield tag.

+

Note how Struts 2 created a table inside the form to position the form elements. In later tutorials you’ll learn how +to specify the layout (table, CSS). The Struts 2 textfield tag created an HTML input tag of type text with a name value +that matches the name value of the textfield tag. Struts 2 also created a label HTML tag based on the label value +of the textfield tag.

In the next tutorial we’ll cover how to use Struts 2 to process this form submission.

@@ -256,11 +287,17 @@ If you want to actually read the Struts 2 tag TLD file, you’ll find it in the
<s:property value="messageStore.message" />
 
-

The most common use of the property tag is to “get” the value returned by calling a public get method (of the Action class) and then to include that value in the HTML returned to the browser.

+

The most common use of the property tag is to “get” the value returned by calling a public get method (of the Action class) +and then to include that value in the HTML returned to the browser.

-

As discussed in the Hello World tutorial, the value of messageStore.message instructs Struts 2 to first call method getMessageStore of the Action class. That method call returns a MessageStore object. The .message part instructs Struts 2 to call the getMes [...] +

As discussed in the Hello World tutorial, the value of messageStore.message instructs +Struts 2 to first call method getMessageStore of the Action class. That method call returns a MessageStore object. +The .message part instructs Struts 2 to call the getMessage method of the MessageStore object. The getMessage +method returns a String which will be included in the HTML returned to the browser.

-

One very useful feature of the Struts 2 property tag is that it will automatically convert the most common data types (int, double, boolean) to their String equivalents. To demonstrate this feature let’s add a static int variable to class HelloWorldAction.

+

One very useful feature of the Struts 2 property tag is that it will automatically convert the most common data types +(int, double, boolean) to their String equivalents. To demonstrate this feature let’s add a static int variable to class +HelloWorldAction.

Add Static Field

@@ -272,7 +309,8 @@ If you want to actually read the Struts 2 tag TLD file, you’ll find it in the
-

Each time the execute method is called we’ll increase helloCount by 1. So add this code to the execute method of the HelloWorldAction class.

+

Each time the execute method is called we’ll increase helloCount by 1. So add this code to the execute method of the +HelloWorldAction class.

Increase helloCount

@@ -280,20 +318,26 @@ If you want to actually read the Struts 2 tag TLD file, you’ll find it in the -

Whenever a user clicks one of the links on the page index.jsp (or submits the form), method execute of class HelloWorldAction will be run and the static field helloCount will be increased by 1.

+

Whenever a user clicks one of the links on the page index.jsp (or submits the form), method execute of class HelloWorldAction +will be run and the static field helloCount will be increased by 1.

-

To include the value of the helloCount attribute in the HelloWorld.jsp we can use the Struts 2 property tag. Add the following to HelloWorld.jsp after the h2 tag.

+

To include the value of the helloCount attribute in the HelloWorld.jsp we can use the Struts 2 property tag. +Add the following to HelloWorld.jsp after the h2 tag.

Use Property Tag To Display helloCount Value

<p>I've said hello <s:property value="helloCount" /> times!</p>
 
-

So even though the getHelloCount method returns an integer type, Struts 2 converted it to type String and placed it into the body of the p tag.

+

So even though the getHelloCount method returns an integer type, Struts 2 converted it to type String and placed it +into the body of the p tag.

-

Note that even though helloCount is a static field, the get method for helloCount is not static. For Struts 2 to call the getHelloCount method to get the value of helloCount, the getHelloCount method cannot be static.

+

Note that even though helloCount is a static field, the get method for helloCount is not static. For Struts 2 to call +the getHelloCount method to get the value of helloCount, the getHelloCount method cannot be static.

-

If the value returned by the get method is an object, then the property tag will cause Struts 2 to call the object’s toString method. Of course, you should always override Class Object’s toString method in your model classes. Add the following toString method to the MessageStore class:

+

If the value returned by the get method is an object, then the property tag will cause Struts 2 to call the object’s +toString method. Of course, you should always override Class Object’s toString method in your model classes. +Add the following toString method to the MessageStore class:

Add toString Method To Class MessageStore

@@ -311,11 +355,13 @@ If you want to actually read the Struts 2 tag TLD file, you’ll find it in the -

Since getMessageStore of HelloWorldAction class returns a MessageStore object, Struts 2 will call the toString method of class MessageStore. The string returned by that toString method will be displayed in the browser.

+

Since getMessageStore of HelloWorldAction class returns a MessageStore object, Struts 2 will call the toString +method of class MessageStore. The string returned by that toString method will be displayed in the browser.

using_tags_hello.png

-

We covered a lot in this tutorial, but we’ve really only scratched the surface of how to use the Struts 2 tags. Consult the Struts 2 Tag Reference for much more information about all the Struts 2 tags.

+

We covered a lot in this tutorial, but we’ve really only scratched the surface of how to use the Struts 2 tags. +Consult the Struts 2 Tag Reference for much more information about all the Struts 2 tags.

diff --git a/content/getting-started/wildcard-method-selection.html b/content/getting-started/wildcard-method-selection.html index 5baae2e..69f636d 100644 --- a/content/getting-started/wildcard-method-selection.html +++ b/content/getting-started/wildcard-method-selection.html @@ -119,25 +119,35 @@
- << back to Getting Started Edit on GitHub -

Wildcard Method Selection

+ + << back to Getting started + +

Wildcard Method Selection

-

The example code for this tutorial, wildcard_method_selection, is available for checkout at https://github.com/apache/struts-examples

+ -

Introduction

+

The example code for this tutorial, wildcard-method-selection, is available for checkout at struts-examples

+ +

Introduction

In this tutorial we’ll cover how to configure an action node in the struts.xml configuration file so that one action node can be used to relate several different Action URLs to specific methods of the Action class. This will reduce the number of action nodes we must write in the struts.xml configuration file.

The Struts 2 user mailing list is an excellent place to get help. If you are having a problem getting the tutorial example applications to work search the Struts 2 mailing list. If you don’t find an answer to your problem, post a question on the mailing list.

-

Example Application

+

Example Application

Screen shot 2010-05-30 at 8.51.40 AM.png

The example application that supports this tutorial shows how to use Struts 2’s wildcard method selection methodology. The example application is just a simple one that lists some people and lets you edit them, delete them, and add a new person to the list. Everything that the application needs to do with a Person (the model class) is controlled by the Struts 2 ActionSupport class Wildcard Method Selection

Without using the wildcard method selection technique, I’d have to write an action mapping node in the Struts 2 configuration file for each separate action I’d want to call. For example:

@@ -184,13 +194,13 @@

If there is no value in front of Person, then the Struts 2 framework will call the execute method of the class PersonAction.

-

Dynamic Method Invocation

+

Dynamic Method Invocation

The wildcard method selection technique explained above should not be confused with the “Dynamic Method Invocation” technique. The Struts 2 documentation explains this technique (which uses the bang, !, operator in the action name) and recommends against using the “Dynamic Method Invocation” technique due to security and other reasons related to how this technique is implemented internally.

The Struts 2 documentation also recommends turning off the option to use the dynamic method invocation by setting struts.enable.DynamicMethodInvocation to FALSE in the Struts configuration.

-

Summary

+

Summary

By using the wildcard method selection technique explained above, you can significantly reduce the number of action mapping nodes you need to write and manage in the Struts 2 XML configuration file.

-- To stop receiving notification emails like this one, please contact ['"commits@struts.apache.org" '].