• Post Reply Bookmark Topic Watch Topic
  • New Topic

JSTL inside tag

 
Samuel Lugo
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

Is it legal to put a conditional JSTL tag inside an opening option tag? For instance:

<option <c:if test="${userProfile.state == 'AK'}">selected="selected"</c:if>>AK</option>

Thanks for the help.

lugos
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65530
108
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What happened when you tried it?
 
Samuel Lugo
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It did work, but I wanted to make sure it was legal. I'm fairly new to JSTL and I don't want any suprises down the road.

Thanks,
lugos
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65530
108
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The larger view is to keep in mind that from the viewpoint of the JSP engine, HTML markup tags are not seen as tags at all; just as semantic-less template text. So whether server-side tags, JSTL or otherwise, are embedded anywhere in HTML is not an issue.
 
Stefan Evans
Bartender
Posts: 1822
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes this approach works.
The only thing I have against it is that it is a little difficult to read.

All of that code just to create "selected" against the correct item.

Myself I prefer the use of a taglib for this (eg the struts html tags - html:select, html ption, html ptions) It just makes it a bit neater.

The other less nice solution, but which makes for more readable code is to put some javascript on the page to set the value of the dropdown box after it is created:
ie something like
<script language="javascript">
document.forms[0].mySelect.value = '${userProfile.state}';
</script>

I don't really like that solution either, but to me it is a bit clearer than all of those tests for each option in the list.

Cheers,
evnafets
 
Samuel Lugo
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Makes sense. Thank you very much.

lugos
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65530
108
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In JSP 2.0 simple EL expressions can be used in placed of a cumbersome tag.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!