0

我在尝试获取一些值时遇到问题。这是我的情况(检票口)

我有一个下拉选择 study_template,我在填充 DDC 时没有问题,问题是当我尝试获取某个值(id 或名称)时。这是代码

ChoiceRenderer choiceRenderer = new ChoiceRenderer("name", "id");
study_template  = new DropDownChoice( "study_template",new PropertyModel( this, "selectedTemplate" ),template_names , choiceRenderer);

template_names 是一个列表< SelectOption >,其值从数据库中获取。这工作正常。

这是我用来填充 DDC 的类

public class SelectOption implements java.io.Serializable {
    private long id; 
    private String  name;
    public SelectOption(long id, String name ) {
        this.id = id; this.name=name;
    }

     public long getId() 
        {return id; } 

        public String getName() 
        {return name; } 
}

通常我可以使用 study_template.getModelObject() 获取值,但在这种情况下它不起作用,我对获取 id 和 name 没有任何想法,我知道我需要 GETID() 和 GETNAME( ),但我不知道如何使用它,任何帮助将不胜感激

4

2 回答 2

0

谢谢你的回答,我已经搞定了

我用这个

private SelectOption selectedTemplate;

接着

selectedTemplate.getId();
于 2015-07-28T05:47:29.317 回答
0

您可以使用以下内容:

公共类 SpikePage 扩展 WebPage {

    类人{

        字符串标识;
        字符串名称;

        公共人员(字符串ID,字符串名称){
            这个.id = id;
            this.name = 名称;
        }

        公共字符串 getId() {
            返回标识;
        }

        公共无效 setId(字符串 id){
            这个.id = id;
        }

        公共字符串 getName() {
            返回名称;
        }

        公共无效集合名称(字符串名称){
            this.name = 名称;
        }
    }

    公共 SpikePage() {

        员工员工 = new Person("E001", "ABC");
        人员经理 = new Person("E002", "XYZ");

        列表 personList = new ArrayList(2);
        personList.add(员工);
        personList.add(经理);

        表单 form = new Form("form");

        最终 DropDownChoice personDropDownChoice = new DropDownChoice("person", new ArrayList(personList), new IChoiceRenderer() {
            @覆盖
            公共对象getDisplayValue(人对象){
                return object.getId().concat("-").concat(object.getName());
            }

            @覆盖
            public String getIdValue(Person object, int index) {
                返回对象.getId();
            }
        });

        personDropDownChoice.setOutputMarkupId(true);
        personDropDownChoice.setNullValid(false);
        form.add(personDropDownChoice);

        最终标签 label = new Label("name");
        label.setOutputMarkupId(true);
        form.add(标签);

        personDropDownChoice.add(new AjaxFormComponentUpdatingBehavior("onchange") {
            @覆盖
            受保护的无效 onUpdate(AjaxRequestTarget 目标){
                Person selectedPerson = personDropDownChoice.getModelObject();
                label.setDefaultModelObject("嗨 ".concat(selectedPerson.getName()));
                目标.添加(标签);
            }
        });

        添加(表格);
    }
}

于 2015-07-22T18:20:50.513 回答