【问题标题】:Struts2 How to get selected value from dropdownlist to other jsp pageStruts2如何从下拉列表中获取选定的值到其他jsp页面
【发布时间】:2013-10-16 20:11:50
【问题描述】:

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

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

【问题讨论】:

  • 这就是 中迭代的同一源中检索值
  • 这不是从同一来源渲染的。我在索引 jsp 中使用了 来填充 countryList。一切正常,在success.jsp中获取key而不是value
  • 大声笑...然后使用listKey="countryName" listValue="countryName"?那么有一个结构是没有意义的,一个 List 就足够了:/

标签: java jsp struts2


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-07-29
      • 1970-01-01
      • 2016-07-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多