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

时间:2018-12-18 18:16:46

标签: java struts2

在您将此帖子标记为重复之前,请仔细阅读,因为我知道Stackoverflow上也有类似的帖子,并且我已经尝试了所有这些解决方案,但对于我的情况似乎没有任何作用。在下面的代码中,您会注意到我从其他类似的帖子中合并了一些解决方案,但是它们不能解决我的问题,因此请提供帮助。 我正在将当前的应用程序从Struts1转换为Struts2,并且不得不将所有html标签更改为's'标签。除下拉菜单外,我已经能够使其他所有功能正常工作。在Struts1中,下拉列表使用的是“ collections =“ CountryCodes2””,但在Struts2中,下拉列表使用的是“ list = CountryCodes2”(标记)。 在过去的几天里,我一直在尝试解决这个Struts2下拉问题,我一直在整个Internet上寻找各种解决方案,但似乎无济于事。我尝试了一切,似乎又回到了一个错误:

“标签'选择',字段'列表',名称'countryCd':不能将请求的列表键'CountryCodes2'解析为集合/数组/地图/枚举/迭代器类型。”

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

这是我的JSP页面:

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

这是我的动作课:

{{1}}

如果您需要我的进一步帮助,请告诉我。

2 个答案:

答案 0 :(得分:1)

以下是操作方面要说明的问题:

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

答案 1 :(得分:0)

这是我为解决此问题所做的工作,这要归功于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) {
    }
  }
}