2013-03-29 59 views
0

我非常惱火的下列問題。我是Struts2的初學者。 我試圖做到這一點Struts2動作> jsp>動作

  1. 獲取對象的列表從數據庫中我的行動(OK)
  2. 打印它JSP(OK)
  3. 這份名單出來作爲JSP可編輯表..我想修改,然後提交回相同的行動,以將其保存在我的數據庫(失敗。當我打電話給我的方法與 <s:submit action="myaction" method="mymethod">列表我以前從db填充現在是null.How我可以解決嗎?

我發現一些話題談論支柱s2攔截器用myaction反射注入數據。

public class CurrentOra { 
    private int idCommessa; 
    private String descrizioneCommessa; 
    private int idCliente; 
    private String descrizioneCliente; 
    private List<OreTimesheetGiorno> orePerCommessa; 

    public int getIdCommessa() { 
     return idCommessa; 
    } 
} 

public class OreTimesheetGiorno { 

    private int numeroGiorno; 
    private OreTimesheet oreTimesheet; 
    public int getNumeroGiorno() { 
     return numeroGiorno; 
    } 
    public void setNumeroGiorno(int numeroGiorno) { 
     this.numeroGiorno = numeroGiorno; 
    } 
    public OreTimesheet getOreTimesheet() { 
     return oreTimesheet; 
    } 
    public void setOreTimesheet(OreTimesheet oreTimesheet) { 
     this.oreTimesheet = oreTimesheet; 
    } 

} 

這是我的對象結構,並在JSP我

<s:iterator value="listOre" > 

    <tr class="giornoSettimana giornoUno"> 

    <td><s:property value="descrizioneCliente"/></td> 
    <td><s:property value="descrizioneCommessa"/></td> 

    <s:iterator value="orePerCommessa"> 
     <td> 
      <input type="text" 
        class="oreConsuntivazione" 
        maxlength="2" 
        giorno = "<s:property value="numeroGiorno" />" 
        value="<s:property value="oreTimesheet.numeroOre" />"> 
     </td> 

    </s:iterator> 

    </tr> 

</s:iterator> 

基本上我需要遍歷向客戶展示,併爲每一位客戶每小時打印處理它。然後,我應該可以編輯每個小時並將其保存回分區

+0

當然'List'是'null' - 你的動作在每一個傳入請求中重新產生。您需要從網頁返回一些_something_以告訴操作要刪除的內容。 [This](http://code.google.com/p/struts2-jquery/wiki/EditGrid)應該給你一個出發點。 –

+0

請注意:從頁面上的表單提交時,所有表單元素都包含在請求中。這意味着如果你想從表單提交信息,它必須存儲在表單中的表單元素中。把你的清單放在表格中。 – DwB

+0

Ofc我試圖提交的所有字段都在表單內。我只在列表中遇到問題。從jsp> action傳入輸入沒有任何問題。我只有當我用 cuz打印一個列表時,纔會出現問題標籤迭代器沒有名稱屬性來綁定請求併發回操作.. – Federik

回答

0

每次向Struts發出請求時,它都會創建一個新的Action對象實例並通過攔截器填充它。其中之一是params攔截器。它負責迭代參數映射並將參數值注入到操作屬性中。該規則採用適合操作屬性名稱的參數名稱,並在操作中具有相應的設置器。所以,你可以通過先

@DefaultInterceptorRef(value = "defaultStack") 

或明或暗地解決這個以確保堆棧上的所有必要的攔截器,和你的行動將引用此堆棧。

然後確保你的屬性在攔截器調用它們之前被初始化。

最後最有趣的話題是表單字段名稱應符合OGNL能夠從valueStack中檢索的規則。如果你的對象結構很簡單,那麼字段名直接映射到動作屬性。如果你使用集合,那麼你最好看my answers,這會讓你更好地瞭解問題。

0
  • giorno不是HTML input字段的有效屬性;
  • CurrentOra不見了getterssetters(但我想你簡單地在這裏發表時忽略它們,btw仔細檢查它們);

這就是說,報值回來了,你需要使用IteratorStatus.index屬性來指定Listindex

假設用戶只能輸入的小時數,並沒有改變天數,嘗試如下:

<s:iterator value="listOre" > 

    <tr class="giornoSettimana giornoUno"> 

    <td><s:property value="descrizioneCliente"/></td> 
    <td><s:property value="descrizioneCommessa"/></td> 

    <s:iterator value="orePerCommessa" status="ctr" > 
     <td> 
      <s:hidden name="orePerCommessa[%{#ctr.index}].numeroGiorno" /> 

      <s:property value="numeroGiorno" /> 

      <s:textfield 
       name="orePerCommessa[%{#ctr.index}].oreTimesheet.numeroOre" 
       cssClass = "oreConsuntivazione" 
       maxlength="2" /> 

     </td> 

    </s:iterator> 

    </tr> 

</s:iterator> 

這順便說不會阻止用戶破解你的代碼改變隱藏值爲orePerCommessa.numeroGiorno;如果將它們從用戶操作中保存起來很重要,那麼一旦從數據庫讀取數據後將它們放入會話中,然後在JSP中顯示它們(使用<s:property/>),但不要發佈它們(不要放置<s:hidden />),然後在回發頁面時,從會話中檢索它們並以某種方式匹配它們(例如,您可以將HashMap與它們一起用作值,並將計數器作爲鍵存儲在JSP中)......這樣用戶就可以混淆鍵,而不是價值。