Struts2 动态下拉列表错误:无法将请求的列表键'CountryCodes2'解析为集合/数组/映射/枚举/迭代器类型



在将此帖子标记为重复之前,请仔细阅读,因为我知道stackoverflow上有类似的帖子,我尝试了所有这些解决方案,但似乎对我的方案没有任何作用。在下面的代码中,您会注意到我已经合并了其他类似帖子的解决方案,但它们无法解决我的问题,因此请提供帮助。我正在努力将当前应用程序从struts1转换为struts2,并且必须将所有HTML标签更改为" S"标签。我已经能够让其他所有工作来阐明下拉菜单。在Struts1中,下拉列表使用'Collections =" CountryCodes2"',但在Struts2中,它们具有'List = CountryCodes2'(TAG)。在过去的几天里,我一直在尝试解决这个struts2下拉问题,我一直在互联网上寻找各种解决方案,但似乎没有任何作用。有了我尝试的一切,我似乎又回到一个错误:

"标签'select',field'列表',name'countrycd':所请求的列表键'countryCodes2'无法作为集合/array/map/map/emumeration/iterator类型解决。"

我能够以hashcode格式获得countryCodes2的价值,但似乎无法在下拉列表中填充它。在Web浏览器中,我正在调用实际操作,而不仅仅是JSP。不知道我在哪里错了。任何帮助将不胜感激。

这是我的JSP页面:

<%@ 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 -->

这是我的动作课:

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

请告诉我,如果您需要我的任何进一步的信息来提供帮助。

这是动作方面所需的证明问题的要求:

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

(如果您想成为 super 有用

在JSP侧:

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

这是原始Java文件的评论版本,为什么与问题无关:

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

这是我为解决此问题所做的工作,这要归功于Dave Newton!:

在JSP中:

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

在动作类中:

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) {
    }
  }
}

最新更新