return JSON message from struts2 action
I use jquery and struts2 in my web application. Now I need to开发者_开发问答 embed a google map into my web page and add some marker. I use jquery.getJSON() command to send request to struts2 action.
In the struts.xml
<package name="ajax" namespace="/ajax" extends="json-default">
<action name="LatLngList" class="com.test.ajax.Action" method="find">
<result type="json"></result>
</action>
</package>
In the web page
<script type="text/javascript">
function readLatLng() {
var latitude = new Array();
var longitude = new Array();
$.getJSON("ajax/LatLngList.action?id=9", function(data){
$.each(data, function(i,latlng){
latitude.push(latlng.latitude);
longitude.push(latlng.longitude);
});
});
}
</script>
In the action, I read data from database and create a list of object and then use jackson json processor's ObjectMapper to convert the List to JSON String. Finally, I return the JSON String as return value.
public String find() throws JsonGenerationException, JsonMappingException, IOException {
List latlngList = new ArrayList();
/*
latlngList.add(...);
*/
ObjectMapper mapper = new ObjectMapper();
String str = mapper.writeValueAsString(latlngList);
System.out.println(str);
return str;
}
When I run the web application, the JSON String show in the console:
[
{"latitude":37.7935697,"longitude":121.181969},
{"latitude":37.7852119,"longitude":121.1759833},
{"latitude":37.7858117,"longitude":121.1250904},
{"latitude":37.794129,"longitude":121.1229535},
{"latitude":37.7974078,"longitude":121.0874301},
{"latitude":37.8280231,"longitude":121.049312}
]
However, It give me the error.
No result defined for action com.test.ajax.Action and result [{"latitude":37.7935697,"longitude":121.181969},{"latitude":37.7852119,"longitude":121.1759833},{"latitude":37.7858117,"longitude":121.1250904},{"latitude":37.794129,"longitude":121.1229535},{"latitude":37.7974078,"longitude":121.0874301},{"latitude":37.8280231,"longitude":121.049312}]
at com.opensymphony.xwork2.DefaultActionInvocation.executeResult(DefaultActionInvocation.java:375)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:277)
at com.opensymphony.xwork2.interceptor.DefaultWorkflowInterceptor.doIntercept(DefaultWorkflowInterceptor.java:176)
at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
at com.opensymphony.xwork2.validator.ValidationInterceptor.doIntercept(ValidationInterceptor.java:263)
at org.apache.struts2.interceptor.validation.AnnotationValidationInterceptor.doIntercept(AnnotationValidationInterceptor.java:68)
at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
at com.opensymphony.xwork2.interceptor.ConversionErrorInterceptor.intercept(ConversionErrorInterceptor.java:133)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
at com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:207)
at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
at com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:207)
at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
at com.opensymphony.xwork2.interceptor.StaticParametersInterceptor.intercept(StaticParametersInterceptor.java:190)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
at org.apache.struts2.interceptor.MultiselectInterceptor.intercept(MultiselectInterceptor.java:75)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
at org.apache.struts2.interceptor.CheckboxInterceptor.intercept(CheckboxInterceptor.java:94)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
at org.apache.struts2.interceptor.FileUploadInterceptor.intercept(FileUploadInterceptor.java:243)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
at com.opensymphony.xwork2.interceptor.ModelDrivenInterceptor.intercept(ModelDrivenInterceptor.java:100)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
at com.opensymphony.xwork2.interceptor.ScopedModelDrivenInterceptor.intercept(ScopedModelDrivenInterceptor.java:141)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
at org.apache.struts2.interceptor.debugging.DebuggingInterceptor.intercept(DebuggingInterceptor.java:270)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
at com.opensymphony.xwork2.interceptor.ChainingInterceptor.intercept(ChainingInterceptor.java:145)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
at com.opensymphony.xwork2.interceptor.PrepareInterceptor.doIntercept(PrepareInterceptor.java:171)
at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
at com.opensymphony.xwork2.interceptor.I18nInterceptor.intercept(I18nInterceptor.java:176)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
at org.apache.struts2.interceptor.ServletConfigInterceptor.intercept(ServletConfigInterceptor.java:164)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
at com.opensymphony.xwork2.interceptor.AliasInterceptor.intercept(AliasInterceptor.java:190)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
at com.opensymphony.xwork2.interceptor.ExceptionMappingInterceptor.intercept(ExceptionMappingInterceptor.java:187)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
at org.apache.struts2.impl.StrutsActionProxy.execute(StrutsActionProxy.java:52)
at org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:498)
at org.apache.struts2.dispatcher.ng.ExecuteOperations.executeAction(ExecuteOperations.java:77)
at org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.doFilter(StrutsPrepareAndExecuteFilter.java:91)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:291)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:859)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:602)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
at java.lang.Thread.run(Thread.java:680)
I have search for a long time but still cannot find out the solution. Waiting for your help. Thank you very much.
It's been a long time since I used Struts2, but from memory you don't return your response from the action, you return a simple string name that determines which result in the config to use, then the properties on the controller class become available to your view. When returning a json
result these properties are serialized for you into JSON to be returned.
So you want something more like this:
private List latlngList;
public List getLatLngList() {
return latlngList;
}
public String find() throws JsonGenerationException, JsonMappingException, IOException {
latlngList = new ArrayList()
/*
latlngList.add(...);
*/
return "success";
}
And then in your javascript grab the LatLngList
property of the resulting JSON (note the change from data
to data.latLngList
):
<script type="text/javascript">
function readLatLng() {
var latitude = new Array();
var longitude = new Array();
$.getJSON("ajax/LatLngList.action?id=9", function(data){
$.each(data.latLngList, function(i,latlng){
latitude.push(latlng.latitude);
longitude.push(latlng.longitude);
});
});
}
</script>
Because you're doing a mix between looking like you're using the JSON plugin, and directly rendering the result. Meaning neither will work. Actions return the name of a result, not raw data. (Or rather they can return raw data, but then must be handled differently.)
Check out the documentation for the JSON plugin.
精彩评论