You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by albert kao <al...@gmail.com> on 2018/12/22 15:09:24 UTC

How to display both selected key and value of s:select

I want to display both selected key and value of s:select.
e.g. for the following codes, after I select
key="2", value="Canada"
and press the submit button.
Only the key "Country: 2" is displayed in the registerResult.jsp as desired.
How to make the value "Canada" to display also in the browser?

public class RegisterAction extends ActionSupport {
private String country, selectedCountry;
private ArrayList<Country> countryList;

public RegisterAction() {
countryList = new ArrayList<Country>();
countryList.add(new Country(1, "India"));
countryList.add(new Country(2, "Canada"));
countryList.add(new Country(3, "US"));
return;
}
}

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

register.jsp
<s:form action="register">

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

<s:submit />
</s:form>


registerResult.jsp
<b>Country:</b> <s:property value="country" />
<br>selectedCountry:</b> <s:property value="selectedCountry" />
<b>CountryListAll:</b> <s:property value="getCountryList()" />
<b>CountryListAll2:</b> <s:property value="countryList" />
<b>CountryList:</b> <s:property value="getCountryList().get(country)" />
<br>CountryList.get <s:property value="countryList.get(country)" />


http://localhost:8080/Struts2Example/register.action
Country: 2
selectedCountry: CountryListAll: [com.test.common.Country@32cdbdf6,
com.test.common.Country@6cbe3d89, com.test.common.Country@7abf043f]
CountryListAll2: [com.test.common.Country@32cdbdf6,
com.test.common.Country@6cbe3d89, com.test.common.Country@7abf043f]
CountryList: com.test.common.Country@7abf043f
CountryList.get com.test.common.Country@7abf043f

Re: How to display both selected key and value of s:select

Posted by albert kao <al...@gmail.com>.
Hi Yasser,

Thanks for your useful suggestion!
It works with:
public class RegisterAction {
...
   public Country getSelectedCountryObject(String selectedCountry) {
      for (Country c : countryList) {
         if (String.valueOf(c.getCountryId()).equals(selectedCountry))
            return c;
      }
      return new Country(0, "");
   }
...
}

<s:property value="getSelectedCountryObject(country).getCountryName()" />

On Mon, Dec 24, 2018 at 3:00 AM Yasser Zamani <ya...@apache.org>
wrote:

> Hi Albert,
>
> The simplest method can be something like this:
>
> public class RegisterAction {
> ...
> public Country getSelectedCountryObject() {
> foreach (c in countryList) if(c.countryId==selectedCountry) return c;
> }
> ...
> }
>
> <s:property value="selectedCountryObject.countryName" />
>
> Thanks for using Struts.
>
> Kind Regards.
>
> >-----Original Message-----
> >From: albert kao <al...@gmail.com>
> >Sent: Saturday, December 22, 2018 6:39 PM
> >To: Struts Users Mailing List <us...@struts.apache.org>
> >Subject: How to display both selected key and value of s:select
> >
> >I want to display both selected key and value of s:select.
> >e.g. for the following codes, after I select key="2", value="Canada"
> >and press the submit button.
> >Only the key "Country: 2" is displayed in the registerResult.jsp as
> desired.
> >How to make the value "Canada" to display also in the browser?
> >
> >public class RegisterAction extends ActionSupport { private String
> country,
> >selectedCountry; private ArrayList<Country> countryList;
> >
> >public RegisterAction() {
> >countryList = new ArrayList<Country>();
> >countryList.add(new Country(1, "India")); countryList.add(new Country(2,
> >"Canada")); countryList.add(new Country(3, "US")); return; } }
> >
> >public class Country {
> >private int countryId;
> >private String countryName;
> >public Country(int countryId, String countryName) { this.countryId =
> countryId;
> >this.countryName = countryName; } public int getCountryId() { return
> countryId; }
> >public void setCountryId(int countryId) { this.countryId = countryId; }
> public String
> >getCountryName() { return countryName; } public void setCountryName(String
> >countryName) { this.countryName = countryName; } }
> >
> >register.jsp
> ><s:form action="register">
> >
> ><s:select key="country" list="countryList" listKey="countryId"
> >listValue="countryName"
> >value="selectedCountry"
> >headerKey="0" headerValue="Country"
> >label="Select a country" />
> >
> ><s:submit />
> ></s:form>
> >
> >
> >registerResult.jsp
> ><b>Country:</b> <s:property value="country" /> <br>selectedCountry:</b>
> ><s:property value="selectedCountry" /> <b>CountryListAll:</b> <s:property
> >value="getCountryList()" /> <b>CountryListAll2:</b> <s:property
> >value="countryList" /> <b>CountryList:</b> <s:property
> >value="getCountryList().get(country)" /> <br>CountryList.get <s:property
> >value="countryList.get(country)" />
> >
> >
> >http://localhost:8080/Struts2Example/register.action
> >Country: 2
> >selectedCountry: CountryListAll: [com.test.common.Country@32cdbdf6,
> >com.test.common.Country@6cbe3d89, com.test.common.Country@7abf043f]
> >CountryListAll2: [com.test.common.Country@32cdbdf6,
> >com.test.common.Country@6cbe3d89, com.test.common.Country@7abf043f]
> >CountryList: com.test.common.Country@7abf043f CountryList.get
> >com.test.common.Country@7abf043f
>

RE: How to display both selected key and value of s:select

Posted by Yasser Zamani <ya...@apache.org>.
Hi Albert,

The simplest method can be something like this:

public class RegisterAction {
...
public Country getSelectedCountryObject() {
foreach (c in countryList) if(c.countryId==selectedCountry) return c;
}
...
}

<s:property value="selectedCountryObject.countryName" />

Thanks for using Struts.

Kind Regards.

>-----Original Message-----
>From: albert kao <al...@gmail.com>
>Sent: Saturday, December 22, 2018 6:39 PM
>To: Struts Users Mailing List <us...@struts.apache.org>
>Subject: How to display both selected key and value of s:select
>
>I want to display both selected key and value of s:select.
>e.g. for the following codes, after I select key="2", value="Canada"
>and press the submit button.
>Only the key "Country: 2" is displayed in the registerResult.jsp as desired.
>How to make the value "Canada" to display also in the browser?
>
>public class RegisterAction extends ActionSupport { private String country,
>selectedCountry; private ArrayList<Country> countryList;
>
>public RegisterAction() {
>countryList = new ArrayList<Country>();
>countryList.add(new Country(1, "India")); countryList.add(new Country(2,
>"Canada")); countryList.add(new Country(3, "US")); return; } }
>
>public class Country {
>private int countryId;
>private String countryName;
>public Country(int countryId, String countryName) { this.countryId = countryId;
>this.countryName = countryName; } public int getCountryId() { return countryId; }
>public void setCountryId(int countryId) { this.countryId = countryId; } public String
>getCountryName() { return countryName; } public void setCountryName(String
>countryName) { this.countryName = countryName; } }
>
>register.jsp
><s:form action="register">
>
><s:select key="country" list="countryList" listKey="countryId"
>listValue="countryName"
>value="selectedCountry"
>headerKey="0" headerValue="Country"
>label="Select a country" />
>
><s:submit />
></s:form>
>
>
>registerResult.jsp
><b>Country:</b> <s:property value="country" /> <br>selectedCountry:</b>
><s:property value="selectedCountry" /> <b>CountryListAll:</b> <s:property
>value="getCountryList()" /> <b>CountryListAll2:</b> <s:property
>value="countryList" /> <b>CountryList:</b> <s:property
>value="getCountryList().get(country)" /> <br>CountryList.get <s:property
>value="countryList.get(country)" />
>
>
>http://localhost:8080/Struts2Example/register.action
>Country: 2
>selectedCountry: CountryListAll: [com.test.common.Country@32cdbdf6,
>com.test.common.Country@6cbe3d89, com.test.common.Country@7abf043f]
>CountryListAll2: [com.test.common.Country@32cdbdf6,
>com.test.common.Country@6cbe3d89, com.test.common.Country@7abf043f]
>CountryList: com.test.common.Country@7abf043f CountryList.get
>com.test.common.Country@7abf043f