如何使用 Struts2 和 Jquery 预填充复选框?

发布于 2024-09-16 10:05:16 字数 641 浏览 2 评论 0原文

我正在尝试确定预填充使用 Struts2 表单标签创建的某些复选框的最佳/最简单方法。我的应用程序是一个“正常”的三层设置,在控制器层使用 Struts2。

在我真正深入挖掘之前,标签是否支持创建所有可能的复选框的列表,然后填充它(例如,通过以下操作)?

示例操作:(

public class UserManagementAction extends ActionSupport implements Preparable {

    private List<String> allRoles;
    private List<String> rolesToPrepopulate;

    // get/set methods

    public void prepare() throws Exception {
       // populate the allRoles and rolesToPrepopulate lists
    }

    public String execute() throws Exception {
        return INPUT;
    }

注意:假设 struts.xml 已配置为返回 INPUT 的 JSP)

感谢您的帮助。

贾森

I am trying to determine the best/easiest way to prepopulate certain checkboxes created using Struts2 form tags. My application is a "normal" three tier setup, using Struts2 on the controller layer.

Before I really, really dig deep here, does the tag support creating the list of all possible checkboxes, then populating it (say, via the below action)?

Sample action:

public class UserManagementAction extends ActionSupport implements Preparable {

    private List<String> allRoles;
    private List<String> rolesToPrepopulate;

    // get/set methods

    public void prepare() throws Exception {
       // populate the allRoles and rolesToPrepopulate lists
    }

    public String execute() throws Exception {
        return INPUT;
    }

(Note: assume that struts.xml has been configured with which JSP to return for INPUT)

Thanks for any help.

Jason

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

鹊巢 2024-09-23 10:05:17

我要做的是一个新的对象类并将其用作复选框。

例如:

public class StrutsCheckbox {
    private Integer id;
    private Boolean selected;
...
}

prepare() 方法中,您可以根据需要设置 selected 字段(以及所有 id 字段)。

接下来在 JSP 中:

<s:iterator value="allRoles">
    <s:checkbox name="selected" id="selected" fieldValue="%{id}" value="%{selected}"/>
</s:iterator>

然后在提交操作中选择的集合将填充 id。

public class UserManagementAction extends ActionSupport implements Preparable {

    private List<StrutsCheckbox> allRoles;
    private List<StrutsCheckbox> rolesToPrepopulate;
    private List<Integer> selectedCheckboxes;

    // get/set methods

    public void prepare() throws Exception {
       // populate the allRoles and rolesToPrepopulate lists
       // fill and set allRoles and/or rolesToPrepopulate
    }

    public String execute() throws Exception {
        return INPUT;
    }

    public String submit() throws Exception {
        // list selectedCheckboxes is filled with selected fields id's 
        return INPUT;
    }

也许经过一些修正它会起作用,但主要思想就在这里。

What I would do is a new object class and use it as for checkboxes.

For example:

public class StrutsCheckbox {
    private Integer id;
    private Boolean selected;
...
}

And in prepare() method you can set selected field as you wish (and also id to all of them).

Next in JSP:

<s:iterator value="allRoles">
    <s:checkbox name="selected" id="selected" fieldValue="%{id}" value="%{selected}"/>
</s:iterator>

And then in submit action Collection selected will be filled with ids.

public class UserManagementAction extends ActionSupport implements Preparable {

    private List<StrutsCheckbox> allRoles;
    private List<StrutsCheckbox> rolesToPrepopulate;
    private List<Integer> selectedCheckboxes;

    // get/set methods

    public void prepare() throws Exception {
       // populate the allRoles and rolesToPrepopulate lists
       // fill and set allRoles and/or rolesToPrepopulate
    }

    public String execute() throws Exception {
        return INPUT;
    }

    public String submit() throws Exception {
        // list selectedCheckboxes is filled with selected fields id's 
        return INPUT;
    }

Maybe with some corrections it will work, but the main idea is here.

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