【问题标题】:How to use HTTP Methods in JAVA with Play Framework如何通过 Play 框架在 JAVA 中使用 HTTP 方法
【发布时间】:2014-12-08 02:01:29
【问题描述】:

我真的不太了解 Play Framework 如何与 Java 交互(我使用 Eclipse 作为 IDE)
所以如果我的解释有点难以理解,请多多包涵

我想要做的是一个 1 页的网页 (localhost:9000),它可能看起来像这样:

http://i.stack.imgur.com/JtRFF.png (我只有 1 个代表,所以我认为不能放图片;只是一个链接)

我需要知道的事情:
1) HTTP 方法如何与 Playframework 交互
2) 如何删除、编辑条目



目前我只知道如何填充学生


来自 controllers.Application.Java 的代码:

    public static Result addStudents(){

    Student student = Form.form(Student.class).bindFromRequest().get();
    student.save();
    return redirect(routes.Application.index());
    }

public static Result getStudents(){

List<Student> students = new Model.Finder(String.class, Student.class).all();
return ok(toJson(students));
}


models.Student.java 中的代码

@Entity
public class Student extends Model{

@Id
public String Id;
public String name;
public String course;

}


views.index.scala.html 中的代码
(我使用 Java 而不是 scala,但我真的不知道区别所以我保留了 intex。 scala.html)

@main("Welcome to Play") {
<form action="@routes.Application.addStudents()" method="post">
<input name="name">
<input course="course">
<input type="submit">
</form>
}


来自 conf.evolutions.routes 的代码

 POST    /student                   controllers.Application.addStudents()
 GET     /student                   controllers.Application.getStudents()

【问题讨论】:

  • w,为什么我会无缘无故被否决?我的问题无关紧要还是我做错了什么?

标签: java javascript playframework xmlhttprequest


【解决方案1】:

这完全取决于您的需求。您可能需要学习一些 ajax,然后学习如何在 play 框架中处理请求。你可以学习ajax基础here.Here你可以学习解析你可以用ajax发送的数据。

例如:

Javascript

var req = new XMLHttpRequest();
var data = "{"Hi"};
req.open("GET", "http://localhost:9000/service, true);
req.send(data);

Java

@BodyParser.Of(BodyParser.Json.class)
public static Result index() {
    RequestBody body = request().body();
    return ok(body.asJson()).as("text/json");
}

此外,您还需要导入 play.mvc.Http。我不确定 javascript,我的 ajax 有点生锈。

这将简单地回显响应,但它显示了基础知识。

【讨论】:

  • 感谢您的回答;只是一个简单的问题,我如何确定 request.body() 的内容?或者 request.body() 是否包含来自单个文本框的所有字符串?
猜你喜欢
  • 1970-01-01
  • 2018-10-10
  • 1970-01-01
  • 2013-09-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-07-11
相关资源
最近更新 更多