我试图使用struts2在jsp(避免表单提交)中实现ajax.我使用ajax代码通过url将请求传递给struts2动作.但struts2的反应并没有在日本出现.它显示"空"值.我使用AJAX在jsp中调用动作的代码如下.
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;
}
Run Code Online (Sandbox Code Playgroud)
Struts.xml中包含的代码:
<action name="ajaxcall" class="com.logic.action.CustomerAction" method="ajaxcall">
<result name="success" >/pages/customer/addCustomer.jsp</result>
</action>
Run Code Online (Sandbox Code Playgroud)
我认为错误在action-response语句和struts.xml中.任何人都可以帮我解决这个问题.提前致谢.
我相信这个简单的代码应该适用于Ajax调用.下面的示例使用流结果,但您甚至可以使用JSON,XML或任何其他您想要的格式作为返回.如果请求来自脚本/ ajax或任何其他方式,服务器端struts2没有考虑到
public class TextResult extends ActionSupport {
private InputStream inputStream;
public InputStream getInputStream() {
return inputStream;
}
public String execute() throws Exception {
inputStream = new StringBufferInputStream("Hello World! This is a text string response from a Struts 2 Action.");
return SUCCESS;
}
}
Run Code Online (Sandbox Code Playgroud)
Struts.xml文件
<action name="text-result" class="actions.TextResult">
<result type="stream">
<param name="contentType">text/html</param>
<param name="inputName">inputStream</param>
</result>
</action>
Run Code Online (Sandbox Code Playgroud)
您可以在JS上使用上面的设置.成功调用Action将返回"Hello World!这是Struts 2 Action的文本字符串响应". 串
这是一个完整的工作代码作为Ajax调用
<head>
<script type="text/javascript">
var xmlHttp;
function ajaxEditFunctionCall(){
var URL = "welcomeAjax.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>
</head>
<body>
<s:form id="form">
<input type="button" onclick="ajaxEditFunctionCall()"/>
</s:form>
<body>
Run Code Online (Sandbox Code Playgroud)
这是Action类的代码
private InputStream inputStream;
public InputStream getInputStream() {
return inputStream;
}
public String ajax() throws Exception {
inputStream = new StringBufferInputStream("Hello World! This is a text string response from a Struts 2 Action.");
return SUCCESS;
}
Run Code Online (Sandbox Code Playgroud)
最后,我们需要在struts.xml文件中定义关系
<action name="welcomeAjax" class="com.demo.WelcomeAction" method="ajax">
<result type="stream">
<param name="contentType">text/html</param>
<param name="inputName">inputStream</param>
</result>
</action>
Run Code Online (Sandbox Code Playgroud)
上面的代码工作得很好,希望对你有所帮助.
| 归档时间: |
|
| 查看次数: |
21385 次 |
| 最近记录: |