You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Charles Parker <ch...@namesforlife.com> on 2009/12/22 17:05:11 UTC

best practices in struts 2.1 - Tiles and Convention - clean URLs with minimum number of actions

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",results={@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-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: best practices in struts 2.1 - Tiles and Convention - clean URLs with minimum number of actions

Posted by Paweł Wielgus <po...@gmail.com>.
Hi Chuck,
i'm also addicted to convention and i do it like that:
/register!input
(i don't remember well if it is ! or some other character but that is
possible for sure)
If one call such an address his browser will land on input result with
no validation being fired.
Also if You want to depent on request method chech the REST plugin,
maybe it will suits your needs best.

Best greetings,
Pawel Wielgus.

2009/12/22, Charles Parker <ch...@namesforlife.com>:
> 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",results={@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-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
>
>

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


Re: best practices in struts 2.1 - Tiles and Convention - clean URLs with minimum number of actions

Posted by Wes Wannemacher <we...@wantii.com>.
Charles,

I haven't really found any way to get around the setup you are talking
about... If you want validation, you pretty much have to map to
different actionable URLs. From the framework's perspective, it really
can't know that you want validation or not without different request
URLs. The alternative is to try to code around it, but then your UI
logic becomes entangled into your application code.

-Wes

On Tue, Dec 22, 2009 at 11:05 AM, Charles Parker
<ch...@namesforlife.com> wrote:
> 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",results={@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-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
>
>



-- 
Wes Wannemacher

Head Engineer, WanTii, Inc.
Need Training? Struts, Spring, Maven, Tomcat...
Ask me for a quote!

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