我的 struts Action 类出错
我有一个 JSP,其中显示数据库中的日期和描述。每个条目都有一个唯一的ID,但我没有显示在页面上(显示复选框) 这些条目是使用“逻辑:迭代”抛出的,因此行数始终根据条目而变化。 现在这些字段显示为文本字段,以便用户还可以更新日期或描述。左侧有一个复选框,因此 用户可以选择他们想要更新的所有值。记住上面的逻辑:迭代,复选框必须使用名称和定义 不能有id。
...
...
<logic:notEmpty name="specialResult" scope="request">
<logic:iterate name="specialResult" id="specialResult" indexId="index">
<tr align="center">
<td width="15%">
<input type="checkbox" name="upisActive" property="upisActive"
value="<bean:write name="specialResult" property="upId"/>"></input></td>
<td width="15%"><input type="text" name="upDate" value="<bean:write name="specialResult" property="upDate"/>"
property="upDate" size="20" class="Date" id="Date"></input></td>
<td width="15%"><input type="text" name="upDesc" value="<bean:write name="specialResult" property="upDesc"/>"
property="upDesc" size="20" id="Desc"/></td>
</tr>
</logic:iterate>
...
我的错误是,如果我有三行,并且我想更新第三行并选择第三个复选框。我的 Action 类正在检索第一行日期和描述。 如何编辑我的操作类以检索选中的复选框的值?
public ActionForward class(ActionMapping mapping, ActionForm theForm,
HttpServletRequest request, HttpServletResponse response) throws IOException,
SQLException, ServletException
{
Connection conn = null;
Service Serv = new Service();
List updList = new ArrayList();
Form upForm = (Form) theForm;
String[] values = request.getParameterValues("upisActive");
try
{
conn = getConnection(request, false);
for (int i=0;i<values.length;i++){
VO hdvo = new VO(); //Vo class with getters and setters
val = values[i];
hdvo.setDate(upForm.upDate[i]);
hdvo.setDesc(upForm.upDesc[i]);
updList.add(hdvo);
}
hdServ.updTest(updList, conn);
...
I have a JSP where I am showing date and description from database. Every entry has a unique id, but I am not showing on the page(showing checkbox)
These entries are thrown using a "logic:iterate", so the number of rows is always changing based on entries.
Now these fields are shown as a text field so that the user can also update the date or description. A checkbox is to the left so the
user can select what all values they want to update. Remember the logic:iterate above, the checkbox has to be defined using name and
cannot have id.
...
...
<logic:notEmpty name="specialResult" scope="request">
<logic:iterate name="specialResult" id="specialResult" indexId="index">
<tr align="center">
<td width="15%">
<input type="checkbox" name="upisActive" property="upisActive"
value="<bean:write name="specialResult" property="upId"/>"></input></td>
<td width="15%"><input type="text" name="upDate" value="<bean:write name="specialResult" property="upDate"/>"
property="upDate" size="20" class="Date" id="Date"></input></td>
<td width="15%"><input type="text" name="upDesc" value="<bean:write name="specialResult" property="upDesc"/>"
property="upDesc" size="20" id="Desc"/></td>
</tr>
</logic:iterate>
...
My error is that if I have three rows and I want to update third row and select third checkbox. My Action class is retrieving the first row date and desc.
How can I edit my action class to retrieve the value against the checked checkboxes?
public ActionForward class(ActionMapping mapping, ActionForm theForm,
HttpServletRequest request, HttpServletResponse response) throws IOException,
SQLException, ServletException
{
Connection conn = null;
Service Serv = new Service();
List updList = new ArrayList();
Form upForm = (Form) theForm;
String[] values = request.getParameterValues("upisActive");
try
{
conn = getConnection(request, false);
for (int i=0;i<values.length;i++){
VO hdvo = new VO(); //Vo class with getters and setters
val = values[i];
hdvo.setDate(upForm.upDate[i]);
hdvo.setDesc(upForm.upDesc[i]);
updList.add(hdvo);
}
hdServ.updTest(updList, conn);
...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题在于您如何设置页面。所有复选框都具有相同的名称(标准设置),但 upDate 和 upDesc 字段也设置为相同的名称。
这意味着当您提交表单时,在服务器上您将获得(考虑您的示例)一个包含 3 个 upDate 值的列表、一个包含 3 个 upDesc 值的列表和一个列表3 个
upisActive
复选框。嗯……不完全是!问题在于您的复选框和用于读取要更新的值的代码。
首先,如果未选中复选框,则不会根据请求发送复选框。这意味着,根据您的选择,您将在服务器上获得长度为 0、1、2 或 3 的
upisActive
值列表。服务器上获得以下代码:
其次,您将在 例如,您选中第三个复选框并提交表单。这意味着
String[] 值
的长度将为 1,因为只有选定的复选框才会发送到服务器。但输入字段始终在 3upDesc 和 3upDate 中发送。然后循环它(一次)并提取
upForm.upDate[0]
和upForm.upDesc[0]
。这样,您可以通过选中第三个复选框来更新第一行。其他问题:
1) 您在以下代码中使用了相同的标识符(这是自找麻烦):
2) 您使用的是经典输入并为其添加了
property
属性:3) 不确定浏览器是否保证每次都会以精确匹配的顺序发送字段,因此我猜使用单个计数器只是“希望”相同的顺序。
4)另外,请阅读此
The problem is with how you've set up your page. You have all checkboxes with the same name (standard setup) but you also have the
upDate
andupDesc
fields also set up with the same name.That means that when you submit your form, on the server you will get (considering your example) a list of 3
upDate
values, a list of 3upDesc
values and a list of 3upisActive
checkboxes. Well... not quite!The problem is your checkboxes and the code you use to read the values to update.
First, checkboxes are not sent on the request if they are not checked. That means that, depending on your selection, on the server you will get a list of
upisActive
values of length 0, 1, 2 or 3.Second, you then have this code on the server:
In you example, you check the third checkbox and submit your form. That means that
String[] values
will be of length 1 because only the selected checkbox is send to the server. But the input fields are always sent in 3upDesc
and 3upDate
.Then you loop-it (once) and extract
upForm.upDate[0]
andupForm.upDesc[0]
. This way, you update the first row by checking the third checkbox.Other problems:
1) You have used the same identifier in the following code (it's asking for trouble):
2) You are using classic inputs and added
property
attribute to it:3) Not sure that the browser guaranties that the fields will be sent in the exact matching order each time, so using a single counter is, I guess, just "hoping" for the same order.
4) Also, read this