2012-09-03 92 views
1

我使用Struts 2和Ajax。我無法在Action類中獲取參數。我有一個表格是這樣的:Struts 2獲取Action類中的參數

<s:form id="form" onSubmit="sendAjaxRequest();"> 
    <s:textfield name="libelle" label="Libelle" /> 
    <s:textfield name="id" label="id" /> 
    <s:submit value="Valider" /> 
    <s:reset value="Effacer" /> 
</s:form> 

當我審視這是在HTTP閱讀器發送的請求:

Content-length contains libelle=Test&id=13 

我的要求很不錯!

在我的動作類:

public class MyAction extends ActionSupport { 
    private String libelle; 
    private Integer id; 

    public String getLibelle() { 
     return auteur; 
    } 

    public void setLibelle(String libelle) { 
     this.libelle = libelle; 
    } 

    public String getId() { 
     return id; 
    } 

    public void setId(Integer id) { 
     this.id = id; 
    } 
    public String execute() { 
     System.out.println("Libelle " + libelle); 
     System.out.println("Libelle " + this.libelle); 
    } 
} 

在我的行動libelle這。 libelle爲null。我不知道我是否犯錯。如果有人可以給我幫助。謝謝。

我發現我的問題,但沒有解決方案。我檢查生成的html代碼,我看到以下內容:

<form id="form" name="form" action="/path/MyOtherAction.action" method="post" onSubmit="sendAjaxRequest();"> 
    <table class="wwFormTable"> 
     <tr> 
      <td class="tdLabel"><label for="form_libelle" class="label">Libelle:</label></td> 
      <td><input type="text" name="libelle" value="" id="form_libelle"/></td> 
     </tr> 
     <tr> 
      <td class="tdLabel"><label for="form_id" class="label">id:</label></td> 
      <td><input type="text" name="ide" value="" id="form_id"/></td> 
     </tr> 
... 
      <td colspan="2"><div align="right"><input type="submit" id="form_0" value="Valider"/> 
.. 
      <td colspan="2"><div align="right"><input type="reset" value="Effacer"/> 
.. 
    </table> 
</form> 

所以MyOtherAction首先在我的sendAjaxRequest之前執行。如何在我的表單中刪除動作MyOtherAction? 感謝您的閱讀。

+0

你需要提供一個有效的動作,這個動作不會被編譯,因爲這個類沒有定義導師......所以考慮到你有一個運行時錯誤,很難說出發生了什麼。 – Quaternion

+0

四元數我正確,這是一個錯誤。 – Pracede

+0

請創建一個工作示例,然後剪切並粘貼它,我可以直觀地看出這也會失敗。 – Quaternion

回答

0

如果你使用的是jQuery,你可以試試preventDefault()。這種方式MyOtherAction不會在提交時執行。

0

可以綁定表單的提交事件是這樣的:

$(document).ready(function() {  
    $("#form").submit(function() { 
     sendAjaxRequest(); 
     return false; // it will stop the submit process 
    }); 
}); 

當事件是抓時,sendAjaxRequest()功能來執行,並且因爲函數返回false,窗體不會被提交給/path/MyOtherAction.action

相關問題