Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

How do we set checkboxes false (on uncheck)?

  • Use a boolean to represent the checkbox property
  • Implement the preparable interface
    • Implement a prepare method and set all checkbox booleans to false
  • Or, set the checkbox booleans to the false value using static parameters.

If the Action is model-backed and persisted in the session

  • Use an Action property as a buffer for the checkbox boolean.
  • On Save, set the model property to the Action property.
    • These extra steps avoid setting the model properties to false prematurely.

See also

In Struts 2, checkboxes are stateful, and we don't need to do that anymore.

Just treat the checkbox like any other tag. The framework will notice if a checkbox is "missing" from a request and inject a false value for that checkbox.

How can we

...

set the focus on a form field?

SAF1 Struts 1 generates a little JavaScript that helps set the focus, if you specify the field in the JSP.

...

Code Block
<action name="Welcome">
<result>/pages/Welcome.jsp</result>
</action>

How do

...

we emulate the default="true" attribute of a Struts 1.x Action mapping?

Rather than tag the action as being the default, the default action is set by name using default-action-ref element.

...

By using an element, rather than an attribute, packages can inherit the default action name.

How do

...

we extend an action mapping in struts.xml??

Starting in Struts 1.3, you could we can use the "extends" attribute in your our Struts configuration Action mapping to have it inherit properties from a base mapping. In Struts 2, that technique is no longer necessary because you we have packages. You We can create a package, then set for that package the default Result type, Interceptor chain, and global results. This leaves very little information to actually be included in an action element.

...

In fact, packages themselves can extend other packages, as the "chat" package extends "struts-default" in the above example.

Can

...

we use DynaBeans in my Struts actions?

First, ask "Do we want to use DynaBeans at all?"

Typically, DynaBeans are used to emulate a plain old JavaBean that is utilized by the business logic. If that is the case, then we can just use the plain old JavaBean directly. Struts 2 will happily cope with rich properties, like date fields. We don't have to reduce everything to a String anymore.

Otherwise, DynaBeans can be treated as a regular Java object, if the particular implementation contains a getMap() method. This method lets OGNL, the Struts 2 expression language, know how to access and set data. DynaBean variants like the LazyDynaBean can create themselves on-the-fly, where other more static types might need to be created in your Action's constructor before being used. However, one of the benefits of Struts 2 over Struts 1.x is that you can use your existing JavaBeans directly without the need of an ActionForm-type wrapper, so you might try bypassing the familiar DynaBean route.

How do we set a token to track duplicate submits?

In Struts 2, there shouldn't be any token code in an Action class at all. The Interceptors are designed to do all the work. There are three flavors, the Token Interceptor, the Token Session Interceptor, and the Execute and Wait Interceptor.

Token Interceptor

The Token Interceptor is most like the Struts 1 approach, except that we don't have to change the Action class to add a lot of busy code. The tradeoff is that we do have to include the token tag in the form, to bootstrap the process. In Struts 1, we set the token in the Action, and the form tag detected it. In Struts 2, we set the token in the page, and the Interceptor detects it.

If the Interceptor does detects a duplicate submit, then it automatically returns an "invalid.token" result code, which can be handled via the action mapping.

Token Session

The Token Session Interceptor tries to be even more automatic by attempting to display the same response that the original, valid action invocation would have displayed if no multiple requests were submitted in the first place.

Execute and Wait

The Execute and Wait Interceptor embraces long-running taks by presenting a progress meter as the Action runs in the background.

Next: What is the ActionContext?

...