我是
Jquery和Struts的新手.
我需要使用Ajax函数将表单数据发送到Struts2动作类.
我需要使用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>
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,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>
我希望这适用于任何面临同样问题的人:)再次感谢