2016-07-28 38 views
0

我試圖弄清楚如何將在Action類中創建的值傳遞給模型(在我的情況下是jsp視圖)。我是Struts 2框架的新手。從動作傳遞值到Struts 2中的模型

我的情況:

  1. 我從請求URL的參數。
  2. 我使用這個值來生成我自己的類的對象 - 產品(我在執行方法中執行它)。
  3. 然後我想將Product類對象的列表注入到jsp視圖中。

我的問題是 - 如何在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

謝謝你的明確解釋。提供吸氣劑後,所需的對象在視圖中可見:) – rosmat

相關問題