3. Working with Struts 2 Actions Flashcards

1
Q

What are the three things an Action does?

A
  1. Encapsulate the work being done for a given request.
  2. Data carrier, giving all needed data to the view.
  3. Determine which result should render the view.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

When does an action get instantiated? Is it a singleton?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How do you configure what results an Action has available?

A

An action has one to many results with names. These names, depending on the string returned by the action, determines which result to use.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What are the parameters for a Struts 2 package? What do they do?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What happens if you don’t provide a namespace for a package?

A

Actions will go to the default namespace, which is just “”.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is the default namespace? What use does it serve?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is the root namespace? Does it relate to the default namespace?

A

Root namespace is “/” and has no relation to the default namespace “”. Root, just like all other namespace must match exactly.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What’s the package name that provides many/most of the struts configuration built in?

A

‘struts-default’

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Unless you have a compelling reason to do so, which package should you always extend in your packages?

A

struts-default

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Does an action need to implement the Action interface? What benefits does it give us?

A

No, it just needs an ‘execute()’ method. However, we get access to some constants that makes getting results a little easier.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What Constant Strings are declared in the Action interface?

A

ERROR, INPUT, LOGIN, NONE, SUCCESS

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is a pass-through action? How would you configure one?

A

Empty actions that render a result. Declare an action, and have a result with no name.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What method would you need to make on an Action validate data before calling execute()? Is there a useful interface for it?

A

validate()

Yes, there is a Validateable interface.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What interceptor calls the validate() method on an action?

A

workflow

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

In your validate() method, how can you put an error on a field?

A

addFielderror(“FieldName”, “Error Message”);

The above method.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What’s usually the best base class for any Action?

A

ActionSupport

17
Q

What’s the name of the interceptor that calls the validate() method, and prevents execute() from being called if there’s any field errors?

A

workflow interceptor

18
Q

What’s the method to add an error to an action call? (Not a specific field)

A

addActionError(“Error Message”);

19
Q

ActionSupport implements TextProvider, what functionality does this provide?

A

Allows you to include a properties file, and read properties from it.

20
Q

What do you name your property file if using TextProvider interface?

A

ActionName.properties

Example: Register.properties

21
Q

How do you setup an action to construct an object instead of individual fields?

A

Use ModelDriven actions

  1. Implement ModelDriven interface. (Interceptor that uses this is already in the default-stack.
  2. override getModel() method, return your object you want to populate fields for.
22
Q

When uploading a file to Struts 2, what parameters are exposed by the params interceptor?

A

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.

23
Q

Do we have to cleanup files uploaded to the server?

A

No, the interceptor will run after the result and cleanp temporary versions of uploaded files.