Submit form data to Struts2 Action Class using Ajax

I am new to jQuery and Struts. I need to submit form data to a Struts2 action class using an Ajax function.

My HTML form element is set as:

<div class="input-append date" id="from_date"> <input type="text" id="processDate" name="processDate" /> <span class="add-on"><i class="icon-th"></i></span> </div> <div> <input id="submit-date" type="button" class="btn btn-primary" value="Search" /> </div> 

I am using jQuery Script as:

 $('#submit-date').click(function() { var processDate = $('#processDate').val(); alert(processDate); $.ajax({ type : "POST", url : "launchapptest", data : processDate, dataType : "json", success : function(result) { alert("Success"); } }); } 

The Struts.XML file is written as:


 <action name="launchapptest" class="com.ge.wd.action.LaunchAppTestAction"> <result type="json"> </result> </action> 

I gave the execution method in the Action Class:

 String processDate; public String getProcessDate() { return processDate; } public void setProcessDate(String processDate) { this.processDate = processDate; } public String execute() throws Exception { processDate=getProcessDate(); System.out.println("Process Date : "+processDate); } 

Please help me how can I get this for data in an action class.

+6
source share
1 answer

Thanks for the help. But the problem is solved, I changed the code to:

HTML:

 <div class="input-append date" id="from_date"> <input type="text" id="processDateForm" name="processDate"/> <span class="add-on"><i class="icon-th"></i></span> </div> <div> <input id="submit-date" type="button" class="btn btn-primary" value="Search" /> </div> 

JQuery:

 $('#submit-date').click(function() { var processDate = $('#processDateForm').val(); alert(processDate); $.ajax({ /* type : "POST", */ url : "launchapptest", /* contentType: "application/json; charset=utf-8", */ data : "processDateInput="+processDate, dataType : "json", async: true, success : function(result) { alert("Success"); } }); 

and JAVA code:

 public class LaunchAppTestAction extends ActionSupport { private static final long serialVersionUID = -367986889632883043L; //private ProcessDate pd = new ProcessDate(); private String processDateInput=null; public String getProcessDateInput() { return processDateInput; } public void setProcessDateInput(String processDateInput) { this.processDateInput = processDateInput; } public String execute() throws Exception { System.out.println("Process Date : "+processDateInput); return SUCCESS; }} 

Struts.xml

 <action name="launchapptest" class="com.ge.wd.action.LaunchAppTestAction"> <result name= "success" type="json"> </result> </action> 

I hope this works for those facing the same problem :) Thanks again

+5
source

Source: https://habr.com/ru/post/978029/


All Articles