我使用以下 JavaScript/jQuery 函数进行远程过程调用。
<script src="../js/jquery-1.8.0.min.js" type="text/javascript"></script>
<s:url var="testJsonUrl" action="testJsonAction"/>
var timeout;
var request;
$(document).ready(function(){
$("#btnUser").click(function(){
if(!request)
{
request = $.ajax({
datatype:"json",
type: "GET",
data: JSON.stringify({jsonrpc:'2.0', method:'getUser', id:'jsonrpc'}),
contentType: "application/json-rpc; charset=utf-8",
url: "<s:property value='#testJsonUrl'/>",
success: function(response)
{
var user = response.result;
alert(JSON.stringify(user)); //Always alerts "undefined".
},
complete: function()
{
timeout = request = null;
},
error: function(request, status, error)
{
if(status!=="timeout"&&status!=="abort")
{
alert(status+" : "+error);
}
}
});
timeout = setTimeout(function() {
if(request)
{
request.abort();
alert("The request has been timed out.");
}
}, 30000);
}
});
});
当单击按钮时,调用上述函数,如下所示。
<s:form namespace="/admin_side" action="Test" validate="true" id="dataForm" name="dataForm">
<input type="button" name="btnUser" id="btnUser" value="Click"/>
</s:form>
调用方法的动作类如下。
import com.opensymphony.xwork2.ActionSupport;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.InterceptorRef;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.ResultPath;
import org.apache.struts2.json.annotations.SMDMethod;
import util.User;
@Namespace("/admin_side")
@ResultPath("/WEB-INF/content")
@ParentPackage(value = "json-default")
public final class TestAction extends ActionSupport
{
private User user;
public TestAction() {}
@SMDMethod
public User getUser()
{
user = new User();
user.setName("Tiny");
user.setLocation("India");
try {
user.setDob(new SimpleDateFormat("dd-MMM-YYYY").parse("29-Feb-2000"));
} catch (ParseException ex) {
Logger.getLogger(TestAction.class.getName()).log(Level.SEVERE, null, ex);
}
return user;
}
public void setUser(User user) {
this.user = user;
}
@Action(value = "testJsonAction",
results = {
@Result(type = "json", params = {"enableSMD", "true", "excludeNullProperties", "true"})},
interceptorRefs = {
@InterceptorRef(value = "json", params = {"enableSMD", "true"})})
public String executeAction() throws Exception {
return SUCCESS;
}
@Action(value = "Test",
results = {
@Result(name = ActionSupport.SUCCESS, location = "Test.jsp"),
@Result(name = ActionSupport.INPUT, location = "Test.jsp")},
interceptorRefs = {
@InterceptorRef(value = "defaultStack", params = {"params.acceptParamNames", "", "params.excludeMethods", "load", "validation.validateAnnotatedMethodOnly", "true"})})
public String load() throws Exception {
//This method is just needed to return a view on page load.
return ActionSupport.SUCCESS;
}
}
User
班级:
public class User
{
private String name;
private Date dob;
private String location;
//Setters & getters.
}
当单击给定按钮但未单击时,应调用带有注释的getUser()
方法(在操作类中)。TestAction
@SMDMethod
alert(JSON.stringify(user));
jQuery 函数的处理程序中的这一行success
总是 alerts undefined
。
在地址栏中使用直接链接,例如,
http://localhost:8080/TestStruts/admin_side/testJsonAction.action
返回以下字符串。
{
"methods": [{
"name": "getUser",
"parameters": []
}],
"serviceType": "JSON-RPC",
"serviceUrl": "\/TestStruts\/admin_side\/testJsonAction.action",
"version": ".1"
}
这里缺少什么?为什么不JSON-RPC
工作?
PS:我正在使用Struts2-json-plugin-2.3.16
相同版本的框架。