Struts2 复选框在 Action 类中返回值

发布于 2024-10-14 21:37:31 字数 1629 浏览 1 评论 0原文

我对 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

孤独岁月 2024-10-21 21:37:31

Staff Bean 应该有

private boolean delete; /// with its setter and getter

JSP

<s:iterator name="staffList" status="bean_rowNum">
    <s:checkbox name="staffList[%{#request.attr.bean_rowNum-1}].delete" />
</s:iterator>

在提交时,您将被选中的复选框作为 true

Staff Bean should have

private boolean delete; /// with its setter and getter

JSP

<s:iterator name="staffList" status="bean_rowNum">
    <s:checkbox name="staffList[%{#request.attr.bean_rowNum-1}].delete" />
</s:iterator>

On submit you will get selected check box as a true

梦途 2024-10-21 21:37:31

与数组相比,我更喜欢列表...让这个非常简短的示例运行起来,它将向您展示如何动态选择复选框。

该演示列出了许多用数字标记的复选框,从第一个列表中选择的框将出现在第二个列表中,在第二个列表中选择的框将成为无序列表的一部分。

JSP“checkboxes.jsp”

<%@taglib prefix="s" uri="/struts-tags"%>
<html>
    <body>
        <h1>All Check Boxes</h1>
        <s:form action="checkboxes">
            <s:checkboxlist name="selectedBoxes" list="allBoxes"/>
            <s:submit/>
        <h1>Selected Check Boxes</h1>
            <s:checkboxlist name="selectedSelectedBoxes" list="selectedBoxes"/>
            <s:submit/>
        </s:form>
        <h1>Values chosen from selected Check boxes</h1>
        <ul>
            <s:iterator value="selectedSelectedBoxes">
                <li><s:property/></li>
            </s:iterator>
        </ul>
    </body>
</html>

操作类“Checkboxes.java”

package struts2;

import com.opensymphony.xwork2.ActionSupport;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class Checkboxes extends ActionSupport{
    public List<Integer> allBoxes = Arrays.asList(1,2,3,4,5,6,7,22,33,44); //ten elements
    public List<Integer> selectedBoxes = new ArrayList();
    public List<Integer> selectedSelectedBoxes = new ArrayList();
}

如果使用约定,如果使用 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"

<%@taglib prefix="s" uri="/struts-tags"%>
<html>
    <body>
        <h1>All Check Boxes</h1>
        <s:form action="checkboxes">
            <s:checkboxlist name="selectedBoxes" list="allBoxes"/>
            <s:submit/>
        <h1>Selected Check Boxes</h1>
            <s:checkboxlist name="selectedSelectedBoxes" list="selectedBoxes"/>
            <s:submit/>
        </s:form>
        <h1>Values chosen from selected Check boxes</h1>
        <ul>
            <s:iterator value="selectedSelectedBoxes">
                <li><s:property/></li>
            </s:iterator>
        </ul>
    </body>
</html>

Action Class "Checkboxes.java"

package struts2;

import com.opensymphony.xwork2.ActionSupport;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class Checkboxes extends ActionSupport{
    public List<Integer> allBoxes = Arrays.asList(1,2,3,4,5,6,7,22,33,44); //ten elements
    public List<Integer> selectedBoxes = new ArrayList();
    public List<Integer> selectedSelectedBoxes = new ArrayList();
}

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文