如何使用 Struts2 和 Jquery 预填充复选框? [英] How To Prepopulate Checkboxes With Struts2 and Jquery?

查看:30
本文介绍了如何使用 Struts2 和 Jquery 预填充复选框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

示例操作:

public class UserManagementAction extends ActionSupport 实现 Preparable {私人列表<字符串>所有角色;私人列表<字符串>角色预填充;//获取/设置方法公共无效准备()抛出异常{//填充 allRoles 和 rolesToPrepopulate 列表}公共字符串执行()抛出异常{返回输入;}

(注:假设struts.xml中已经配置了INPUT返回哪个JSP)

感谢您的帮助.

杰森

解决方案

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

例如:

公共类StrutsCheckbox {私有整数 ID;选择了私有布尔值;...}

并且在 prepare() 方法中,您可以根据需要设置 selected 字段(也可以将 id 设置为所有字段).

JSP 中的下一步:

<s:checkbox name="selected" id="selected" fieldValue="%{id}" value="%{selected}"/></s:迭代器>

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

public class UserManagementAction extends ActionSupport 实现 Preparable {私人列表所有角色;私人列表角色预填充;私人列表<整数>选中的复选框;//获取/设置方法公共无效准备()抛出异常{//填充 allRoles 和 rolesToPrepopulate 列表//填充并设置 allRoles 和/或 rolesToPrepopulate}公共字符串执行()抛出异常{返回输入;}公共字符串提交()抛出异常{//列表 selectedCheckboxes 填充了选定的字段 id返回输入;}

也许进行一些更正后它会起作用,但主要思想在这里.

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

解决方案

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.

这篇关于如何使用 Struts2 和 Jquery 预填充复选框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆