2011-06-26 3 views
2

私はこの問題を解決するのに苦労していますが、運がない。なぜstruts2がジェネリックで動作しないのですか

以下はサンプルコードです.jspでeditメソッドを呼び出すと、すべての値が入力されますが、変更を保存する場合はOGNLで例外が発生します。 おそらく、stuts2はジェネリックスでうまく動作しません。どんな提案も高く評価されます。おそらく...タイプミスフォームが、終了タグは、単なるHTMLのformタグである:S:

public abstract class ActionHelper<T> extends ActionSupport{ 

    protected T entity; 
    protected Integer id; 

    public void setId(Integer id){ 
     this.id=id; 
    } 

    public void setEntity(T entity){ 
     this.entity=entity; 
    } 

    public void getEntity(T entity){ 
     this.entity=entity; 
    } 

    public String edit(){ 
     this.entity=fillEntity(); 
     return "edit"; 
    } 
    public String save(){ 
     genericDao.save(entity); 

    } 
    protected abstract T fillEntity(); 
} 

public class PersonAction extends ActionHelper<Person>{ 
    Person fillEntity(){ 
     return genericDao.find(id,Person.class); 
    } 
} 

<s:form action="person_save" method="post"> <s:hidden name="entity.id"> <s:textfield name="entity.name"> <s:textfield name="entity.surname"> </form>

+0

関連するJSPコードを投稿できますか? – nmc

+0

例外は何ですか? – joelittlejohn

+1

この[質問](http://stackoverflow.com/questions/2516407/how-to-declare-a-generics-action-in-struts2-xml-file)が役立つかもしれません。 – doctrey

答えて

1

フォームの開始タグはありますか?

あなたはfillEntityのデフォルトのアクセス指定子を使用してJavaの命名規則れてフォローしていない、これを試してみてください。

public Person getEntity(){ 
    return genericDao.find(id,Person.class); 
} 

今OGNLはPersonActionに「実体」を見つけることができるようになります。

+0

編集/保存するべきActionEntityごとにActionHelperを再利用したい。 Struts 2はPersonにバインドできず、クラスを認識できません。 – Zemzela

+2

さて、私は今参照してください。これはStruts2の欠点ではありません.Javaが一般的に動作する方法です。タイプ情報は、実行時ではなく、コンパイラに対してのみ存在します。これは、「型消去」と呼ばれます。これはより複雑なストーリー(および可能な答え)のための簡単なストーリーです:http://stackoverflow.com/questions/1509896/getting-around-type-erasure-in-java – Quaternion

関連する問題