You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@velocity.apache.org by Mike Kienenberger <mk...@alaska.net> on 2004/01/24 01:52:57 UTC

Re: Form Value

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