Struts2 复选框在 Action 类中返回值
我对 struts2 还很陌生,所以我试图发布您需要了解我的问题的所有内容,非常感谢您的帮助。尽量说得更清楚,我对这个问题缺乏想法,即使它听起来像一个经典。
<s:checkbox name="selectedIndex" theme="simple" >
</s:checkbox>
我的 JSP 中有动态数量的复选框。假设我的 jsp 中有三个复选框。然后,如果用户选择第一个和第二个复选框,那么我想要操作类中的一个数组,其元素为 {1,2}。如果用户选择第一个和第三个复选框,然后我想要一个动作类中的数组,其元素是{1,3}。我如何在struts2中做到这一点?
我在action类中有getter和setter:
public String[] getSelectedIndex() {
return selectedIndex;
}
public void setSelectedIndex(String[] selectedIndex) {
this.selectedIndex = selectedIndex;
}
所以actionClass给我true和false。
在下面的代码中,List1(存储在会话对象中)是 StatusDTO 列表,下面是 getter、setter StatusDTO 的元素是 :-
public boolean getIsDisabled()
{
return this.isDisabled;
}
public void setIsDisabled(boolean isDisabled)
{
this.isDisabled=isDisabled;
}
public int getSerialNo()
{
return this.serialNo;
}
public void setSerialNo(int serialNo)
{
this.serialNo=serialNo;
}
jsp 中的实际代码是
<%
ArrayList temp=(ArrayList)ActionContext.getContext().getSession().get("List1");
Iterator itr=temp.iterator();
while(itr.hasNext())
{
StatusDTO psd=(StatusDTO)itr.next();
System.out.println("********"+psd.getSerialNo());
%>
<tr>
<td bgcolor="#E6FAFB">
<%=psd.getSerialNo()%>
</td>
<td bgcolor="#E6FAFB">
<%
if(psd.getIsDisabled())
{
%>
<s:checkbox name="selectedIndex" theme="simple" disabled="true">
</s:checkbox>
<%
}
else
{
%>
<s:checkbox name="selectedIndex" theme="simple" value="1">
</s:checkbox>
<%}
%>
</td>
</tr>
<% }
%>
Im pretty new to struts2, so Im trying to post everything you need to understand my problem,Your help is much appreciated. try to be more clear, i'm in lack of ideas in this problem, even it sounds like a classic .
<s:checkbox name="selectedIndex" theme="simple" >
</s:checkbox>
I have dynamic number of checkboxes in my JSP.Lets say i have three checkbox in my jsp.Then if user select first and second checkbox then i want a array in action class whose element is {1,2}.If user select first and third checkbox then i want a array in action class whose element is {1,3}.How can i do it in struts2?
i have getter and setter in action class as:
public String[] getSelectedIndex() {
return selectedIndex;
}
public void setSelectedIndex(String[] selectedIndex) {
this.selectedIndex = selectedIndex;
}
so actionClass give me true and false.
In the followin code List1(Stored in session object) is list of StatusDTOs and getter,setter are following
element of StatusDTO is
:-
public boolean getIsDisabled()
{
return this.isDisabled;
}
public void setIsDisabled(boolean isDisabled)
{
this.isDisabled=isDisabled;
}
public int getSerialNo()
{
return this.serialNo;
}
public void setSerialNo(int serialNo)
{
this.serialNo=serialNo;
}
Actual code in jsp is
<%
ArrayList temp=(ArrayList)ActionContext.getContext().getSession().get("List1");
Iterator itr=temp.iterator();
while(itr.hasNext())
{
StatusDTO psd=(StatusDTO)itr.next();
System.out.println("********"+psd.getSerialNo());
%>
<tr>
<td bgcolor="#E6FAFB">
<%=psd.getSerialNo()%>
</td>
<td bgcolor="#E6FAFB">
<%
if(psd.getIsDisabled())
{
%>
<s:checkbox name="selectedIndex" theme="simple" disabled="true">
</s:checkbox>
<%
}
else
{
%>
<s:checkbox name="selectedIndex" theme="simple" value="1">
</s:checkbox>
<%}
%>
</td>
</tr>
<% }
%>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Staff Bean 应该有
JSP
在提交时,您将被选中的复选框作为 true
Staff Bean should have
JSP
On submit you will get selected check box as a true
与数组相比,我更喜欢列表...让这个非常简短的示例运行起来,它将向您展示如何动态选择复选框。
该演示列出了许多用数字标记的复选框,从第一个列表中选择的框将出现在第二个列表中,在第二个列表中选择的框将成为无序列表的一部分。
JSP“checkboxes.jsp”
操作类“Checkboxes.java”
如果使用约定,如果使用 xml 操作“checkboxes”映射到类“struts2.Checkboxes”,则这很简单。
您可以从示例中看到,显示的内容之间可能存在不一致所选复选框的状态以及无序列表中的内容。在操作方法中这确实很容易纠正,但会使示例更长并且有损于说明复选框的功能。此外,有些人认为用 getter 和 setter 封装字段是一个好主意,但它又会大大增加代码长度。
I prefer List to arrays... Get this very short example working and it will show you how to dynamically select check boxes.
The demo lists a number of check boxes labeled by numbers, a box selected from the first list will appear in a second list, boxes selected in the second list will become part of a unordered list.
JSP "checkboxes.jsp"
Action Class "Checkboxes.java"
If using conventions this is straight forward if using xml action "checkboxes" maps to class "struts2.Checkboxes"
You can see from the example that there can be a disagreement between the displayed state of the selected checkboxes and what is in the unordered list. This would be really easy to correct within the action method but would make the example longer and detract from illustrating the functionality of the check boxes. Further some feel that encapsulating the fields with getters and setters is a good idea but again it would greatly expand the code length.