Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I produce a select tag using JSTL or Standard Actions in a JSP

Tags:

java

html

jsp

jstl

I want to make a select tag in a JSP, where the options are an Enumeration (for example, all US States). Is their a tag in JSTL or a standard tag that can do this, without manually iterating through the list?

like image 811
Eric Wilson Avatar asked Feb 10 '10 13:02

Eric Wilson


People also ask

What is the use of JSTL tags in JSP?

JSTL stands for JSP Standard Tag Library. JSTL is the standard tag library that provides tags to control the JSP page behavior. JSTL tags can be used for iteration and control statements, internationalization, SQL etc.

What is the difference when using JSTL and JSP?

JSP lets you even define your own tags (you must write the code that actually implement the logic of those tags in Java). JSTL is just a standard tag library provided by Sun (well, now Oracle) to carry out common tasks (such as looping, formatting, etc.).

Which of the following declaration will add JSTL SQL library in your JSP?

Install JSTL Library To use any of the libraries, you must include a <taglib> directive at the top of each JSP that uses the library.

What tag is used in JSTL to show the output?

The <c:out> tag is similar to JSP expression tag, but it can only be used with expression. It will display the result of an expression, similar to the way < %=... % > work.


2 Answers

Certainly, in JSTL (just drop jstl-1.2.jar in /WEB-INF/lib) there's the c:forEach tag. You'll only have to convert the (old fashioned) Enumeration to a modern List or perhaps Enum if it's hardcoded in Java. You can if necessary grab Collections#list() for this if the Enumeration is to be obtained from an unchangeable 3rd party API.

Here's a demo how the <c:forEach> can then be used:

<select name="country">
   <c:forEach items="${countries}" var="country">
       <option value="${country.code}" ${param.country eq country.code ? 'selected' : ''}>${country.name}</option>
   </c:forEach>
</select>

The ${countries} should refer a List<Country> or Country[] which has been put in any of the page, request, session or application scopes — of which the application scope is the most straightforward choice, as a list of countries is supposed to be an application wide constant. You could use a ServletContextListener to load it once and put in application scope on application's startup. The Country is in this example just a Javabean (model) class with at least two properties.

See also:

  • Populating cascading dropdown lists in JSP/Servlet
  • Set selected option on existing select tag with jstl
  • Using enum in JSTL - Can I access the values of an enum class from a JSP using EL?
like image 168
BalusC Avatar answered Oct 06 '22 00:10

BalusC


There isn't in JSTL. However many frameworks provide such additional tags:

  • Struts2 - <s:select>

  • Spring MVC - <form:select>

  • JSF - <h:selectOneMenu>
like image 35
Bozho Avatar answered Oct 06 '22 00:10

Bozho