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.