java Struts2 填充标签选择下拉列表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12783926/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 10:12:17  来源:igfitidea点击:

Struts2 populate tag select drop down list

javaselectstruts2tags

提问by Pracede

I try to populate select drop down list with Struts but it's not working:

我尝试使用 Struts 填充选择下拉列表,但它不起作用:

I have my action class :

我有我的动作课:

private Integer anneeResultat;

私人整数anneeResultat;

    private List<AnneeResultat> anneeResultatList = new ArrayList<AnneeResultat>();


public Integer getAnneeResultat() {

        return anneeResultat;
    }
    public void setAnneeResultat(Integer anneeResultat) {
        this.anneeResultat = anneeResultat;
    }


public List<AnneeResultat> getAnneeResultatList() {
    this.anneeResultatList.add(new AnneeResultat(2005,"2005"));
    this.anneeResultatList.add(new AnneeResultat(2006,"2006"));
    this.anneeResultatList.add(new AnneeResultat(2007,"2007"));
    this.anneeResultatList.add(new AnneeResultat(2008,"2008"));
    this.anneeResultatList.add(new AnneeResultat(2009,"2009"));
    this.anneeResultatList.add(new AnneeResultat(2010,"2010"));
    this.anneeResultatList.add(new AnneeResultat(2011,"2011"));
        return this.anneeResultatList;
    }
    public void setAnneeResultatsList(List<AnneeResultat> anneeResultatList) {
        this.anneeResultatList = anneeResultatList;
    }
    public String execute(){
    return SUCCESS;
    }

Here is the class AnneeResultat:

这是 AnneeResultat 类:

public class AnneeResultat {
     private Integer keyAnnee;  
     private String valueAnnee;         
    public AnneeResultat() {    
    }
    public AnneeResultat(Integer key, String value) {
        super();
        this.keyAnnee = key;
        this.valueAnnee = value;
    }
    public Integer getKey() {
        return keyAnnee;
    }
    public void setKey(Integer key) {
        this.keyAnnee = key;
    }
    public String getValue() {
        return valueAnnee;
    }
    public void setValue(String value) {
        this.valueAnnee = value;
    }   
}

Here is select tag as used on my jsp page. I use tiles on my page. Here is my first version on my page:

这是在我的 jsp 页面上使用的选择标记。我在我的页面上使用瓷砖。这是我页面上的第一个版本:

<s:select name="anneeResultat" label="Année de résultats" list="anneeResultatList" listKey="keyAnnee" listValue="valueAnnee"></s:select>

The result is that in my page the select tag generate a select box with empty element. I can't see value on the select box. No value is visible. The generated code is :

结果是在我的页面中,选择标记生成一个带有空元素的选择框。我在选择框上看不到值。看不到任何价值。生成的代码是:

<select name="anneeResultat" id="choices_anneeResultat">
    <option value=""></option>
    <option value=""></option>
    <option value=""></option>
    <option value=""></option>
    <option value=""></option>
    <option value=""></option>
    <option value=""></option>
</select>

When i used the following select tag :

当我使用以下选择标签时:

<s:select name="anneeResultat" label="Année de résultats" list="anneeResultatList" key="keyAnnee" value="valueAnnee"></s:select>

Hashcode is printed on select box list.

哈希码打印在选择框列表上。

The generated code is :

生成的代码是:

<select name="anneeResultat" id="choices_anneeResultat">
    <option value="fr.si2m.occ.dao.model.ui.AnneeResultat@1b209bc">fr.si2m.occ.dao.model.ui.AnneeResultat@1b209bc</option>
    <option value="fr.si2m.occ.dao.model.ui.AnneeResultat@19d5723">fr.si2m.occ.dao.model.ui.AnneeResultat@19d5723</option>
    <option value="fr.si2m.occ.dao.model.ui.AnneeResultat@c3bba9">fr.si2m.occ.dao.model.ui.AnneeResultat@c3bba9</option>
    <option value="fr.si2m.occ.dao.model.ui.AnneeResultat@5554a3">fr.si2m.occ.dao.model.ui.AnneeResultat@5554a3</option>
    <option value="fr.si2m.occ.dao.model.ui.AnneeResultat@27b326">fr.si2m.occ.dao.model.ui.AnneeResultat@27b326</option>
    <option value="fr.si2m.occ.dao.model.ui.AnneeResultat@6283de">fr.si2m.occ.dao.model.ui.AnneeResultat@6283de</option>
    <option value="fr.si2m.occ.dao.model.ui.AnneeResultat@1bf7b9c">fr.si2m.occ.dao.model.ui.AnneeResultat@1bf7b9c</option>
</select>

I don't know how to do to solve this problem. If someone has some ideas, it will welcome !

我不知道如何解决这个问题。如果有人有一些想法,它会欢迎!

回答by pkb

Just delete the constructor from AnneeResultatand use the below select tag in your JSP.

只需从中删除构造函数AnneeResultat并在 JSP 中使用以下选择标记即可。

<s:select name="anneeResultat" label="Année de résultats" 
    list="anneeResultatList" key="keyAnnee" value="valueAnnee"></s:select>

回答by dnhung

use "listValue" for the column you want to display.

对要显示的列使用“listValue”。

<s:select name="anneeResultat" label="Année de résultats" list="anneeResultatList" key="keyAnnee" listValue="valueAnnee"></s:select>

<s:select name="anneeResultat" label="Année de résultats" list="anneeResultatList" key="keyAnnee" listValue="valueAnnee"></s:select>

回答by Johan Tidén

The "listKey=foo" expects a getter with the same name, but named with "get" appended to the value, e.g. getFoo().

"listKey=foo" 需要一个同名的 getter,但命名时附加在值上,例如 getFoo()。

Either change your getter name to "getKeyAnnee" or change the listKey to "key"

将您的 getter 名称更改为“getKeyAnnee”或将 listKey 更改为“key”