Struts2 and jsp form submit using jQuery ajax post and getting a toast as a message that comes and goes?

I use struts2 to develop my web application. In the registration form, I want to use query ajax so that the form is submitted asynchronously and upon successful submission of the form I can receive a success message on the server side in the form of a toast or an element that comes and goes.

It's good that I tried a lot and achieved this goal somewhere, but did not use jquery ajax , but regular javascript and ajax .

The code stream is as follows:

  • Home.jsp with the contained form.

      <s:form theme="simple" action="registrationForDemo"> <table> <tbody> <tr> <td><s:label value="username" /></td> <td><s:textfield name="uName" cssClass="textfield" /></td> </tr> <tr> <td><s:label value="password" /></td> <td><s:password name="password" cssClass="textfield" /></td> </tr> <tr> <td><s:label value="email" /></td> <td><s:textfield name="email" cssClass="textfield" /></td> </tr> <tr> <td><s:label value="contact no." /></td> <td><s:textfield name="contactNo" cssClass="textfield" /></td> </tr> <tr> <td><s:label value="location" /></td> <td><s:textfield name="location" cssClass="textfield" /></td> </tr> <tr> <td><s:label value="category" /></td> <td><s:select list="{'Hospital','Doctor','Clinic','Others'}" name="category"/></td> </tr> <tr> <td></td> <td><s:submit value="Submit" cssClass="button" /></td> </tr> </tbody> </table> </s:form> 
  • The file in which the mapping is defined, i.e. struts.xml.

struts.xml file

  • When the data is successfully stored in the database, the output page is again the same page.

Now, using ajax and jquery, I want to know how to achieve the required functionality. Where to convey the action from ... and how to move on ... ??

+4
source share
3 answers

Good solution for me was jquery.html() . Where I passed other html content to do this work. Jquery html

JQuery is great and easy.

+1
source

Well, if you have achieved this with simple java-script and AJAX, and I believe that it is no different from what you will do using jQuery. JQuery is no different from the fact that java-script is its library creating don java script to make the life of a developer happy.

You can submit your form using the jQuery post method and you can send a line from the server upon successful registration, something like

 $.ajax({ type: 'POST', url: url, data: data, success: success, dataType: dataType }); 

For more precise control, I suggest you use the Struts2-Json plugin and the Jquery Json function to send data to the server and receive results from the server, something like

 var formInput='val='+val; $.getJSON('myaction',formInput,function(data) { $.each(data.myData,function(index, value){ alert(index); }); }); 

See this lesson for more details.

+5
source
 //saving category button $("#save").on({click:function () { if (!($("#subcategory").val().length == 0)) { $.ajax({url:"savecategory", data:$("#savecategoryform").serialize(), type:"post", dataType:"json", success:function (data) { alert("Successfully Saved"); }}); } }}); <form id="savecategoryform"> <table id="addtable"> <thead id="subcategoryhead"> <tr> <td align="left"> <input type="text" name="strMainCategoryName" value="Main Category" width="80" id="maincategory" class="inputmaintext"> </td> <td colspan="2" align="left"> <img src="../images/plus.png" width="18" height="18" id="addmaincategory" hovertext="Add" class="link"> </td> </tr> </thead> <tbody id="subcategorybody"></tbody> </table> <input type="button" value="Save" id="save"> </form> 
0
source

All Articles