繁体   English   中英

使用 Ajax 将表单数据发送到 Struts2 操作类

[英]Send Form Data to Struts2 Action Class using Ajax

我是 Jquery 和 Struts 的新手。 我需要使用 Ajax 函数将表单数据发送到 Struts2 操作类。

我的 HTML 表单元素设置为:

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

我使用 JQuery 脚本作为:

$('#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 文件写成:


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

我在 Action Class 中给出了 execute 方法:

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

请帮助我如何在操作类中接收数据。

谢谢您的帮助。 但问题已解决,我将代码更改为:

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>

查询:

$('#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");
        }
    });

和 JAVA 代码:

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>

我希望这适用于任何面临同样问题的人:) 再次感谢

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM