You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Ashish Kulkarni <ku...@yahoo.com> on 2006/01/11 17:08:59 UTC

tag question

Hi
I am a bit confused about using <html:select> and
<html:optionCollection> tag 
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>

Where test is name and test data1 is description in
MyData bean. also data value selectedView should be
the selected value in option
I have looked the examples supplied but still could
not figure out how it works

Ashish


__________________________________________________
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 
http://mail.yahoo.com 

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: tag question

Posted by Rick Reumann <st...@reumann.net>.
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: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org