2

テーブル(永続データベーステーブル)からjspページの「フォーム」の「選択」リストに列をフェッチしたい。struts2と休止状態を使用しています。

私の列は「name」で、テーブルは「Category」です。マッピング構成とBeanクラスを作成しました。

jspページの「形式」のコード:

<s:select label="Select Category :" name="cname" list="categoryList" />

私のアクションクラス:

package com.rambo.action;

import beans.Category;
import com.opensymphony.xwork2.ActionSupport;
import java.util.ArrayList;
import java.util.List;
import org.hibernate.Session;

public class FindCategory extends ActionSupport {

    private List<Category> cl = new ArrayList<Category>();
    private List<String> categoryList = new ArrayList<String>();

    @Override
    public String execute() throws Exception {
        Session session = null;
        try {
            session = HibernateUtil.getSessionFactory().getCurrentSession();
            session.beginTransaction();
            this.cl = (List<Category>) session.createQuery("from Category").list();
            if (this.cl.isEmpty()) {
                this.addActionError("Sorry.. No category Available. Try again Later.!");
                return ERROR;
            }
            for (int i = cl.size()-1; i >= 0; i--) {
                categoryList.add(cl.get(i).getName());
            }
            session.getTransaction().commit();
        } catch (Exception e) {
            this.addActionError("Oops. An Error Encountered...!");
            return ERROR;
        }
        return SUCCESS;
    }
}

Category.hbm.xmlでのマッピング:

<property name="name" type="string">
            <column name="NAME" length="20" not-null="true" />
        </property>

Bean「Category.java」のゲッターとセッター:

public String getName() {
        return this.name;
    }

    public void setName(String name) {
        this.name = name;
    }

私のglassfishサーバーは次のようなエラーを表示します:

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

root cause tag 'select', field 'list', name 'cname': The requested list key 'categoryList' could not be resolved as a collection/array/map/enumeration/iterator type. Example: people or people.{name} - [unknown location]

誰かがエラーを引き起こす可能性があるものを指摘できますか..?前もって感謝します。

4

1 に答える 1

3

のパブリックゲッターを作成しますcategoryList。そうしないと、タグはリストにアクセスできません。

また、あなたはアクション、IMOであまりにも多くの仕事をしています。

于 2012-07-12T15:16:37.507 に答える