简体   繁体   中英

Struts2 Dynamic DropDown Error: The requested list key 'CountryCodes2' could not be resolved as a collection/array/map/enumeration/iterator type

Before you mark this post as duplicate, please read carefully since I know there are similar posts on Stackoverflow and I have tried all those solutions but nothing seems to work for my scenario. In the code below, you will notice that I have incorporated some solutions from other similar posts but they don't solve my problem so please help. I am working on converting our current application from Struts1 to Struts2 and had to change all html tags to 's' tags. I've been able to get everything else to work exccept the dropdowns. In Struts1, the dropdowns were using 'collections="CountryCodes2"' but in Struts2, they have 'list=CountryCodes2' ( tag). I have been trying to resolve this Struts2 dropdown issue for the last couple days, I've been looking all over the internet and trying various solutions but nothing seems to work. With everything I try, I seem to come back to one error:

"tag 'select', field 'list', name 'countryCd': The requested list key 'CountryCodes2' could not be resolved as a collection/array/map/enumeration/iterator type."

I am able to get the value of CountryCodes2 in hashcode format but can't seem to get it to populate in dropdown. In the web browser, I am calling the actual action, not just the JSP. Not sure where I am wrong. Any help will be appreciated.

Here is my JSP page:

<%@ taglib prefix="s" uri="/struts-tags" %>
<%@ taglib uri="http://tiles.apache.org/tags-tiles" prefix="tiles" %>
Test Body
<s:select list="CountryCodes2" name="countryCd" /> <!-- This one does not work, see Action class for details-->
<s:select list="searchEngine" name="yourSearchEngine" /> <!-- This one works fine, see Action class for details -->

Here is my Action class:

package abc;//package name
import *.*//all imports

public final class StateStudentAction extends ActionSupport  implements ServletRequestAware, ServletContextAware, SessionAware {

    private Map session;
    public void setSession (Map session) {
        this.session = session;
    }

    private HttpServletRequest request;     
    public void setServletRequest(HttpServletRequest httpServletRequest) {         
        this.request = httpServletRequest;     
    }   

    private HttpServletResponse response;

    public void setServletResponse(HttpServletResponse httpServletReponse) {
        this.response = httpServletReponse;
    }

    private ServletContext servletContext;

    public void setServletContext(ServletContext servletContext) {
        this.servletContext = servletContext;
    }
    public ServletContext getServletContext() {
        return servletContext;
    }
    private StudentForm f = new StudentForm();
    public StudentForm getf() {return f;}
    public void setf(StudentForm f) {this.f = f;}

    private List<String> CountryCodes2;
    public List<String> getCountryCodes() {
        return CountryCodes2;
    }
    public void setCountryCodes(List<String> CountryCodes2) {
        this.CountryCodes2 = CountryCodes2;
    }

    private List<String> searchEngine;
        public List<String> getSearchEngine() {
            return searchEngine;
        }

        public void setSearchEngine(List<String> searchEngine) {
            this.searchEngine = searchEngine;
        }

    public String execute() throws Exception {
            HttpSession session = request.getSession();
            System.out.println("Inside StateStudentAction.execute");
            BeanManager beanManager = (BeanManager)this.servletContext.getAttribute("beanManager");
    try {

        CountryCodesController ccc = beanManager.getCountryCodesController();
        //CountryCodesController is another class from where I am getting the value of CountryCodes2 and I have checked to confirm that it is not null
        ArrayList CountryCodes2 = new ArrayList(ccc.getCountryCodes());
        System.out.println(CountryCodes2);//This prints CountryCodes2 but in hashcode format, not string format
        searchEngine = new ArrayList<String>();
        searchEngine.add("google.com");
        searchEngine.add("bing.com");
        searchEngine.add("yahoo.com");
    } catch (Exception ex) {
      Logger.error("Exception", ex);
    } 
    System.out.println("error");
    return "error";
  }
}

Please let me know if you need any further information from me to help out.

Here's what's required on the action side to demonstrate the problem:

public class StateStudentAction extends ActionSupport {
    public List<String> getCountryCodes() {
        String a[] = new String[] { "A", "B", "C", "D" }; 
        return Arrays.asList(a); 
    }
}

(If you want to be super helpful you'd add the imports so someone could cut-and-paste and execute.)

And on the JSP side:

<s:select list="CountryCodes2" name="countryCd" />

Here's a commented version of the original Java file with why things aren't relevant to the question:

// Useless comment.
package abc;//package name

// Useess comment.
import *.*//all imports

// None of these implementations are relevant.
public final class StateStudentAction extends ActionSupport  implements ServletRequestAware, ServletContextAware, SessionAware {

    // <UselessImplementationDetails>
    private Map session;
    public void setSession (Map session) {
        this.session = session;
    }

    private HttpServletRequest request;     
    public void setServletRequest(HttpServletRequest httpServletRequest) {         
        this.request = httpServletRequest;     
    }   

    private HttpServletResponse response;

    public void setServletResponse(HttpServletResponse httpServletReponse) {
        this.response = httpServletReponse;
    }

    private ServletContext servletContext;

    public void setServletContext(ServletContext servletContext) {
        this.servletContext = servletContext;
    }
    public ServletContext getServletContext() {
        return servletContext;
    }
    private StudentForm f = new StudentForm();
    public StudentForm getf() {return f;}
    public void setf(StudentForm f) {this.f = f;}

    private List<String> searchEngine;
    public List<String> getSearchEngine() {
        return searchEngine;
    }

    public void setSearchEngine(List<String> searchEngine) {
        this.searchEngine = searchEngine;
    }
    // </UselessImplementationDetails>

    private List<String> CountryCodes2;

    public List<String> getCountryCodes() {
        return CountryCodes2;
    }

    public void setCountryCodes(List<String> CountryCodes2) {
        this.CountryCodes2 = CountryCodes2;
    }

    public String execute() throws Exception {
        // Not useful, and redundant; you implement `SessionAware`
        HttpSession session = request.getSession();

        // Not helpful to the question
        System.out.println("Inside StateStudentAction.execute");

        // *Super*-not helpful
        BeanManager beanManager = (BeanManager)this.servletContext.getAttribute("beanManager");

        try {
          // *How* you get the codes is not relevant: you're not having
          // an issue with the *data*, you're having an issue with making
          // the data visible on the view layer.
          CountryCodesController ccc = beanManager.getCountryCodesController();
          // CountryCodesController is another class from where I am getting the value of CountryCodes2 and I have checked to confirm that it is not null

          ArrayList CountryCodes2 = new ArrayList(ccc.getCountryCodes());
          System.out.println(CountryCodes2);//This prints CountryCodes2 but in hashcode format, not string format

          // Completely irrelevant (and the functionality is 
          // located in the wrong place.)
          searchEngine = new ArrayList<String>();
          searchEngine.add("google.com");
          searchEngine.add("bing.com");
          searchEngine.add("yahoo.com");
      } catch (Exception ex) {
          Logger.error("Exception", ex);
      } 

      // This is misleading logic: no matter what you return
      // `error`, even if there's no error. This will be confusing
      // to anybody reading the code.
      System.out.println("error");
      return "error";
  }
}

Here is what I did to fix this issue, thanks to Dave Newton!:

In the JSP:

<%@ taglib prefix="s" uri="/struts-tags" %>
<%@ taglib uri="http://tiles.apache.org/tags-tiles" prefix="tiles" %>
<s:select list="countryCodes2"/>

In the Action Class:

public final class StateStudentAction extends ActionSupport {

private List<String> countryCodes2;
public List<String> getCountryCodes2() {
    return countryCodes2;
}
public void setCountryCodes2(List<String> countryCodes2) {
    this.countryCodes2 = countryCodes2;
}
public String execute() throws Exception {
try {
    countryCodes2 = new ArrayList(ccc.getCountryCodes());
    } catch (Exception ex) {
    }
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM