8

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.

11
  • Output at console is - Process Date : null Commented Nov 11, 2014 at 13:02
  • 1
    Why shouldn't be null? You are sending only value of your input. To what it should be mapped? Commented Nov 11, 2014 at 13:07
  • please help me how to map the value Commented Nov 11, 2014 at 13:11
  • how can I map the value to a java variable? Commented Nov 11, 2014 at 13:12
  • If it isn't the ajax call, how do you do it? Commented Nov 11, 2014 at 13:19

1 Answer 1

7

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.