3. Working with Struts 2 Actions Flashcards
What are the three things an Action does?
- Encapsulate the work being done for a given request.
- Data carrier, giving all needed data to the view.
- Determine which result should render the view.
When does an action get instantiated? Is it a singleton?
It’s instantiated when the request is made, yes it’s a singleton.
It’s the data carrier for the result, so if it wasn’t you’d get collisions.
How do you configure what results an Action has available?
An action has one to many results with names. These names, depending on the string returned by the action, determines which result to use.

What are the parameters for a Struts 2 package? What do they do?
Name (required) – Doesn’t do anything, used for organizing really.
namespace – URL for all actions in this package. Ex. “/chapterThree/secure”
extends – parent package to inherit from.
abstract – (default false) if true package will only be used to define inheritable components.

What happens if you don’t provide a namespace for a package?
Actions will go to the default namespace, which is just “”.
What is the default namespace? What use does it serve?
It’s the namespace “”, if a call fails to find an action in the namespace it’s supposed to be in then it will check the default namespace as a last ditch effort.
What is the root namespace? Does it relate to the default namespace?
Root namespace is “/” and has no relation to the default namespace “”. Root, just like all other namespace must match exactly.
What’s the package name that provides many/most of the struts configuration built in?
‘struts-default’
Unless you have a compelling reason to do so, which package should you always extend in your packages?
struts-default
Does an action need to implement the Action interface? What benefits does it give us?
No, it just needs an ‘execute()’ method. However, we get access to some constants that makes getting results a little easier.
What Constant Strings are declared in the Action interface?
ERROR, INPUT, LOGIN, NONE, SUCCESS
What is a pass-through action? How would you configure one?
Empty actions that render a result. Declare an action, and have a result with no name.

What method would you need to make on an Action validate data before calling execute()? Is there a useful interface for it?
validate()
Yes, there is a Validateable interface.
What interceptor calls the validate() method on an action?
workflow
In your validate() method, how can you put an error on a field?
addFielderror(“FieldName”, “Error Message”);
The above method.
What’s usually the best base class for any Action?
ActionSupport
What’s the name of the interceptor that calls the validate() method, and prevents execute() from being called if there’s any field errors?
workflow interceptor
What’s the method to add an error to an action call? (Not a specific field)
addActionError(“Error Message”);
ActionSupport implements TextProvider, what functionality does this provide?
Allows you to include a properties file, and read properties from it.
What do you name your property file if using TextProvider interface?
ActionName.properties
Example: Register.properties
How do you setup an action to construct an object instead of individual fields?
Use ModelDriven actions
- Implement ModelDriven interface. (Interceptor that uses this is already in the default-stack.
- override getModel() method, return your object you want to populate fields for.
When uploading a file to Struts 2, what parameters are exposed by the params interceptor?
Formname – the file itself.
FormnameContentType – Content type of the file.
FormnameFileName – The name of the uploaded file, as stored on the server.
*Formname – This is the name of the form element that file was attached to.

Do we have to cleanup files uploaded to the server?
No, the interceptor will run after the result and cleanp temporary versions of uploaded files.