preselect select field with freemarker

In my struts2 action that prepares the ftl page i have

private static List<Product> listOfProducts;

with getter and setters. This list is filled with products. First product in the list has type B.

In ftl page I am iterating over the list of products

<#list listOfProducts as product>
<select name = product[0].type>
  <option value="A">fistType</option>
  <option value="B">secondType</option>
  <option value="C">thirdType</option>
</select>
</#list>

Problem is that firstType is preselected each time even if in the list i have a product with type B.

Can you tell me what am i missing here? Why option B was not selected when the ftl is loaded?

Thanks


See http://www.w3schools.com/tags/tag_select.asp on the correct syntax for a select

The attribute "name" sets the name of the control - it does not influence the selection

See How can I set the default value for an HTML <select> element? on how to do that


Use Struts select tag.

<@s.select theme="simple" name="selectedProduct" list="listOfProducts" listKey="productId" listValue="productName" value="defaultProduct"/>

Please go through the example in below link for more understanding.

http://www.mkyong.com/struts2/struts-2-sselect-drop-down-box-example/

链接地址: http://www.djcxy.com/p/88094.html

上一篇: 设置默认选中

下一篇: 用freemarker预选选择字段