struts 2选择标签异常

时间:2014-11-03 19:45:08

标签: java drop-down-menu struts2

我有一个需要使用Struts 2选择标记填充的下拉列表。我得到以下异常。哪里出错了?

    <s:select label="Country" headerKey="-1" headerValue="Select Country" list="countries"  
                                                listKey="key" listValue="label" name="searchForm.custCountry"/> 

在我的动作类中,我有以下声明,然后是getter和setter。

    ArrayList<DropDown> countries = new ArrayList<DropDown>();


    SEVERE: Servlet.service() for servlet jsp threw exception
    tag 'select', field 'list', name 'searchForm.custCountry': 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:240)
        at org.apache.struts2.components.Component.findValue(Component.java:333)
        at org.apache.struts2.components.ListUIBean.evaluateExtraParams(ListUIBean.java:80)
        at org.apache.struts2.components.Select.evaluateExtraParams(Select.java:105)
        at org.apache.struts2.components.UIBean.evaluateParams(UIBean.java:902)
        at org.apache.struts2.components.UIBean.end(UIBean.java:544)
        at org.apache.struts2.views.jsp.ComponentTagSupport.doEndTag(ComponentTagSupport.java:42)
        at org.apache.jsp.accountSearchDtls_jsp._jspx_meth_s_005fselect_005f0(accountSearchDtls_jsp.java:979)
        at org.apache.jsp.accountSearchDtls_jsp._jspx_meth_s_005fdiv_005f0(accountSearchDtls_jsp.java:935)
        at org.apache.jsp.accountSearchDtls_jsp._jspService(accountSearchDtls_jsp.java:521)
        at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:723)
        at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:388)
        at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
        at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:723)
        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.ng.filter.StrutsPrepareAndExecuteFilter.doFilter(StrutsPrepareAndExecuteFilter.java:96)
        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:127)
        at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
        at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
        at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
        at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:861)
        at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:606)
        at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
        at java.lang.Thread.run(Thread.java:745)

以下是我获取下拉列表数据的方式。我有这个就够了,如果不让我知道的话。

public ArrayList<DropDown> getCountrydd() {
        ArrayList<DropDown> countrydd = new ArrayList<DropDown>();
        try {
            OraConn conn = new OraConn();
            conn.getConnection();
            cstmt = conn.setProc("call mgm.getCountry(?)");
               cstmt.registerOutParameter(1, OracleTypes.CURSOR);
               cstmt.execute();
         rs = (OracleResultSet)cstmt.getObject(1);
               countrydd = rsToDropDown(rs);

        } catch (Exception e) {
               e.printStackTrace();
               //log.error("***getDSdropdowns*** ");
        } finally {
            closeORAConnection();
        }
        return countrydd;
    }

2 个答案:

答案 0 :(得分:1)

您需要更改代码以填充列表

public ArrayList<DropDown> getCountrydd() {

    List<DropDown> countrydd = new ArrayList<DropDown>();
    OraConn oraConn = new OraConn();
    Connection conn = null;
    CallableStatement cstmt = null;
    ResultSet rs = null;
    try {
        conn = oraConn.getConnection();
        cstmt = conn.prepareCall("{call mgm.getCountry(?)}");
        cstmt.registerOutParameter(1, OracleTypes.CURSOR);
        cstmt.executeUpdate();
        rs = (ResultSet)cstmt.getObject(1);

        rsToDropDown(rs, countrydd);

    } catch (Exception e) {
           e.printStackTrace();
           //log.error("***getDSdropdowns*** ");
    } finally {
        if (rs != null) 
            rs.close();              
        if (cstmt!= null) 
            cstmt.close();               
        if (conn != null) 
            conn.close();                   
    }
    return countrydd;
}

答案 1 :(得分:0)

我发现即使用户为空,我也会使用国家/地区值填充我的组合框或下拉列表,因此当我点击创建新用户按钮时,我得到了这些例外。现在我改变了我的代码,现在就是这样:

public String getProfile() throws Exception {

    user_name =(String) sessionMap.get("userid") ;

    MgmService service = new MgmService();
    userForm = new User();
    userForm = service.getUsersProfile(user_name);

    if(searched_user_name!=null){
        userForm = service.getUsersProfile(searched_user_name);
        countries= service.getCountrydd(); //This keep the content of the combobox by default on the list
        return "createEditUser";
    }
    else if(userForm!=null)
    {
        return "success";
    }

    else {
        addActionError("Invalid Login.");
        return "success";
    }
}

这是我之前的错误代码......

public String getProfile() throws Exception {

    user_name =(String) sessionMap.get("userid") ;

    MgmService service = new MgmService();
    userForm = new User();

    userForm = service.getUsersProfile(user_name);
    countries= service.getCountrydd(); //This keep the content of the combobox by default on the list
    if(searched_user_name!=null){
        userForm = service.getUsersProfile(searched_user_name);
        return "createEditUser";
    }
    else if(userForm!=null)
    {
        return "success";
    }

    else {
        addActionError("Invalid Login.");
        return "success";
    }
}