Struts2如何从下拉列表中选择值到其他jsp页面

时间:2013-10-16 20:11:51

标签: java jsp struts2

这是register.jsp页面。这里在Action类

的下拉列表中设置countryList
 <s:select name="country" list="countryList" listKey="countryId"  listValue="countryName"  headerKey="0" headerValue="Country"  label="Select a country">

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

当我提交动作时,我在success.jsp上获得了密钥而不是值。

<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;
}
 }

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;
}
  }

2 个答案:

答案 0 :(得分:1)

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

HttpServletRequest request = (HttpServletRequest)(ActionContext.getContext().get(ServletActionContext.HTTP_REQUEST));
Country value = (Country)request.getParameter("country");

答案 1 :(得分:0)

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