Ashish Kulkarni wrote the following on 1/11/2006 11:08 AM:
Hi
I am a bit confused about using <html:select> and
<html:optionCollection> tag

I hate that optionsCollection tag:) I think the naming of the attributes is confusing. I prefer using html:select and using html:option withing a JSTL forEach loop. Slightly more verbose than optionsCollection, but I find it easy to understand. As an example:

<html:select property="departmentId">
    <c:forEach var="dept" items="${departments}">
        <html:option value="${dept.id}">
            <c:out value="${dept.description}"/>
        </html:option>
    </c:forEach>
</html:select>

Suppose i have defined following in struts-config.xml
file
<form-bean name="viewlinephase"
type="org.apache.struts.action.DynaActionForm">
<form-property name="selectedView"
type="java.lang.String" />
<form-property name="viewNames"
type="java.util.Vector" />
</form-bean>
I have a bean where in i have get and set method for
name and desctiption, suppose the bean is called
MyBean and viewNames vector is populated by MyBean
class

How do i define it in jsp page, what i want is some thing like this in html page
<SELECT name="selectedView" size="4">
        <OPTION value="test">
        test data
        </OPTION>
        <OPTION value="test1" selected>
        test dtaa 1
        </OPTION>
</SELECT>

<html:select property="selectedView">
    <c:forEach var="viewName" items="${viewNames}">
        <html:option value="${viewName}">
            <c:out value="${viewName}"/>
        </html:option>
    </c:forEach>
</html:select>

Using html:select and html:option will take care of marking the correct option selected.. assuming your case the value of selectedView matches the viewName.

I left them both the same for viewName but in reality you might be submitting a different value than what is being displayed as the option. I couldn't tell that by what you provided though.


More than like you might have something like a Collection of View objects with and id and a name in them (by the way, you probably don't want to use Vector for your Collection). Assuming a View object as described, you'd want:

<html:select property="selectedViewID">
    <c:forEach var="view" items="${viewItems}">
        <html:option value="${view.id}">
            <c:out value="${view.name}"/>
        </html:option>
    </c:forEach>
</html:select>


To use JSTL download the standard and jstl jars http://cvs.apache.org/builds/jakarta-taglibs/nightly/, throw them in your web-inf/lib dir then to use the c tag, on the top of this JSP put:

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"; %>


--
Rick

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to