Javascript 来自java控制器的返回值
我是java控制器新手。我正在处理Play框架,我试图从控制器返回一个ArrayList,它给出了错误。 这样做的正确方式是什么?我的代码就在下面 另一个问题,我想写一个最简单的页面,其中包含html和javascript功能,这些功能调用Play框架控制器。是否有一个简单的平台,如游戏框架,但前端 此方法:Javascript 来自java控制器的返回值,javascript,java,angular,playframework,controller,Javascript,Java,Angular,Playframework,Controller,我是java控制器新手。我正在处理Play框架,我试图从控制器返回一个ArrayList,它给出了错误。 这样做的正确方式是什么?我的代码就在下面 另一个问题,我想写一个最简单的页面,其中包含html和javascript功能,这些功能调用Play框架控制器。是否有一个简单的平台,如游戏框架,但前端 此方法: public Result getComments(int postId) { ArrayList<Comment> comments = this.c
public Result getComments(int postId) {
ArrayList<Comment> comments = this.commentsDic.getComments(postId);
return ok(comments);
}
指向标记行上的routes文件:
# Routes
# This file defines all application routes (Higher priority routes first)
# ~~~~
# An example controller showing a sample home page
GET / controllers.HomeController.index
# An example controller showing how to use dependency injection
GET /count controllers.CountController.count
# An example controller showing how to write asynchronous code
GET /message controllers.AsyncController.message
****************the error points on this line:************
GET /comments
controllers.CommentsController.getComments(postId: Integer)
# Map static resources from the /public folder to the /assets URL path
GET /assets/*file
controllers.Assets.versioned(path="/public", file: Asset)
完整控制器代码:
package controllers;
import play.mvc.Controller;
import play.mvc.Result;
import services.CommentsDictionary;
import java.util.ArrayList;
import models.Comment;
import javax.inject.Inject;
import javax.inject.Singleton;
import java.util.List;
@Singleton
public class CommentsController extends Controller {
private final CommentsDictionary commentsDic;
@Inject
public CommentsController() {
this.commentsDic = new CommentsDictionary();
}
public List<Comment> getComments(int postId) {
List<Comment> comments = this.commentsDic.getComments(postId);
return comments;
}
}
包控制器;
导入play.mvc.Controller;
导入play.mvc.Result;
进口服务.CommentsDictionary;
导入java.util.ArrayList;
导入模型。评论;
导入javax.inject.inject;
导入javax.inject.Singleton;
导入java.util.List;
@独生子女
公共类CommentsController扩展控制器{
私人最终评论文学评论;
@注入
公共评论控制器(){
this.commentsDic=新的CommentsDictionary();
}
公共列表getComments(int postId){
列表注释=this.commentsDic.getComments(postId);
返回评论;
}
}
您想要哪种内容类型的HTTP响应
如果您想要Json,只需返回
ok(play.libs.Json.toJson(comments));
如果要按如下方式显示页面呈现模板:
ok(views.html.yourTemplate.render(comments));
进一步阅读的一些技巧:
您想要哪种内容类型的HTTP响应 如果您想要Json,只需返回
ok(play.libs.Json.toJson(comments));
如果要按如下方式显示页面呈现模板:
ok(views.html.yourTemplate.render(comments));
进一步阅读的一些技巧:
您可以共享结果类吗?您所说的结果类是什么意思?在您的第一个方法中,您提到了返回类型result。您可以共享完整的代码吗?result是java的默认对象。这不是我的目标。结果用于将简单对象(作为字符串)包装为来自java控制器的答案。这就是我所知道的。你能共享结果类吗?你说的结果类是什么意思?在你的第一个方法中,你提到了返回类型result。你能共享完整的代码吗?结果是java的默认对象。这不是我的目标。结果用于将简单对象(作为字符串)包装为来自java控制器的答案。这就是我所知道的。
ok(views.html.yourTemplate.render(comments));