在struts2的selectbox中检索值 package-vaannila; 导入java.util.ArrayList; 导入com.opensymphony.xwork2.ActionSupport; 公共类RegisterAction扩展了ActionSupport{ 私有字符串用户名; 私有字符串密码; 私人字符串性别; 私人串; 私人国家; 私有ArrayList国家列表; 私人社区; 私人ArrayList社区列表; 私有布尔邮件列表; 公共字符串填充(){ countryList=新的ArrayList(); 国家列表。添加(新国家(1,“印度”); 国家列表。添加(新国家(2,“美国”); 国家列表。添加(新国家(3,“法国”); communityList=新的ArrayList(); communityList.add(“Java”); communityList.add(“.Net”); 社区列表。添加(“SOA”); community=新字符串[]{“Java”,“.Net”}; mailingList=true; 返回“填充”; } @凌驾 公共字符串execute(){ 回归成功; } 公共字符串getUserName(){ 返回用户名; } public void setUserName(字符串用户名){ this.userName=用户名; } 公共字符串getPassword(){ 返回密码; } public void setPassword(字符串密码){ this.password=密码; } 公共字符串getGender(){ 返回性别; } 公共无效设置性别(字符串性别){ 这个。性别=性别; } 公共字符串getAbout(){ 返回大约; } 公共无效设置(字符串设置){ 这个; } 公共字符串getCountry(){ 返回国; } 公共国家/地区(字符串国家/地区){ 这个国家=国家; } 公共阵列列表getCountryList(){ 返回国家列表; } 公共无效setCountryList(ArrayList countryList){ this.countryList=countryList; } 公共字符串[]getCommunity(){ 返回社区; } 公共void集合社区(字符串[]社区){ 这个社区=社区; } 公共阵列列表getCommunityList(){ 返回社区列表; } 公共无效setCommunityList(ArrayList communityList){ this.communityList=社区列表; } 公共布尔getMailingList(){ 返回邮件列表; } 公共无效设置邮件列表(布尔邮件列表){ this.mailingList=mailingList; } }

在struts2的selectbox中检索值 package-vaannila; 导入java.util.ArrayList; 导入com.opensymphony.xwork2.ActionSupport; 公共类RegisterAction扩展了ActionSupport{ 私有字符串用户名; 私有字符串密码; 私人字符串性别; 私人串; 私人国家; 私有ArrayList国家列表; 私人社区; 私人ArrayList社区列表; 私有布尔邮件列表; 公共字符串填充(){ countryList=新的ArrayList(); 国家列表。添加(新国家(1,“印度”); 国家列表。添加(新国家(2,“美国”); 国家列表。添加(新国家(3,“法国”); communityList=新的ArrayList(); communityList.add(“Java”); communityList.add(“.Net”); 社区列表。添加(“SOA”); community=新字符串[]{“Java”,“.Net”}; mailingList=true; 返回“填充”; } @凌驾 公共字符串execute(){ 回归成功; } 公共字符串getUserName(){ 返回用户名; } public void setUserName(字符串用户名){ this.userName=用户名; } 公共字符串getPassword(){ 返回密码; } public void setPassword(字符串密码){ this.password=密码; } 公共字符串getGender(){ 返回性别; } 公共无效设置性别(字符串性别){ 这个。性别=性别; } 公共字符串getAbout(){ 返回大约; } 公共无效设置(字符串设置){ 这个; } 公共字符串getCountry(){ 返回国; } 公共国家/地区(字符串国家/地区){ 这个国家=国家; } 公共阵列列表getCountryList(){ 返回国家列表; } 公共无效setCountryList(ArrayList countryList){ this.countryList=countryList; } 公共字符串[]getCommunity(){ 返回社区; } 公共void集合社区(字符串[]社区){ 这个社区=社区; } 公共阵列列表getCommunityList(){ 返回社区列表; } 公共无效setCommunityList(ArrayList communityList){ this.communityList=社区列表; } 公共布尔getMailingList(){ 返回邮件列表; } 公共无效设置邮件列表(布尔邮件列表){ this.mailingList=mailingList; } },java,struts2,Java,Struts2,index.jsp package vaannila; import java.util.ArrayList; import com.opensymphony.xwork2.ActionSupport; public class RegisterAction extends ActionSupport { private String userName; private String password; private String gender;

index.jsp

package vaannila;

import java.util.ArrayList;

import com.opensymphony.xwork2.ActionSupport;

public class RegisterAction extends ActionSupport {

    private String userName;

    private String password;

    private String gender;

    private String about;

    private String country;

    private ArrayList<Country> countryList;

    private String[] community;

    private ArrayList<String> communityList;

    private Boolean  mailingList;

    public String populate() {

        countryList = new ArrayList<Country>();
        countryList.add(new Country(1, "India"));
        countryList.add(new Country(2, "USA"));
        countryList.add(new Country(3, "France"));

        communityList = new ArrayList<String>();
        communityList.add("Java");
        communityList.add(".Net");
        communityList.add("SOA");

        community = new String[]{"Java",".Net"};
        mailingList = true;

        return "populate";
    }

    @Override
    public String execute() {
        return SUCCESS;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    public String getAbout() {
        return about;
    }

    public void setAbout(String about) {
        this.about = about;
    }

    public String getCountry() {
        return country;
    }

    public void setCountry(String country) {
        this.country = country;
    }

    public ArrayList<Country> getCountryList() {
        return countryList;
    }

    public void setCountryList(ArrayList<Country> countryList) {
        this.countryList = countryList;
    }

    public String[] getCommunity() {
        return community;
    }

    public void setCommunity(String[] community) {
        this.community = community;
    }

    public ArrayList<String> getCommunityList() {
        return communityList;
    }

    public void setCommunityList(ArrayList<String> communityList) {
        this.communityList = communityList;
    }

    public Boolean getMailingList() {
        return mailingList;
    }

    public void setMailingList(Boolean mailingList) {
        this.mailingList = mailingList;
    }

}
      <s:select name="country" list="countryList" listKey="countryId"
    listValue="countryName" headerKey="0" headerValue="Country"
    label="Select a country" />

success.jsp

package vaannila;

import java.util.ArrayList;

import com.opensymphony.xwork2.ActionSupport;

public class RegisterAction extends ActionSupport {

    private String userName;

    private String password;

    private String gender;

    private String about;

    private String country;

    private ArrayList<Country> countryList;

    private String[] community;

    private ArrayList<String> communityList;

    private Boolean  mailingList;

    public String populate() {

        countryList = new ArrayList<Country>();
        countryList.add(new Country(1, "India"));
        countryList.add(new Country(2, "USA"));
        countryList.add(new Country(3, "France"));

        communityList = new ArrayList<String>();
        communityList.add("Java");
        communityList.add(".Net");
        communityList.add("SOA");

        community = new String[]{"Java",".Net"};
        mailingList = true;

        return "populate";
    }

    @Override
    public String execute() {
        return SUCCESS;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    public String getAbout() {
        return about;
    }

    public void setAbout(String about) {
        this.about = about;
    }

    public String getCountry() {
        return country;
    }

    public void setCountry(String country) {
        this.country = country;
    }

    public ArrayList<Country> getCountryList() {
        return countryList;
    }

    public void setCountryList(ArrayList<Country> countryList) {
        this.countryList = countryList;
    }

    public String[] getCommunity() {
        return community;
    }

    public void setCommunity(String[] community) {
        this.community = community;
    }

    public ArrayList<String> getCommunityList() {
        return communityList;
    }

    public void setCommunityList(ArrayList<String> communityList) {
        this.communityList = communityList;
    }

    public Boolean getMailingList() {
        return mailingList;
    }

    public void setMailingList(Boolean mailingList) {
        this.mailingList = mailingList;
    }

}
      <s:select name="country" list="countryList" listKey="countryId"
    listValue="countryName" headerKey="0" headerValue="Country"
    label="Select a country" />
国家:
当我在文本框中选择一个值时,它正在检索id而不是名称,即当我选择india时,我得到的是1而不是india


有没有办法检索名称而不是id?

arrayList communityList必须声明为Country而不是String类型

        Country: <s:property value="country" /><br>
私有数组列表社区列表;
确保它在action类中有getter方法

然后在jsp中使用以下语法

private ArrayList<Country> communityList;

确保您有countryList的公共getter。如果仍不工作,则查看输出中是否打印了6,以验证循环本身是否正在运行。同时检查控制台中是否存在任何异常。

我已编辑了我的问题,实际上我希望显示selectbox值…..而不是id。为了获得国家名称作为键,您需要在select标记中将countryName声明为listKey。
如果我这样做,我将在下拉列表中获得1,2,3作为值,而不是India,我们不要更改listValue,让它仅为countryName YYES,它工作了,thks,但我不理解逻辑。在中,我调用getCountry()。listVale和Key的用途是什么。可以给出解释请显示您的完整操作和jsp。我在countryName的操作中根本看不到属性。