在Struts2的操作类Ajax响应 [英] Ajax response in struts2 action class
问题描述
我想实现Ajax在JSP中使用的Struts2(避免表单提交)。我用AJAX code通过URL来传递请求Struts2的行动。但是从struts2的响应是不是获取填充在JAP。它显示空值。我的code。使用AJAX是如下调用行动的jsp。
I am trying to implement ajax in jsp(avoid form submit) using struts2. I used ajax code to pass request via url to struts2 action. But the response from struts2 is not get populated in jap. Its showing "null" value. My code to call a action in jsp using AJAX is as Follows.
function ajaxEditFunctionCall(){
var xmlHttp;
var url = "ajaxcall.action?stateName="+frm.stateName.value;
try{
xmlHttp=new XMLHttpRequest();
}catch (e){
try{
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
}catch (e){
try{
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}catch (e){
alert("Your browser does not support AJAX!");
return false;
}
}
}
alert(1);
xmlHttp.onreadystatechange = showMessage;
alert(2);
xmlHttp.open("GET", URL, true);
alert(3);
xmlHttp.send(null);
}
function showMessage() {
alert("Inside Show Message1");
alert(xmlhttp.readyState);
if(xmlhttp.readyState==4)
{
alert("Inside Show Message2&ReadyState4");
alert(xmlhttp.responseText);
}
}
Included following code in Action Class:
public String ajaxcall() throws Exception{
System.out.println("Inside AjaxCall");
String errorXml = "This is a Sample to Check";
response.setContentType("text/html");
response.setHeader("Cache-Control", "no-cache");
response.setContentType("text/html");
response.getWriter().write(errorXml);
return null;
}
code包含在struts.xml中:
Code included in Struts.xml:
<action name="ajaxcall" class="com.logic.action.CustomerAction" method="ajaxcall">
<result name="success" >/pages/customer/addCustomer.jsp</result>
</action>
我认为错误是在动作类响应语句并在struts.xml中。任何一个可以请帮我解决这个问题。在此先感谢。
I think the error is in the action class-response statement and in struts.xml. Can any one please help me to fix this issue. Thanks in Advance.
推荐答案
下面,校正需要做的JavaScript函数。当你说 VAR XMLHTTP
,它有一个范围,在函数内部 ajaxEditFunctionCall
而不是为 showMessage
。此外, XMLHTTP
在 showMessage()
不一样的 XMLHTTP
在 ajaxEditFunctionCall
。因此,保持 VAR XMLHTTP
宣布全球和进行更正。这里是工作code:
Here, the correction needs to be done to the JavaScript function. When you say var xmlHttp
, it has a scope inside the function ajaxEditFunctionCall
and not for the showMessage
. Also, xmlhttp
in showMessage()
is not same as xmlHttp
object in ajaxEditFunctionCall
. So, keep the var xmlHttp
declaration global and make the corrections. Here is the working code:
<script type="text/javascript">
var xmlHttp;
function ajaxEditFunctionCall(){
var URL = "ajaxcall.action?stateName=State1";
try{
xmlHttp=new XMLHttpRequest();
}catch (e){
try{
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
}catch (e){
try{
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}catch (e){
alert("Your browser does not support AJAX!");
return false;
}
}
}
//alert(1);
xmlHttp.onreadystatechange = showMessage;
//alert(2);
xmlHttp.open("GET", URL, true);
//alert(3);
xmlHttp.send(null);
}
function showMessage() {
//alert("Inside Show Message1");
//alert(xmlHttp.readyState);
if(xmlHttp.readyState==4)
{
alert("Inside Show Message2&ReadyState4");
alert(xmlHttp.responseText);
}
}
</script>
Java的code是:
The Java code is:
public class CustomerAction extends ActionSupport implements ServletResponseAware {
HttpServletResponse response;
public String ajaxcall() {
System.out.println("Inside AjaxCall");
String errorXml = "This is a Sample to Check";
response.setContentType("text/html");
response.setHeader("Cache-Control", "no-cache");
try {
response.getWriter().write(errorXml);
} catch (IOException ioe) {
ioe.printStackTrace();
}
return null;
}
public void setServletResponse(HttpServletResponse response) {
this.response = response;
}
}
在struts.xml中是:
The struts.xml is:
<struts>
<constant name="struts.devMode" value="true" />
<package name="default" extends="struts-default">
<action name="ajaxcall" class="com.logic.action.CustomerAction" method="ajaxcall">
<result name="success" >/pages/customer/addCustomer.jsp</result>
</action>
</package>
</struts>
这篇关于在Struts2的操作类Ajax响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!