【问题标题】:Playframework forwarding json values to a formPlayframework 将 json 值转发到表单
【发布时间】:2016-01-03 04:34:13
【问题描述】:

我是 Playframework 和 AngularJS 的新手,我在前端使用 AngularJS 并在后端使用 Playframework 开发应用程序。

我面临一种情况:当用户单击菜单链接(显示学生)时,它应该显示所有学生。在 play 动作中,数据以 json 格式发送。现在的问题是,如果数据作为 Json 发送,那么我看不到它应该转发到哪个页面的选项。 或者,我必须使用正常的播放框架样式,例如转发到带有值列表的页面。

//This is the current playframework code I use. Here I set the values to students form 
// This is not the way I want
public static Result getAllEmployees(){
        List<Student> all = Student.find.all();     
        JsonNode json = Json.toJson(all);
        return ok(views.html.students.render(all));
}


//This is the way I want
public static Result getAllEmployees(){
        List<Student> all = Student.find.all();     
        JsonNode json = Json.toJson(all);
        return ok(json); // no option for specifying the page.
}

还有办法吗?

【问题讨论】:

    标签: angularjs playframework playframework-2.0 playframework-2.2 playframework-2.3


    【解决方案1】:

    它们都是返回数据的有效方法,但第一个返回整个 html 页面,例如响应您视图中的 @routes.Employees.getAllEmployees 引用。第二个只返回 Json 内容类型响应,例如响应 Javascript 请求或 AngularJS $http.get。这些方法需要有不同的名称(它们不能都称为getAllEmployees)。它们都将在路由文件中具有条目。它们将是您编写它们的方式不同的条目,所以可能是这样的:

    GET /employee controllers.Employees.getAllEmployees
    GET /api/employee controllers.Employees.apiGetAllEmployees
    

    (免责声明:我还没有在AngularJS中做过,但原理应该差不多。)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-10
      • 2014-09-06
      • 2023-03-19
      • 1970-01-01
      相关资源
      最近更新 更多