0

这是 register.jsp 页面。在这里从 Action 类的下拉列表中设置 countryList

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

在这里,我从 Action 中获取所有列表;

当我提交 action 时,我在 success.jsp 上获得了 key 而不是 value。

<s:property value="country"/> 

在这里,我得到了选择的键,例如 0,1,2 而不是国家/地区值。

我的行动课

   public class RegisterAction extends ActionSupport {
   private List<String> communityList;
   private List<Country> countryList;
   private String country;
   private String userName;
   private String password;
   private String gender;
   private String about;
   private String[] community;
   private boolean mailingList;

       public String execute() {
       return SUCCESS;}
       public String populate(){
       communityList = new ArrayList<String>();
       countryList  = new ArrayList<Country>();
       countryList.add(new Country(1,"India"));
       countryList.add(new Country(2,"US"));
       countryList.add(new Country(3,"UK"));
       communityList.add("JAVA");
       communityList.add(".NET");
       communityList.add("SOA");
       community=new String[]{"JAVA",".NET"};
       mailingList = true;
       return "populate";
    }

public List<String> getCommunityList() {
    return communityList;
}
public void setCommunityList(List<String> communityList) {
    this.communityList = communityList;
}

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[] getCommunity() {
    return community;
}
public void setCommunity(String[] community) {
    this.community = community;
}
public boolean isMailingList() {
    return mailingList;
}
public void setMailingList(boolean mailingList) {
    this.mailingList = mailingList;
}


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

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

public String getCountry() {
    return country;
}

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

国家.java

public class Country {
  private String countryName;
  private int countryId;
  public Country(){}
  public Country(int countryId,String countryName){
      this.countryId=countryId;
      this.countryName=countryName;
  }
public String getCountryName() {
    return countryName;
}
public void setCountryName(String countryName) {
    this.countryName = countryName;
}
public int getCountryId() {
    return countryId;
}
public void setCountryId(int countryId) {
    this.countryId = countryId;
}
  }
4

2 回答 2

1

您可以从请求对象中获取值。

HttpServletRequest request = (HttpServletRequest)(ActionContext.getContext().get(ServletActionContext.HTTP_REQUEST));
Country value = (Country)request.getParameter("country");
于 2017-09-11T09:03:46.657 回答
0

以这种方式使用listKey="countryName" listValue="countryName"您将获得价值

于 2013-10-17T13:59:39.703 回答