0

With this drop down I'll select a value and pass in a url, but with this code i'm not able to get only the selected one, can you guys help me to setup a c:if and mark selected on the side?. Thanks anyway.

<select>
<c:forEach var="foo" items="${bar}">
<option value="${foo}">${foo}</option>
</c:forEach></select>


<div class="links" style="font-size: 110%">
<a href="someURL?path=${foo}">someName</a>
</div>

Ex:

<select>
<c:forEach var="name" items="${names}">
<option value="${name}">${name}</option>
</c:forEach></select>

After i select the name, place the selected one in the url

<div class="links" style="font-size: 110%">
<a href="someURL?path=${name}">someName</a>
</div>

Sorry, my english isn't that good so it's hard sometimes to express myself

Bruno Andrade
  • 253
  • 3
  • 8
  • 17

2 Answers2

1
<option value="${foo}" 
        <c:if test="${param.path == foo}">selected="selected"</c:if>>${foo}</option>

Read http://docs.oracle.com/javaee/1.4/tutorial/doc/JSPIntro7.html

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
  • the thing is this ${bar} is a list, so when i'm putting 2 items or more i got this [something1,something2] – Bruno Andrade Dec 14 '12 at 08:47
  • I don't understand your question. You're sending a request to a URL like `someURL?path=hello`. And in response to this request, you want to display a select box containing all the items from a list called `bar`, and select the item in bar which is equal to `hello`, right? If so, that's what my answer above does. If not so, please edit your question and add more details. – JB Nizet Dec 14 '12 at 08:52
  • what i want is select a item in the drop down and put this selected item in the url so i can send as a param to another controller – Bruno Andrade Dec 14 '12 at 08:57
  • 1
    Well, just submit the form containing this select box, and the selected option value will be sent as a parameter. If you really want to modify a link in the same page as the select box each time an option is selected, then the JSTL is irrelevant: you need to use JavaScript. – JB Nizet Dec 14 '12 at 09:04
-1

u can use javascript traversal to get selected value,if u do not use this ,u can use request.setAttribute("","") to set value ,<%= request.getAttribute("","")%> to get value , <% for(){}%> ,javascript to get selected value.

zhangy
  • 1
  • 3