javascript 使用 Ajax 将表单数据发送到 Struts2 操作类
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26865476/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Send Form Data to Struts2 Action Class using Ajax
提问by Tushar
I am new to Jquery and Struts. I need to send the form data to Struts2 action class using Ajax function.
我是 Jquery 和 Struts 的新手。我需要使用 Ajax 函数将表单数据发送到 Struts2 操作类。
My HTML form element is set as :
我的 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>
I am using the JQuery Script as :
我将 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 file is written as :
Struts.XML 文件写成:
<action name="launchapptest" class="com.ge.wd.action.LaunchAppTestAction">
<result type="json">
</result>
</action>
I have given execute method in Action Class :
我在 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);
}
Please help me as how can I receive this for data in the action class.
请帮助我如何在操作类中接收数据。
采纳答案by Tushar
Thanks for the help. But issue is resolved, I changed the code to :
谢谢您的帮助。但问题已解决,我将代码更改为:
HTML:
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 :
和 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
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
我希望这适用于任何面临同样问题的人:) 再次感谢