转自:http://www.theserverside.com/discussions/thread.tss?thread_id=35726
Render default value for html:select in STRUTS
Posted by: Jiao Yu on ?? 07, 2005 DIGG
Hello,
I am trying to render the default value for html:select with STRUTS, but can't get it work correctly.
----------------------------------------------------------
Here are the component I have:
The JSP page to render the html:select
<%@ taglib prefix="req" uri="/WEB-INF/taglibs-request.tld" %>
<%@ taglib uri="/WEB-INF/c.tld" prefix="c" %>
<%@ taglib prefix="fmt" uri="/WEB-INF/fmt.tld" %>
<%@ taglib uri="/WEB-INF/struts-tiles.tld" prefix="tiles" %>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<head><title>eQuation 2.0</title></head>
<body>
<html:form action="/mappingAction" >
<table>
<c:forEach var="line" items="${mappingForm.map.matchColumnList}" >
<TR>
<TD><c:out value="${line.databaseColumnNameLabel}"/></TD>
<TD><c:out value="${line.excelColumnPosition}"/></TD>
<TD>
<html:select name="line" property="excelColumnPosition"
multiple="false" size="5" value="${line.excelColumnPosition}" >
<html:option value="" >No Match</html:option>
<html:optionsCollection name="mappingForm"
property="excelColumnList"
value="index"
label="columnName"/>
</html:select>
</TD>
</TR>
</c:forEach>
</table>
<html:submit value="Process"/>
</html:form>
</body>
----------------------------------------------------------
Here is the form setction for my STRUTS configuration xml:
<form-bean name="mappingForm" type="org.apache.struts.action.DynaActionForm" >
<form-property name="excelColumnList" type="rawdata.model.ExcelColumnNameBean[]"/>
<form-property name="matchColumnList" type="rawdata.model.ColumnMatchBean[]" />
----------------------------------------------------------
Here are the definitions for the two beans used in the mappingForm:
public class ColumnMatchBean {
private String excelColumnName;
private String recommmendedDatabaseColumnName;
private String databaseColumnNameLabel;
private int excelColumnPosition;
...}
public class ExcelColumnNameBean {
private String columnName;
private int index;
...}
----------------------------------------------------------
Here is the action mapping used to populate the mappingForm and render the form in a JSP:
<action
path="/loadMatchingColumn"
type="rawdata.LoadMatchingColumnAction"
name="mappingForm"
attribute="mappingForm"
scope="session"
>
<forward name="success"
path="/../upload/showExcelColumns.jsp"
redirect="false"/>
</action>
----------------------------------------------
In LoadMatchingColumnAction, I populated both properties of the mappingForm,
For excelColumnList ( Array of ExcelColumnNameBean), I populated a couple of ExcelColumnNameBean ( with both columnName and index populated),
For matchColumnList ( Array of ColumnMatchBean, I populated a couple of ColumnMatchBean, ( with only recommmendedDatabaseColumnName and databaseColumnNameLabel populated),
Then I the showExcelColumns.jsp, I am going to render a couple of select lists with corresponding labels, see the details of the JSP at the beginning of this POST.
Here my questions come,
1)I can render the couple of select lists successfully, but for each of them, I would like to set a default value, I can't make this to work, although I set the value attribute in the select tag:
<html:select name="line" property="excelColumnPosition"
multiple="false" size="5" value="${line.excelColumnPosition}" >
I wrap select tag with C:foreach tags to render a group of select lists, and the problem seems to happen in
value="${line.excelColumnPosition}"
the correct predefined value can't be retrieved,
If I change "${line.excelColumnPosition}" to a specific number, let's say "2", then it works fine.
How can I get the correct value from line.excelColumnPosition?
2)My second question is: the select list rendered in the JSP page doesn't render a drop down list, but a select box, how can I make it a select drop down list?
Hope I have provided enough and clear information to explain my puzzles?
Thanks so much and have a good night,
Jiao
Message #180699 Post reply Post reply Post reply Go to top Go to top Go to top
Check html:optionsCollection
Posted by: Cliff Liang on ?? 08, 2005 in response to Message #180569
I think you check the following and its tag doc first.
<html:optionsCollection name="mappingForm"
property="excelColumnList"
value="index"
label="columnName"/>
I guess each item in excelColumnList is the pair of index and value. You set value="index", so when you change "${line.excelColumnPosition}" to "2", it works fine.
Try to change to value="value", which is the value of excelColumnPosition is from.
Of course, html:select can be used for drop down list.
Hope it can help you.
Cliff Liang
Message #180705 Post reply Post reply Post reply Go to top Go to top Go to top
RE:Render default value for html:select in STRUTS
Posted by: Jiao Yu on ?? 09, 2005 in response to Message #180569
Cliff,
Thanks for your reply. But I don't think that's the problem, since I already identified the issue under the help of a teammate.
The major issue here is that STRUTS doesn't work well with EL, so ${line.excelColumnPosition}" doesn't work well.
He suggests following options to fix the problem:
a)You must use runtime expression in the value parameter. I have not tested following code.
<bean:define id="defaultValue" name="line" property="excelColumnPosition"/>
<html:select name="line" property="excelColumnPosition" value='<%= defaultValue %>' >
b) Use Struts-EL library
c) Tomcat bundled with JBoss 4.0.2 is 5.5.x. It is Servlet 2.4 and JSP 2.0 compliant. You could change the web.xml to specify the 2.4 version of the servlet specification. EL expresions would be interpreted by the servlet container - Tomcat. The version 2.2 in web.xml does not interpret EL expressions for compatibility reasons.
Changing follow code:
“<!DOCTYPE web-app
PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN"
"http://java.sun.com/j2ee/dtds/web-app_2_2.dtd">”
To
<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
version="2.4">
....
</web-app>
------------------------------------------
To dropdown list issue is also resolved, just omit the multiple attribute for select.
Thanks again and happy programming.
Jiao
Message #180707 Post reply Post reply Post reply Go to top Go to top Go to top
Render default value for html:select in STRUTS
Posted by: Jiao Yu on ?? 09, 2005 in response to Message #180569
Hello,
Another tricky thing is :
The var attribute (mathchColumnList)in C:forEach tag, the property after map. for the items attribute (matchColumnList) in the C:foeEach tag, and the name attribute for the html:select tag should be all the same, otherwise, you can't get the POST data in the following action.
<c:forEach var="matchColumnList" items="${mappingForm.map.matchColumnList}" >
<TR>
<TD><c:out value="${matchColumnList.databaseColumnNameLabel}"/></TD>
<TD><c:out value="${matchColumnList.excelColumnPosition}"/></TD>
<TD>
<html:select name="matchColumnList" property="excelColumnPosition"
multiple="false" size="5" value="${matchColumnList.excelColumnPosition}" >
<html:option value="" >No Match</html:option>
<html:optionsCollection name="mappingForm"
property="excelColumnList"
value="index"
label="columnName"/>
</html:select>
Have a good night,
Jiao