gospodin
gospodin

Reputation: 1201

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

Upvotes: 2

Views: 5082

Answers (2)

jeevanragula
jeevanragula

Reputation: 62

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/

Upvotes: 0

Wolfgang Fahl
Wolfgang Fahl

Reputation: 15769

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

Upvotes: 1

Related Questions