You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tapestry.apache.org by lu...@centerparcs.com on 2004/08/31 19:08:19 UTC

puzzled by PropertySelection




Hi,

I am a bit puzzled by the behaviour of PropertySelection component. I am
using a simple StringPropertySelectionModel defined in the page like so:

/**
 * Returns a set of priorities that the user may choose from.
 */
public IPropertySelectionModel getPriorityModel() {
      return new StringPropertySelectionModel(
            new String[] {
                  getMessage( prio_L ),
                  getMessage( prio_M ),
                  getMessage( prio_H ) } );
}

and use like so:

<select jwcid="prioritySelection@PropertySelection"
      model="ognl:getPriorityModel()"
      value="ognl:newsItem.priority"/>

newsItem.priority is an integer. I understood from the
StringPropertySelectionModel documentation that the value set/returned
would be a simple index into the array of Strings. However, when I submit
the form, I get a java.lang.NumberFormatException; apparently Tapestry is
trying to convert the label value to an integer:

Unable to update expression 'newsItem.priority' for
NewsItemView_New01a$Enhance_254@4369905b[NewsCRUDPage/theNewsItemEdit_New01_1]
 to medium

I checked the source of the page, and the select seems to be ok, it
contains the numeric values I expected:

<select name="prioritySelection">
<option value="0">low</option>
<option value="1">medium</option>
<option value="2">high</option>
</select>

Another thing I would have expected is that the PropertySelection picked up
the current value from the newsitem when rendered.

Can anybody give me a hint as to what I am missing here ?

Thanks in advance, Luc.


Disclaimer

The information contained in this message and any attachments:

does not constitute an offer or an acceptance of an offer or a
representation or warranty, nor shall it form any part of a legally binding
contract;
has been compiled with care but no representation or warranty (express or
implied) is made as to its accuracy or completeness and no liability can be
accepted for any loss arising from the use of any of the information;
may include opinions or views, which unless expressly stated otherwise, are
not those of the company or any person in relation to whom the company
would have vicarious liability or responsibility;
is not guaranteed to be free from any so-called computer viruses and it is
strongly recommended that you check for such viruses before downloading it
to your computer equipment;
may include hypertext links to the sites of other companies or persons, the
content of which this company has no control over and accepts no liability
for;
is for the exclusive use of the addressee and may contain confidential,
privileged, work product immunity and/or non-disclosable information.  If
you are not the intended recipient and receive this message and any
attachments, you must not read, copy, transmit, disclose or otherwise use
any of them (or part of them) in any way (to do so may be unlawful) and we
would be grateful if you could inform us immediately of any such receipt
and subsequently entirely and permanently erase the message and any
attachments from your equipment.

E-mail is an informal method of communication and is subject to possible
data corruption.  For these reasons it will normally be inappropriate to
rely upon information contained in an e-mail without obtaining tangible
written confirmation of it.


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


Re: puzzled by PropertySelection

Posted by Paul Ferraro <pm...@columbia.edu>.
The PropertySelection component uses a PropertySelectionModel to relate 
a set of objects to html option values and labels.
The nomenclature here is admittedly poor...
The value binding of the PropertySelection component is used to indicate 
the selected "option", not the "value" from the model.

The StringPropertySelectionModel is an helper implementation that 
defines its options as strings.  You are expecting your selected option 
to be an integer.
The NumberFormatException is coming from OGNL rightfully failing to 
coerce your strings into integers.

Since Tapestry doesn't include an IPropertySelectionModel implementation 
of that returns integer options, you'll probably want to define your own 
- something like this perhaps:

public IPropertySelectionModel getPriorityModel()
{
    return new IPropertySelectionModel()
    {
       private String[] priorities = new String[] { prio_L, prio_M, 
prio_H };

       public String getLabel(int index)
       {
          return getMessage(this.priorities[index]);
       }

       public Object getOption(int index)
       {
          return new Integer(index);
       }

       public String getValue(int index)
       {
          return Integer.toString(index);
       }

       public int getOptionCount()
       {
          return this.priorities.length;
       }

       public Object translateValue(String value)
       {
          return Integer.valueOf(value);
       }
    }
}

If this IPropertySelectionModel is a implementation that you use 
elsewhere, you'll want to generalize it into a reusable object.

Since your newsItem prorities appear to be bound to a finite set of 
values, you may also want to consider using an Enum to represent your 
newsItem priority.  That way, you'd be able to use the 
EnumPropertySelectionModel.

Paul

luc.peerdeman@centerparcs.com wrote:

>
>
>Hi,
>
>I am a bit puzzled by the behaviour of PropertySelection component. I am
>using a simple StringPropertySelectionModel defined in the page like so:
>
>/**
> * Returns a set of priorities that the user may choose from.
> */
>public IPropertySelectionModel getPriorityModel() {
>      return new StringPropertySelectionModel(
>            new String[] {
>                  getMessage( prio_L ),
>                  getMessage( prio_M ),
>                  getMessage( prio_H ) } );
>}
>
>and use like so:
>
><select jwcid="prioritySelection@PropertySelection"
>      model="ognl:getPriorityModel()"
>      value="ognl:newsItem.priority"/>
>
>newsItem.priority is an integer. I understood from the
>StringPropertySelectionModel documentation that the value set/returned
>would be a simple index into the array of Strings. However, when I submit
>the form, I get a java.lang.NumberFormatException; apparently Tapestry is
>trying to convert the label value to an integer:
>
>Unable to update expression 'newsItem.priority' for
>NewsItemView_New01a$Enhance_254@4369905b[NewsCRUDPage/theNewsItemEdit_New01_1]
> to medium
>
>I checked the source of the page, and the select seems to be ok, it
>contains the numeric values I expected:
>
><select name="prioritySelection">
><option value="0">low</option>
><option value="1">medium</option>
><option value="2">high</option>
></select>
>
>Another thing I would have expected is that the PropertySelection picked up
>the current value from the newsitem when rendered.
>
>Can anybody give me a hint as to what I am missing here ?
>
>Thanks in advance, Luc.
>
>
>Disclaimer
>
>The information contained in this message and any attachments:
>
>does not constitute an offer or an acceptance of an offer or a
>representation or warranty, nor shall it form any part of a legally binding
>contract;
>has been compiled with care but no representation or warranty (express or
>implied) is made as to its accuracy or completeness and no liability can be
>accepted for any loss arising from the use of any of the information;
>may include opinions or views, which unless expressly stated otherwise, are
>not those of the company or any person in relation to whom the company
>would have vicarious liability or responsibility;
>is not guaranteed to be free from any so-called computer viruses and it is
>strongly recommended that you check for such viruses before downloading it
>to your computer equipment;
>may include hypertext links to the sites of other companies or persons, the
>content of which this company has no control over and accepts no liability
>for;
>is for the exclusive use of the addressee and may contain confidential,
>privileged, work product immunity and/or non-disclosable information.  If
>you are not the intended recipient and receive this message and any
>attachments, you must not read, copy, transmit, disclose or otherwise use
>any of them (or part of them) in any way (to do so may be unlawful) and we
>would be grateful if you could inform us immediately of any such receipt
>and subsequently entirely and permanently erase the message and any
>attachments from your equipment.
>
>E-mail is an informal method of communication and is subject to possible
>data corruption.  For these reasons it will normally be inappropriate to
>rely upon information contained in an e-mail without obtaining tangible
>written confirmation of it.
>
>
>---------------------------------------------------------------------
>To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
>For additional commands, e-mail: tapestry-user-help@jakarta.apache.org
>
>  
>


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