我在通过ajax调用设置动作类String时遇到问题。这是我的代码
DashBoardGraphAction.java
public class DashBoardGraphAction extends DispatchAction
{
private String testString;
public ActionForward getTransactionsByStatus(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception
{
System.out.println(fromDate);
return null;
}
public String getTestString() {
return testString;
}
public void setTestString(String testString) {
this.testString = testString;
}
Ajax调用如下:
<script>
var testString = "testName";
$.ajax({
url: '${pageContext.request.contextPath}/gettransactionData.do?parameter=getTransactionsByStatus',
type: 'POST',
data: 'testString='+testString,
dataType: 'json',
success: function(data)
{
str = data;
},
async:false,
error: function(XMLHttpRequest, textStatus, errorThrown){
alert('Error ' + textStatus);
alert(errorThrown);
alert(XMLHttpRequest.responseText);
}
});
</script>
的struts-config.xml
<action path="/gettransactionData" parameter="parameter"
type="com.pp.dashboard.DashBoardGraphAction">
<forward name = "getTransactionsByStatus" path = "/jsp/pp/dashboardReportStatus.jsp" />
<forward name = "getTransactionsByProcessor" path = "/jsp/pp/dashboardReportProcessorWise.jsp" />
</action>
当动作调用时,我将testString作为null。我尽力解决它,但找不到遗漏的东西。非常感谢任何人。
答案 0 :(得分:1)
定义一个ActionForm类
public class Person extends ActionForm {
private String testString;
public String getTestString() {
return testString;
}
public void setTestString(String testString) {
this.testString = testString;
}
}
您的Struts配置将如下所示
<struts-config>
<form-beans>
<form-bean name="formClass" type="com.forms.Person"/>
</form-beans>
<action-mappings>
<action name="formClass" path="/gettransactionData" parameter="parameter"
type="com.pp.dashboard.DashBoardGraphAction">
<forward name = "getTransactionsByStatus" path = "/jsp/pp/dashboardReportStatus.jsp" />
<forward name = "getTransactionsByProcessor" path = "/jsp/pp/dashboardReportProcessorWise.jsp" />
</action>
</action-mappings>
</struts-config>
像这样更改动作类方法
public ActionForward getTransactionsByStatus(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception
{
Person p = (Person) form;
System.out.println("Testing************************"+p.getTestString());
return null;
}
检查这些更改,希望它能够正常工作