从 struts2 操作返回 JSON 消息

发布于 2024-12-04 18:54:32 字数 8075 浏览 0 评论 0原文

我在我的 Web 应用程序中使用 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 将列表转换为 JSON 字符串。最后,我返回 JSON 字符串作为返回值。

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 字符串显示在控制台中:

[
{"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)

我找了好久还是没找到解决办法。 等待您的帮助。非常感谢。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

路弥 2024-12-11 18:54:32

自从我使用 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 属性(注意从 datadata.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>

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>
不可一世的女人 2024-12-11 18:54:32

因为您正在混合使用 JSON 插件和直接渲染结果。这意味着两者都不起作用。操作返回结果的名称,而不是原始数据。 (或者更确切地说,它们可以返回原始数据,但必须以不同的方式处理。)

查看 JSON 插件的文档。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文