玩!使用 JQuery 渲染框架示例列表

发布于 2024-11-19 20:37:41 字数 4739 浏览 1 评论 0原文

需要一些帮助,我有一个服务器端控制器功能:

public static void getNews(){
    List<Thread> threads = Thread.findAll();
    renderJSON(threads);
}

和前端请求:

<script type="text/javascript">
$.getJSON('@{Application.getNews()}', function(threads) {
      var items = [];

      $.each(threads, function(title, text) {
        items.push("<div>" + text + "</div>");
      });
    });
</script>

但它不起作用。控制台异常列表:

Internal Server Error (500) for request GET /application/getnews

Execution exception (In /app/controllers/Application.java around line 43)
IllegalStateException occured : circular reference error   Offending field: thre
ads    Offending object: preserveType: true, type: class models.Thread, obj: Thr
ead[21]

play.exceptions.JavaExecutionException: circular reference error
  Offending field: threads

  Offending object: preserveType: true, type: class models.Thread, obj: Thread[2
1]
        at play.mvc.ActionInvoker.invoke(ActionInvoker.java:227)
        at Invocation.HTTP Request(Play!)
Caused by: java.lang.IllegalStateException: circular reference error
  Offending field: threads

  Offending object: preserveType: true, type: class models.Thread, obj: Thread[2
1]
        at com.google.gson.CircularReferenceException.createDetailedException(Ci
rcularReferenceException.java:43)
        at com.google.gson.JsonSerializationVisitor.visitFieldUsingCustomHandler
(JsonSerializationVisitor.java:212)
        at com.google.gson.ObjectNavigator.navigateClassFields(ObjectNavigator.j
ava:158)
        at com.google.gson.ObjectNavigator.accept(ObjectNavigator.java:131)
        at com.google.gson.JsonSerializationVisitor.getJsonElementForChild(JsonS
erializationVisitor.java:147)
        at com.google.gson.JsonSerializationVisitor.addAsChildOfObject(JsonSeria
lizationVisitor.java:125)
        at com.google.gson.JsonSerializationVisitor.visitObjectField(JsonSeriali
zationVisitor.java:112)
        at com.google.gson.ObjectNavigator.navigateClassFields(ObjectNavigator.j
ava:164)
        at com.google.gson.ObjectNavigator.accept(ObjectNavigator.java:131)
        at com.google.gson.JsonSerializationContextDefault.serialize(JsonSeriali
zationContextDefault.java:56)
        at com.google.gson.JsonSerializationContextDefault.serialize(JsonSeriali
zationContextDefault.java:49)
        at com.google.gson.DefaultTypeAdapters$CollectionTypeAdapter.serialize(D
efaultTypeAdapters.java:528)
        at com.google.gson.DefaultTypeAdapters$CollectionTypeAdapter.serialize(D
efaultTypeAdapters.java:510)
        at com.google.gson.JsonSerializationVisitor.findAndInvokeCustomSerialize
r(JsonSerializationVisitor.java:184)
        at com.google.gson.JsonSerializationVisitor.visitUsingCustomHandler(Json
SerializationVisitor.java:160)
        at com.google.gson.ObjectNavigator.accept(ObjectNavigator.java:106)
        at com.google.gson.JsonSerializationContextDefault.serialize(JsonSeriali
zationContextDefault.java:56)
        at com.google.gson.Gson.toJsonTree(Gson.java:233)
        at com.google.gson.Gson.toJson(Gson.java:273)
        at com.google.gson.Gson.toJson(Gson.java:253)
        at play.mvc.results.RenderJson.<init>(RenderJson.java:20)
        at play.mvc.Controller.renderJSON(Controller.java:325)
        at controllers.Application.getNews(Application.java:43)
        at play.mvc.ActionInvoker.invokeWithContinuation(ActionInvoker.java:540)

        at play.mvc.ActionInvoker.invoke(ActionInvoker.java:498)
        at play.mvc.ActionInvoker.invokeControllerMethod(ActionInvoker.java:474)

        at play.mvc.ActionInvoker.invokeControllerMethod(ActionInvoker.java:469)

        at play.mvc.ActionInvoker.invoke(ActionInvoker.java:157)
        ... 1 more
Caused by: com.google.gson.CircularReferenceException: circular reference error
        at com.google.gson.JsonSerializationVisitor.start(JsonSerializationVisit
or.java:56)
        at com.google.gson.ObjectNavigator.accept(ObjectNavigator.java:114)
        at com.google.gson.JsonSerializationContextDefault.serialize(JsonSeriali
zationContextDefault.java:56)
        at com.google.gson.JsonSerializationContextDefault.serialize(JsonSeriali
zationContextDefault.java:49)
        at com.google.gson.DefaultTypeAdapters$CollectionTypeAdapter.serialize(D
efaultTypeAdapters.java:528)
        at com.google.gson.DefaultTypeAdapters$CollectionTypeAdapter.serialize(D
efaultTypeAdapters.java:510)
        at com.google.gson.JsonSerializationVisitor.findAndInvokeCustomSerialize
r(JsonSerializationVisitor.java:184)
        at com.google.gson.JsonSerializationVisitor.visitFieldUsingCustomHandler
(JsonSerializationVisitor.java:203)
        ... 27 more

need some help, i have a server-side Contoller function:

public static void getNews(){
    List<Thread> threads = Thread.findAll();
    renderJSON(threads);
}

And front-end request:

<script type="text/javascript">
$.getJSON('@{Application.getNews()}', function(threads) {
      var items = [];

      $.each(threads, function(title, text) {
        items.push("<div>" + text + "</div>");
      });
    });
</script>

But it doesn't work. Console exception listing:

Internal Server Error (500) for request GET /application/getnews

Execution exception (In /app/controllers/Application.java around line 43)
IllegalStateException occured : circular reference error   Offending field: thre
ads    Offending object: preserveType: true, type: class models.Thread, obj: Thr
ead[21]

play.exceptions.JavaExecutionException: circular reference error
  Offending field: threads

  Offending object: preserveType: true, type: class models.Thread, obj: Thread[2
1]
        at play.mvc.ActionInvoker.invoke(ActionInvoker.java:227)
        at Invocation.HTTP Request(Play!)
Caused by: java.lang.IllegalStateException: circular reference error
  Offending field: threads

  Offending object: preserveType: true, type: class models.Thread, obj: Thread[2
1]
        at com.google.gson.CircularReferenceException.createDetailedException(Ci
rcularReferenceException.java:43)
        at com.google.gson.JsonSerializationVisitor.visitFieldUsingCustomHandler
(JsonSerializationVisitor.java:212)
        at com.google.gson.ObjectNavigator.navigateClassFields(ObjectNavigator.j
ava:158)
        at com.google.gson.ObjectNavigator.accept(ObjectNavigator.java:131)
        at com.google.gson.JsonSerializationVisitor.getJsonElementForChild(JsonS
erializationVisitor.java:147)
        at com.google.gson.JsonSerializationVisitor.addAsChildOfObject(JsonSeria
lizationVisitor.java:125)
        at com.google.gson.JsonSerializationVisitor.visitObjectField(JsonSeriali
zationVisitor.java:112)
        at com.google.gson.ObjectNavigator.navigateClassFields(ObjectNavigator.j
ava:164)
        at com.google.gson.ObjectNavigator.accept(ObjectNavigator.java:131)
        at com.google.gson.JsonSerializationContextDefault.serialize(JsonSeriali
zationContextDefault.java:56)
        at com.google.gson.JsonSerializationContextDefault.serialize(JsonSeriali
zationContextDefault.java:49)
        at com.google.gson.DefaultTypeAdapters$CollectionTypeAdapter.serialize(D
efaultTypeAdapters.java:528)
        at com.google.gson.DefaultTypeAdapters$CollectionTypeAdapter.serialize(D
efaultTypeAdapters.java:510)
        at com.google.gson.JsonSerializationVisitor.findAndInvokeCustomSerialize
r(JsonSerializationVisitor.java:184)
        at com.google.gson.JsonSerializationVisitor.visitUsingCustomHandler(Json
SerializationVisitor.java:160)
        at com.google.gson.ObjectNavigator.accept(ObjectNavigator.java:106)
        at com.google.gson.JsonSerializationContextDefault.serialize(JsonSeriali
zationContextDefault.java:56)
        at com.google.gson.Gson.toJsonTree(Gson.java:233)
        at com.google.gson.Gson.toJson(Gson.java:273)
        at com.google.gson.Gson.toJson(Gson.java:253)
        at play.mvc.results.RenderJson.<init>(RenderJson.java:20)
        at play.mvc.Controller.renderJSON(Controller.java:325)
        at controllers.Application.getNews(Application.java:43)
        at play.mvc.ActionInvoker.invokeWithContinuation(ActionInvoker.java:540)

        at play.mvc.ActionInvoker.invoke(ActionInvoker.java:498)
        at play.mvc.ActionInvoker.invokeControllerMethod(ActionInvoker.java:474)

        at play.mvc.ActionInvoker.invokeControllerMethod(ActionInvoker.java:469)

        at play.mvc.ActionInvoker.invoke(ActionInvoker.java:157)
        ... 1 more
Caused by: com.google.gson.CircularReferenceException: circular reference error
        at com.google.gson.JsonSerializationVisitor.start(JsonSerializationVisit
or.java:56)
        at com.google.gson.ObjectNavigator.accept(ObjectNavigator.java:114)
        at com.google.gson.JsonSerializationContextDefault.serialize(JsonSeriali
zationContextDefault.java:56)
        at com.google.gson.JsonSerializationContextDefault.serialize(JsonSeriali
zationContextDefault.java:49)
        at com.google.gson.DefaultTypeAdapters$CollectionTypeAdapter.serialize(D
efaultTypeAdapters.java:528)
        at com.google.gson.DefaultTypeAdapters$CollectionTypeAdapter.serialize(D
efaultTypeAdapters.java:510)
        at com.google.gson.JsonSerializationVisitor.findAndInvokeCustomSerialize
r(JsonSerializationVisitor.java:184)
        at com.google.gson.JsonSerializationVisitor.visitFieldUsingCustomHandler
(JsonSerializationVisitor.java:203)
        ... 27 more

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

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

发布评论

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

评论(2

滥情空心 2024-11-26 20:37:42

这是 Play 中默认 JSON 引擎 GSON 的问题。切换到 Flex.JSON

解决您的具体问题 - http://www.lunatech-research.fr/archives/2011/04/20/play-framework-better-json-serialization-flexjson

This is an issue with GSON the default JSON engine in Play. Switch to Flex.JSON

Solution to your exact problem - http://www.lunatech-research.fr/archives/2011/04/20/play-framework-better-json-serialization-flexjson

っ〆星空下的拥抱 2024-11-26 20:37:42

试试这个。

在 URL 中放置一个简单的路由

JS URL ref
'route/'

在路由配置文件中添加此内容,其中 method 是控制器函数的名称。

GETroute/controllers.Application.method()

这应该有帮助。

Try this.

In the URL place a simple route

JS URL ref
'route/'

In the routes conf file add this where method is the name of the controller function.

GET route/ controllers.Application.method()

That should help.

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