You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@velocity.apache.org by Jianqun Wang <wa...@un.org> on 2004/01/24 00:15:36 UTC

Form Value

Hi,

I am trying to write a velocity macro for rendering drop down with the
following code:


##$bean -- the struts formbean name
##$name -- the name of the select in form
## labels -- drop down labels
## values -- drop down values
## initialValue -- the default value for the drop down

#macro (dropdownBean $bean $name $labels $values $initialValue)

  #set ($s = $bean.get($name))
  #if(!$s)
     #set ($s = $initialValue)
  #end

  #set ($s = $initialValue)
  #set($i=0 )
  <select name="$name">
  #foreach ($v in $values)
  <option value="$v" #if("$v"=="$s") selected="selected" #end>$!labels.get(
$i)</option>
  #set($i=1+$i)
  #end
  </select>
#end

What I am trying to do in this macro is to rendering the drop down with the
initial value ($initialValue) first. But if there is value carried back
from a formbean (Struts form bean) $bean, the drop down should be rendered
with the value in the formbean. When I run it, it always is rendered with
the initial value, and the value $bean.$name in my file which the drop down
is rendered is something like this: myformbean@1832c.fieldName. Can anybody
help me out?

Thanks,

Jane










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


Re: Form Value

Posted by Mike Kienenberger <mk...@alaska.net>.
Jianqun Wang <wa...@un.org> wrote:
> Hi,
> 
> I am trying to write a velocity macro for rendering drop down with the
> following code:
> 
> 
> ##$bean -- the struts formbean name
> ##$name -- the name of the select in form
> ## labels -- drop down labels
> ## values -- drop down values
> ## initialValue -- the default value for the drop down
> 
> #macro (dropdownBean $bean $name $labels $values $initialValue)
> 
>   #set ($s = $bean.get($name))
>   #if(!$s)
>      #set ($s = $initialValue)
>   #end
> 
>   #set ($s = $initialValue)
>   #set($i=0 )
>   <select name="$name">
>   #foreach ($v in $values)
>   <option value="$v" #if("$v"=="$s") selected="selected" #end>
$!labels.get(
> $i)</option>
>   #set($i=1+$i)
>   #end
>   </select>
> #end
> 
> What I am trying to do in this macro is to rendering the drop down with 
the
> initial value ($initialValue) first. But if there is value carried back
> from a formbean (Struts form bean) $bean, the drop down should be rendered
> with the value in the formbean. When I run it, it always is rendered with
> the initial value, and the value $bean.$name in my file which the drop 
down
> is rendered is something like this: myformbean@1832c.fieldName. Can 
anybody
> help me out?


In my experience, I've found this pattern of code works best. (It's from one 
of my projects.)
Note that the value of the form is an index into $bankList (or "No 
Selection" if there's no index item preselected)
You'll want to adjust your velocityCount to start at 0 and not 1 (or handle 
it in your code properly).

<select name="bankIndex">
	<option value="No Selection"#if("No 
Selection"=="${chooseBankForm.bankIndex}") selected="selected"#end>
${msg.get('label.chooseBankPullDownDefault')}</option>
#foreach ($currentBank in $bankList)
	<option 
value="${velocityCount}"#if("$velocityCount"=="${chooseBankForm.bankIndex}") 
selected="selected"#end>${currentBank.getBankName()}</option>
#end## ($currentBank in $bankList)
</select>

By the way, this is really a question for velocity-user.  I've cc'd this to 
velocity-user.  Please remove references to velocity-dev in any responses.

-Mike

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


Re: Form Value

Posted by Mike Kienenberger <mk...@alaska.net>.
Jianqun Wang <wa...@un.org> wrote:
> Hi,
> 
> I am trying to write a velocity macro for rendering drop down with the
> following code:
> 
> 
> ##$bean -- the struts formbean name
> ##$name -- the name of the select in form
> ## labels -- drop down labels
> ## values -- drop down values
> ## initialValue -- the default value for the drop down
> 
> #macro (dropdownBean $bean $name $labels $values $initialValue)
> 
>   #set ($s = $bean.get($name))
>   #if(!$s)
>      #set ($s = $initialValue)
>   #end
> 
>   #set ($s = $initialValue)
>   #set($i=0 )
>   <select name="$name">
>   #foreach ($v in $values)
>   <option value="$v" #if("$v"=="$s") selected="selected" #end>
$!labels.get(
> $i)</option>
>   #set($i=1+$i)
>   #end
>   </select>
> #end
> 
> What I am trying to do in this macro is to rendering the drop down with 
the
> initial value ($initialValue) first. But if there is value carried back
> from a formbean (Struts form bean) $bean, the drop down should be rendered
> with the value in the formbean. When I run it, it always is rendered with
> the initial value, and the value $bean.$name in my file which the drop 
down
> is rendered is something like this: myformbean@1832c.fieldName. Can 
anybody
> help me out?


In my experience, I've found this pattern of code works best. (It's from one 
of my projects.)
Note that the value of the form is an index into $bankList (or "No 
Selection" if there's no index item preselected)
You'll want to adjust your velocityCount to start at 0 and not 1 (or handle 
it in your code properly).

<select name="bankIndex">
	<option value="No Selection"#if("No 
Selection"=="${chooseBankForm.bankIndex}") selected="selected"#end>
${msg.get('label.chooseBankPullDownDefault')}</option>
#foreach ($currentBank in $bankList)
	<option 
value="${velocityCount}"#if("$velocityCount"=="${chooseBankForm.bankIndex}") 
selected="selected"#end>${currentBank.getBankName()}</option>
#end## ($currentBank in $bankList)
</select>

By the way, this is really a question for velocity-user.  I've cc'd this to 
velocity-user.  Please remove references to velocity-dev in any responses.

-Mike

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