I'm using struts 2.1.8.1 here and have a question about best practices in the following situation:

I have a form page, 'register'. It takes several fields, username, password, etc.

I want the initial form page to be rendered via GET:

http://my.server.net/register

This shows the blank form (the tile definition below), without attempting to validate the fields.

  <definition name="registrationPage" extends="defaultLayout">
<put-attribute name="content" value="/WEB-INF/pages/registration.jsp"/>
  </definition>

On POST to the same URL 'http://my.server.net/register', I want to perform validation on the form, (via register-validation.xml or Annotation). If the form validates then perform the action that triggers the underlying business logic. Otherwise re-display the form with validation errors (again, same URL, 'http://my.server.net/register').

I am using the Convention plugin and the Tiles plugin. The combination of these two seems to throw a wrench in the works. It seems that with the Convention plugin, I can only depend on execute() being called, or I need to define multiple actions.

The approach I'm using now works reasonably well but there must be a cleaner way:

@ParentPackage("testing")
public class Register extends ActionSupport
{
@Action(value="register",result...@result(name="input", type="tiles", location="registrationPage")})
    @SkipValidation
    public String execute() throws Exception
    {
        return this.isBlank() ? INPUT : "submit";
    }

    @Action
    (
        value="register-submit",
        results=
        {
@Result(name="input", type="tiles", location="registrationPage"), @Result(name="error", type="tiles", location="registrationPage"), @Result(name="success", type="tiles", location="registrationSuccessfulPage")
        }
    )
    public String executeValidate() throws Exception
    {
        // perform business logic
        return SUCCESS;
    }

    private boolean isBlank()
    {
return StringUtils.isBlank(this.username) && StringUtils.isBlank(this.password)
    }
}

Someone can still link in to /register-submit, which isn't awful, since validation is performed. But I'd like to minimize the number of actionable URLs on my site and avoid writing an isBlank() method for each form.

It would seem more straightforward if I could depend on an INPUT result on the first load of the page (with @SkipValidation), and then execute() thereafter, with only a single action defined at the class level instead of multiple actions at the method level.


Is this possible, and if so, what is the most appropriate way to do this via struts 2.1?

I am addicted to the Convention plugin annotation, and I cannot do without Tiles.

For reference, I'm using the following in struts.xml:

<constant name="struts.convention.action.alwaysMapExecute" value="false"/>

<package name="testing" extends="struts-default">
    <!-- Set the tiles result type as the default for this package. -->
    <result-types>
<result-type name="tiles" class="org.apache.struts2.views.tiles.TilesResult" default="true"/>
    </result-types>
</package>

Many thanks for any alternatives or refinements of this approach.

/ chuck


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscr...@struts.apache.org
For additional commands, e-mail: user-h...@struts.apache.org

Reply via email to