JSP 列表框和 Servlet
在我的应用程序中,我使用 2 个列表框,我想将所选项目从一个列表框移动到另一个列表框。我知道将数据库中的值分配给列表框。但我不知道如何将 java 文件中的字符串数组值分配给 html 字段。在我的“record.java”中,我有以下代码:
public class Report
{
private static String[] types = {
"Value1",
"Value2"
};
private static String[] fields = {
"number1",
"number2"
};
public static String[] getList() {
return types;
}
public static String getFieldName(String description) {
for(int i=0; i< fields.length; i++) {
if (description.compareToIgnoreCase(types[i]) ==0)
return fields[i];
}
return "";
}
}
我的“chart.jsp”文件如下:
<form method="post">
<fieldset>
<legend>Chart Data</legend>
<br/>
<br/>
<table >
<tbody>
<tr>
<td>
<select name="data" size="5" id="s">
<option value=""></option>
</select>
</td>
<td>
<input type="submit" value="<<"/>
</td>
<td>
<select name="data" size="5" id="d">
<option value=""></option>
</select></td>
</tr>
</tbody>
</table>
<br/>
</fieldset>
<input class="submit" type="submit" value="Submit" />
</form>
我是 JSP 新手。任何人都可以帮助我如何做到这一点吗? 谢谢....
In my application, am using 2 list boxes where i want to move selected items from one to another. I know to assign values to list box from database. But i don't know how to assign string array value from java file to html field. In my 'record.java' i have following code:
public class Report
{
private static String[] types = {
"Value1",
"Value2"
};
private static String[] fields = {
"number1",
"number2"
};
public static String[] getList() {
return types;
}
public static String getFieldName(String description) {
for(int i=0; i< fields.length; i++) {
if (description.compareToIgnoreCase(types[i]) ==0)
return fields[i];
}
return "";
}
}
and i have my 'chart.jsp' file as follows:
<form method="post">
<fieldset>
<legend>Chart Data</legend>
<br/>
<br/>
<table >
<tbody>
<tr>
<td>
<select name="data" size="5" id="s">
<option value=""></option>
</select>
</td>
<td>
<input type="submit" value="<<"/>
</td>
<td>
<select name="data" size="5" id="d">
<option value=""></option>
</select></td>
</tr>
</tbody>
</table>
<br/>
</fieldset>
<input class="submit" type="submit" value="Submit" />
</form>
I am new to JSP. Can any one help me how to do this?
Thank You....
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
getter 方法不应该是静态的:
Report
的实例应该放在 servlet 的doGet()
方法的请求范围内:这样它就可以在 JSP 中使用EL 为
${report}
,列表以${report.list}
形式提供。您可以使用 JSTLc:forEach
来迭代数组或List
。请注意,您不应为独立的输入元素指定相同的名称。
The getter method should not be static:
An instance of
Report
should be placed in request scope indoGet()
method of the servlet:This way it'll be available in JSP EL as
${report}
and the list is available as${report.list}
. You can use JSTLc:forEach
to iterate over an array or aList
.Note that you should not give independent input elements the same name.