我在我的网络应用程序中使用jquery和struts2。现在我需要将谷歌地图嵌入我的网页并添加一些标记。我使用jquery.getJSON()命令向struts2动作发送请求。
在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>
在网页
中<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>
在动作中,我从数据库中读取数据并创建对象列表,然后使用jackson json处理器的ObjectMapper将List转换为JSON String。最后,我将JSON String作为返回值返回。
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;
}
当我运行Web应用程序时,控制台中显示JSON String:
[
{"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}
]
然而,它给了我错误。
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)
我已经搜索了很长时间但仍无法找到解决方案。 等待你的帮助。非常感谢。
答案 0 :(得分:6)
自从我使用Struts2以来已经很长时间了,但是从内存中你没有从动作返回你的响应,你返回一个简单的字符串名称来确定配置中要使用哪个结果,然后是控制器类的属性可供您查看。返回json
结果时,会将这些属性序列化为JSON以便返回。
所以你想要更像这样的东西:
private List latlngList;
public List getLatLngList() {
return latlngList;
}
public String find() throws JsonGenerationException, JsonMappingException, IOException {
latlngList = new ArrayList()
/*
latlngList.add(...);
*/
return "success";
}
然后在您的javascript中获取生成的JSON的LatLngList
属性(请注意从data
到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>
答案 1 :(得分:2)
因为你正在看你正在使用JSON插件并直接渲染结果。意义也不起作用。操作返回结果的名称,而不是原始数据。 (或者他们可以返回原始数据,但必须以不同方式处理。)