You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Filipe David Manana <fd...@ieee.org> on 2008/01/28 16:48:06 UTC

Struts 2.0.11 Validation and Conversion

Hi,

I am getting errors from Parameters Interceptor while it tries to set
a value for a property.

My JSP is:

 <s:form theme="ajax" method="post" action="add_account"
namespace="/lsf/accounts" validate="true">
    <s:textfield required="true" label="Name" name="account.name" />
    <s:textfield label="UNIX GroupID" name="account.unixGroupId" />
    <s:submit value="Add" onclick="this.form.submit();" />
 </s:form>

The action is mapped to the method "String add()" of the following class:


@Validation
@Conversion
public class AccountAction extends BaseAction
{
   private Account account;
   private Long accountId;


   public void prepare() throws Exception
   {
      if ( accountId == null )
         account = new Account();
      else
         account = AccountManager.findById(accountId);
   }


   public String add() throws Exception
   {
      return SUCCESS;
   }


   public String update() throws Exception
   {
      return SUCCESS;
   }


   @VisitorFieldValidator(message = "")
   public Account getAccount()
   {
      return account;
   }


   public void setAccount(Account account)
   {
      this.account = account;
   }


   public Long getAccountId()
   {
      return accountId;
   }

   public void setAccountId(Long accountId)
   {
      this.accountId = accountId;
   }


The definition of the Account class:


@Validation
@Conversion
public class Account
{
   private Long id;
   private String name;
   private Long unixGroupId;

   public Long getId()
   {
      return id;
   }

   public void setId(Long id)
   {
      this.id = id;
   }

   public String getName()
   {
      return name;
   }

   @RequiredStringValidator(message = "Account name is required", trim = true)
   public void setName(String name)
   {
      this.name = name;
   }

   public long getUnixGroupId()
   {
      return unixGroupId;
   }

   @TypeConversion(converter = "actions.NullLongConverter")
   @ConversionErrorFieldValidator(message = "UNIX GroupID must be in
the range 0..65535")
   @RegexFieldValidator( message = "UNIX GroupID must be in the range
0..65535", expression = "^\\s*(?:[0-9]{1,6})?\\s*$")
   public void setUnixGroupId(Long unixGroupId)
   {
      this.unixGroupId = unixGroupId;
   }
}

My validator:
public class NullLongConverter extends StrutsTypeConverter
{
   public Object convertFromString(Map context, String[] values, Class toClass)
   {
      Long val = null;

      if ( values != null && values[0] != null && !values[0].equals("") )
      {
         try
         {
            values[0].trim();
            val = Long.parseLong(values[0]);
            long v = val.longValue();
            if ( v < 0 || v > 65535 ) val = null;
         }
         catch( Exception ex ) { }
      }
      return val;
   }

   public String convertToString(Map context, Object o)
   {
      if ( o == null ) return null;
      else return o.toString();
   }
}


My action is using the paramsPrepareParamsStack.
I get this exceptions from

[ERROR com.opensymphony.xwork2.interceptor.ParametersInterceptor 28
Jan 2008 16:37:50] ParametersInterceptor - [setParameters]: Unexpected
Exception caught setting 'account.unixGroupId' on 'class
actions.lsf.accounts.AccountAction: Error setting expression
'account.unixGroupId' with value '12'

[ERROR com.opensymphony.xwork2.interceptor.ParametersInterceptor 28
Jan 2008 16:37:51] ParametersInterceptor - [setParameters
]: Unexpected Exception caught setting 'account.unixGroupId' on 'class
actions.lsf.accounts.AccountAction: Error setting expression
'account.unixGroupId' with value'[Ljava.lang.String;@52f9b2'



Any ideia why this is not working?

By the way, I also tried not using custom conversions / validations,
that is, using the built in conversion mechanisms. I get similar
errors.

Thanks


-- 
Filipe David Manana,
fdmanana@ieee.org

Obvious facts are like secrets to those not trained to see them.

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


Re: Struts 2.0.11 Validation and Conversion

Posted by Adam Hardy <ah...@cyberspaceroad.com>.
Hi Filipe,

what is that regex that you are using in the annotations for setUnixGroupId()?

Is it required? It seems like unnecessary overhead for validating and converting 
a long. Have you tried it without it?

What is the reason for your own NullLongConverter? I guess it's because there is 
no out-of-the-box Struts LongConverter that handles null? (not had the need for 
it myself yet).

The second error seems to come from the parameter coming in as an array. Is 
there an obvious reason why, considering your HTML form?

Perhaps there are clues in the stacktraces.

Regards
Adam

Filipe David Manana on 28/01/08 15:48, wrote:
> I am getting errors from Parameters Interceptor while it tries to set
> a value for a property.
> 
> My JSP is:
> 
>  <s:form theme="ajax" method="post" action="add_account"
> namespace="/lsf/accounts" validate="true">
>     <s:textfield required="true" label="Name" name="account.name" />
>     <s:textfield label="UNIX GroupID" name="account.unixGroupId" />
>     <s:submit value="Add" onclick="this.form.submit();" />
>  </s:form>
> 
> The action is mapped to the method "String add()" of the following class:
> 
> 
> @Validation
> @Conversion
> public class AccountAction extends BaseAction
> {
>    private Account account;
>    private Long accountId;
> 
> 
>    public void prepare() throws Exception
>    {
>       if ( accountId == null )
>          account = new Account();
>       else
>          account = AccountManager.findById(accountId);
>    }
> 
> 
>    public String add() throws Exception
>    {
>       return SUCCESS;
>    }
> 
> 
>    public String update() throws Exception
>    {
>       return SUCCESS;
>    }
> 
> 
>    @VisitorFieldValidator(message = "")
>    public Account getAccount()
>    {
>       return account;
>    }
> 
> 
>    public void setAccount(Account account)
>    {
>       this.account = account;
>    }
> 
> 
>    public Long getAccountId()
>    {
>       return accountId;
>    }
> 
>    public void setAccountId(Long accountId)
>    {
>       this.accountId = accountId;
>    }
> 
> 
> The definition of the Account class:
> 
> 
> @Validation
> @Conversion
> public class Account
> {
>    private Long id;
>    private String name;
>    private Long unixGroupId;
> 
>    public Long getId()
>    {
>       return id;
>    }
> 
>    public void setId(Long id)
>    {
>       this.id = id;
>    }
> 
>    public String getName()
>    {
>       return name;
>    }
> 
>    @RequiredStringValidator(message = "Account name is required", trim = true)
>    public void setName(String name)
>    {
>       this.name = name;
>    }
> 
>    public long getUnixGroupId()
>    {
>       return unixGroupId;
>    }
> 
>    @TypeConversion(converter = "actions.NullLongConverter")
>    @ConversionErrorFieldValidator(message = "UNIX GroupID must be in
> the range 0..65535")
>    @RegexFieldValidator( message = "UNIX GroupID must be in the range
> 0..65535", expression = "^\\s*(?:[0-9]{1,6})?\\s*$")
>    public void setUnixGroupId(Long unixGroupId)
>    {
>       this.unixGroupId = unixGroupId;
>    }
> }
> 
> My validator:
> public class NullLongConverter extends StrutsTypeConverter
> {
>    public Object convertFromString(Map context, String[] values, Class toClass)
>    {
>       Long val = null;
> 
>       if ( values != null && values[0] != null && !values[0].equals("") )
>       {
>          try
>          {
>             values[0].trim();
>             val = Long.parseLong(values[0]);
>             long v = val.longValue();
>             if ( v < 0 || v > 65535 ) val = null;
>          }
>          catch( Exception ex ) { }
>       }
>       return val;
>    }
> 
>    public String convertToString(Map context, Object o)
>    {
>       if ( o == null ) return null;
>       else return o.toString();
>    }
> }
> 
> 
> My action is using the paramsPrepareParamsStack.
> I get this exceptions from
> 
> [ERROR com.opensymphony.xwork2.interceptor.ParametersInterceptor 28
> Jan 2008 16:37:50] ParametersInterceptor - [setParameters]: Unexpected
> Exception caught setting 'account.unixGroupId' on 'class
> actions.lsf.accounts.AccountAction: Error setting expression
> 'account.unixGroupId' with value '12'
> 
> [ERROR com.opensymphony.xwork2.interceptor.ParametersInterceptor 28
> Jan 2008 16:37:51] ParametersInterceptor - [setParameters
> ]: Unexpected Exception caught setting 'account.unixGroupId' on 'class
> actions.lsf.accounts.AccountAction: Error setting expression
> 'account.unixGroupId' with value'[Ljava.lang.String;@52f9b2'
> 
> 
> 
> Any ideia why this is not working?
> 
> By the way, I also tried not using custom conversions / validations,
> that is, using the built in conversion mechanisms. I get similar
> errors.


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