• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to display a list box with values from an "Array List" in "Struts2"

 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello people,

In our application, we have several list boxes having "fixed values". We have displayed them in our ".jsp" pages using tags defined as given below:

<s:select
tooltip="Choose Your Favourite Colour"
label="fav colour"
list="{'Red','Bue','Green'}"
name="fav_colour"
headerKey="None"
headerValue="None"/>

But there is a "list box", which needs to be populated with values from an "Array List". We have inserted values into the "Array List" as follows:

List countries = new ArrayList();
countries.add("India");
countries.add("United States");
countries.add("UK");


Now., we do not know how to make the values of this "Array List" appear in the appropriate list box in the ".jsp" page. The tag, as defined in the ".jsp" page, is as follows:

<s:select
tooltip="Choose Your Country"
label="Country"
list= ??? (This is where we need to substitute the names of the countries from the "Array List" defined above)
name="country"
headerKey="None"
headerValue="None"/>

Thanks for your time,
Lakshman.





 
Ranch Hand
Posts: 1211
Mac IntelliJ IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If there is a getCountries() method in your Action class that returns the list, the you will use -
list = "countries"

HTH.
 
guthula lakshman
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sonny Gill,

Thank you for your quick response

As you had told, we have provided the "getCountries" method in the POJO class, as follows:

public List getCountries(){
return countries;
}

We had defined the tag of our '.jsp' as:

<s:select
tooltip="Choose Your Country"
label="Country"
list="countries"
name="country"
headerKey="None"
headerValue="None"/>

We had actually had a reference to the link, http://www.roseindia.net/struts/struts2/struts2uitags/select-tag.shtml, while attempting to implement this. But, unfortunately, the solution given in the link did not work for us.

Thanks again for your time and prompt reply,
Lakshman.


 
Sonny Gill
Ranch Hand
Posts: 1211
Mac IntelliJ IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What exactly do you mean when you say it didn't work?
What was the output? Were there any error messages on the console or log files?
 
guthula lakshman
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Sonny Gill,

Thanks for your reply..

We have a list of countries added in class file using ArrayList (as shown in the first post of this thread). we have to display those countries in a jsp page as dropdown list using <s:select> tag of struts2.

we have added necessary getter/setter methods and execute method (POJO) as follows

public List getCountries(){
return countries;
}

public String getCountry(){
return country;
}

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

public String execute() throws Exception {
return SUCCESS;
}

but we couldnt populate those countries in drop-down list (using select tag).

When we execute we get blank page

and error log shown as follows :

SEVERE: Servlet.service() for servlet jsp threw exception
tag 'select', field 'list', name 'country': The requested list key 'countries' could not be resolved as a collection/array/map/enumeration/iterator type. Example: people or people.{name} - [unknown location]
at org.apache.struts2.components.Component.fieldError(Component.java:231)
at org.apache.struts2.components.Component.findValue(Component.java:293)
at org.apache.struts2.components.ListUIBean.evaluateExtraParams(ListUIBean.java:79)
at org.apache.struts2.components.Select.evaluateExtraParams(Select.java:99)
at org.apache.struts2.components.UIBean.evaluateParams(UIBean.java:780)
at org.apache.struts2.components.UIBean.end(UIBean.java:481)
at org.apache.struts2.views.jsp.ComponentTagSupport.doEndTag(ComponentTagSupport.java:43)
at org.apache.jsp.vendor_005fsignup_jsp._jspx_meth_s_005fselect_005f1(vendor_005fsignup_jsp.java:370)
at org.apache.jsp.vendor_005fsignup_jsp._jspx_meth_s_005fform_005f0(vendor_005fsignup_jsp.java:172)
at org.apache.jsp.vendor_005fsignup_jsp._jspService(vendor_005fsignup_jsp.java:100)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:374)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:342)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:267)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.struts2.dispatcher.FilterDispatcher.doFilter(FilterDispatcher.java:416)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:286)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:845)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
at java.lang.Thread.run(Unknown Source)

Thanks again for your time and prompt reply,
Lakshman.
 
Sonny Gill
Ranch Hand
Posts: 1211
Mac IntelliJ IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That error will show up if there is no getter for countries, or if the result of calling getCountries is null.
Can you verify, using logging or System.out.println that countries is in fact not null before the control passes to the JSP?
 
guthula lakshman
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Sonny Gill,

Thanks for your reply..

i have provided System.out.println() command in getter/setter methods of countryList, in constructor, and in execute method. and the result is as follows:

Feb 7, 2009 5:29:58 PM org.apache.catalina.startup.Catalina start
INFO: Server startup in 8643 ms
In Constructor: [com.action.CountryVO@11ed166, com.action.CountryVO@45aa2c, com.action.CountryVO@1a734ff]
Feb 7, 2009 5:30:05 PM com.opensymphony.xwork2.validator.ActionValidatorManagerFactory <clinit>
INFO: Detected AnnotationActionValidatorManager, initializing it...
In getCountryList(): [com.action.CountryVO@11ed166, com.action.CountryVO@45aa2c, com.action.CountryVO@1a734ff]
In setCountryList(): [com.action.CountryVO@11ed166, com.action.CountryVO@45aa2c, com.action.CountryVO@1a734ff]
In execute(): [com.action.CountryVO@11ed166, com.action.CountryVO@45aa2c, com.action.CountryVO@1a734ff]
In getCountryList(): [com.action.CountryVO@11ed166, com.action.CountryVO@45aa2c, com.action.CountryVO@1a734ff]

I am not getting proper values .. and the select tag is empty when executing the program (means, empty drop-down list)


Please have a look on my java program :



and my JSP is :



Is anything wrong in this program?


Thanks again for your time and prompt reply,
Lakshman.
 
Sonny Gill
Ranch Hand
Posts: 1211
Mac IntelliJ IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I just had a very quick look, and it kinda looks ok..


list="countryLis}"



Is that a typo when you posted the code here, or does that exist in your code?
 
guthula lakshman
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Sonny Gill,

Thanks for your reply..

i was trying like this : list="{countryList}" .. Its typing mistake.... list="countryList"

almost its one week.. could not get the solution

Thanks again for your time and prompt reply,
Lakshman.
 
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1) CountryVO doesn't need to call super(); it's a POJO.
2) SelectTagExample.execute() doesn't need to call setCountryList(getCountryList()); weirdly redundant.
3) If you're using generics why not make countryList a List<CountryVO>?!?!

How are you accessing the form page? In other words, what's the URL, and how is the action configured?
 
guthula lakshman
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Newton,

I removed super() from POJO class and removed set method from execute() and also changed the list to generic type ..

but still it doesnt work.

struts.xml is as follows:





Also i have a suggestion that I have seen lots of people asking questions on <s:select> tag... i doubt, will <s:select> tag populate from bean/database? or mistake in struts2 framework? if not, you guys can provide a complete working example here.. so that many people get solved their problem na..

please consider this as a valid request

Thanks again for your time and prompt reply,
Lakshman.
 
David Newton
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
HOW ARE YOU ACCESSING THE PAGE? In other words, what URL are you using? Are you accessing it through the action?

Using your code (more or less) it's working fine--are you sure you're deploying the new class? Are you accessing the page via the action (not the JSP directly)?
 
guthula lakshman
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I given as follows in index.jsp

<s:url id="url" value="pages/selectTag.jsp"/><s:a href="%{#url}">SelectTag - from jsp to action</s:a>

when i run the project i get this page with a link.. when i click the link it takes to jsp page(http://localhost:8080/Sample/pages/selectTag.jsp) where i use <s:select> tag


how can i access directly thru action? since the list has to populate and show it to users choosing their country, so jsp page should be called na.... one thing is to pre-populate countries may work based on your suggestion but then how can i redirect to jsp page? ...

can you please provide me the code... so that i can try and get solved ...

Thanks again for your time and prompt reply,
Lakshman.
 
David Newton
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm not entirely sure what you're asking.

The URL should be to an action, not a JSP. The JSP would be used as the result configuration for a successful execution of the action.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic