Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Send Form Data to Struts2 Action Class using Ajax

I am new to Jquery and Struts. I need to send the form data to Struts2 action class using 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 the 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");
        }
    });
}

Struts.XML file is written as :


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

I have given execute method in 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 as how can I receive this for data in the action class.

like image 587
Tushar Avatar asked Nov 11 '14 12:11

Tushar


1 Answers

Thanks for the help. But issue is resolved, 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 anyone facing the same issue :) Thanks again

like image 170
Tushar Avatar answered Oct 07 '22 09:10

Tushar