2016-07-28 22 views
0

Actionクラスで作成した値をモデル(jspビューの場合)に渡す方法を解明しようとします。私はStruts 2フレームワークが初めてです。Struts 2のActionからModelへの値の受け渡し

私の場合:

  1. 私は、リクエストされたURLからパラメータを取得します。
  2. 私は自分のクラスのオブジェクトを生成するためにこの値を使用します - 製品(私はそれを実行メソッドで行います)。
  3. そして、jspビューにProductクラスオブジェクトのリストを注入したいと思います。

私の質問は、どのようにjspビューで自分のクラスのオブジェクトを挿入できますか?

Actionクラスの私の実装:

public class ProductAction extends ActionSupport { 

private int n; 

public int getN() { 
    return n; 
} 

public void setN(int n) { 
    this.n = n; 
} 

public String execute() throws Exception { 
    List<Product> products = Service.getProducts(n);//I want to inject this to jsp view 
    return SUCCESS; 
} 
+0

デバッグの助けを求めるhttp://struts.apache.org/docs/tutorials.html –

+0

質問(「なぜこのコードは動作しないの?」)は、所望の動作が含まれている必要があり、特定の問題やエラーと質問自体の中でそれを再現するのに必要な最短のコード。明確な問題文がない質問は、他の読者にとって有用ではありません。参照:最小、完全、および検証可能な例を作成する方法。 –

答えて

0

あなたProductActionに入ってくるパラメータを注入するために、あなたは何でもあなたの内部で利用できるようにしたいパラメータを公開する必要がありますセッターを指定する方法のような多くをビューテクノロジの選択はjsp、ftl、vmなどのように起こります。そのためにはゲッターを提供する必要があります。

public class ProductAction extends ActionSupport { 
    private Integer n; 
    private Collection<Product> products; 

    @Override 
    public String execute() throws Exception { 
    // you may want to add logic for when no products or null is returned. 
    this.products = productService.getProducts(n); 
    return SUCCESS; 
    } 

    // this method allows 'n' to be visible to the view technology if needed. 
    public Integer getN() { 
    return n; 
    } 

    // this method allows the request to set 'n' 
    public void setN(Integer n) { 
    this.n = n; 
    } 

    // makes the collection of products visible in the view technology. 
    public Collection<Product> getProducts() { 
    return products; 
    }  
} 
+0

明確な説明をいただきありがとうございます。 getterを提供した後、目的のオブジェクトがビューに表示されます:) – rosmat

関連する問題