1

I use Struts 2 and Ajax. I am not able to get parameter in Action class. I have a form like this:

<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>

When I examine the request which is sent in HTTP Reader :

Content-length contains libelle=Test&id=13

My request is good !

In my action class :

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);
    }
}

In my Action libelle and this. libelle are null. I don't know if I am making mistakes. If someone could give me help. Thanks.

I found my problem but not a solution. I inspect the html code generated and i see the following one :

<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>

So MyOtherAction is executed first before my sendAjaxRequest. How to remove action MyOtherAction in my form ? Thanks for reading.

4

2 回答 2

0

您可以像这样在表单上绑定提交事件:

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

当事件被捕获时,sendAjaxRequest()函数被执行,由于函数返回 false,表单将不会被提交到/path/MyOtherAction.action

于 2012-10-03T08:17:23.347 回答
0

如果您使用的是 jQuery,您可以尝试使用preventDefault()。这种方式MyOtherAction不会在提交时执行。

于 2012-09-03T14:10:54.733 回答