开发者

retrieve value in selectbox in struts2

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.communityLi开发者_JAVA技巧st = communityList;
    }

    public Boolean getMailingList() {
        return mailingList;
    }

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

}

index.jsp

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

success.jsp

        Country: <s:property value="country" /><br>

When I select a value in textbox,it is retrieving id and not name i.e when I select india I am getting 1 instead of India.

Is there any way to retrieve name instead of id?


the arrayList communityList must be declared of type Country not String

private ArrayList<Country> communityList;

make sure it has getter method in the action class

Then use the following syntax in the jsp

   <s:iterator value="countryList"> <!-- here myList contains the list of objects -
     <s:property value="countryName" /><br/>
     <s:property value="countryId" /><br/>
   </s:iterator>

As per your edited question you need to use countryName as the listKey

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


Make sure you have a public getter for countryList. If it is still not working then see if 6 <br/> are getting printed in the output to verify whether the loop itself is running or not. Also check for any exceptions in your console.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜