Struts 2选择不从DAO填充的标记

时间:2014-08-07 23:16:31

标签: java struts2 struts glassfish-4

我有这个DAO课程,

public class CountriesDAO {

    static final Logger logger = LogManager.getLogger(CountriesDAO.class.getName());

    public List<SelectCountriesBean> getAllCountries() {
        java.sql.Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        List<SelectCountriesBean> countries = new ArrayList<>();
        try {
            CreateConnection.loadProperties();
            System.out.println("Beginning database connection to retrieve countries list");
            conn = CreateConnection.getConnection();
            pstmt = conn.prepareStatement("select * from KIRAN.TBHHCOUNTRY");
            rs = pstmt.executeQuery();
            while (rs.next()) {
                SelectCountriesBean countrieslist = new SelectCountriesBean();
                countrieslist.setCNCOUNTRYCODE(rs.getString("CNCOUNTRYCODE"));
                countrieslist.setCNCOUNTRYNAME(rs.getString("CNCOUNTRYNAME"));
                countries.add(countrieslist);
            }
        } catch (SQLException ex) {
            logger.error("Countries retrival failed due to SQL Error", ex);
        }
        return countries;
    }
}

这是我的豆,

 public class SelectCountriesBean {
        private String CNCOUNTRYCODE;
        private String CNCOUNTRYNAME;

        /**
         * @return the CNCOUNTRYCODE
         */
        public String getCNCOUNTRYCODE() {
            return CNCOUNTRYCODE;
        }

        /**
         * @param CNCOUNTRYCODE the CNCOUNTRYCODE to set
         */
        public void setCNCOUNTRYCODE(String CNCOUNTRYCODE) {
            this.CNCOUNTRYCODE = CNCOUNTRYCODE;
        }

        /**
         * @return the CNCOUNTRYNAME
         */
        public String getCNCOUNTRYNAME() {
            return CNCOUNTRYNAME;
        }

        /**
         * @param CNCOUNTRYNAME the CNCOUNTRYNAME to set
         */
        public void setCNCOUNTRYNAME(String CNCOUNTRYNAME) {
            this.CNCOUNTRYNAME = CNCOUNTRYNAME;
        }
    }

This is my action

public class getAllCountries extends ActionSupport implements Preparable {
     private List<SelectCountriesBean> countriesList;
     CountriesDAO allCountriesDAO = new CountriesDAO();

    public getAllCountries() {


    }
     @Override
    public void prepare(){

    }
    @Override
    public String execute() throws Exception {  
        countriesList = allCountriesDAO.getAllCountries();
       return "success";
    }

    /**
     * @return the countriesList
     */
    public List getCountriesList() {
        return countriesList;
    }

    /**
     * @param countriesList the countriesList to set
     */
    public void setCountriesList(List countriesList) {
        this.countriesList = countriesList;
    }


    }

这是我的JSP

  <s:select name="COLCOUNTRY"  list="countriesList"  listKey="CNCOUNTRYCODE"  listValue="CNCOUNTRYNAME"  required="true"></s:select>

这是我的struts.xml

<action name="getAllCountries" class="helpers.getAllCountries" method="execute" > 
            <result name="success">/WEB-INF/views/CreateAccountForm.jsp</result>
       </action> 

我得到以下信息,

org.apache.jasper.JasperException: tag 'select', field 'list', name 'COLCOUNTRY': The requested list key 'countriesList' could not be resolved as a collection/array/map/enumeration/iterator type. Example: people or people.{name} - [unknown location]

我现在已经没想到了为什么countriesList没有像Collection一样解析。有些人可以看看并分享一些想法。

2 个答案:

答案 0 :(得分:0)

有可能getCountriesList()方法返回null。

在运行时,系统无法告知null的类型,因此会返回该错误。

答案 1 :(得分:0)

<s:select name="COLCOUNTRY"  list="countriesList"  listKey="CNCOUNTRYCODE"  listValue="CNCOUNTRYNAME"  required="true"></s:select>

必须改为

<s:select name="COLCOUNTRY"  list="allCountries"  listKey="CNCOUNTRYCODE"  listValue="CNCOUNTRYNAME"  required="true"></s:select>

由于allCountries返回国家列表而不是countryList

或者你有一个名为class =&#34; helpers.getAllCountries&#34;的动作类是真的吗?  它有一个getCountriesList()方法?

为什么你的班级名称是getAllCountries?我认为将这些名字提供给班级是不好的。