简体   繁体   English

如何在Java和Play Framework中使用HTTP方法

[英]How to use HTTP Methods in JAVA with Play Framework

I really dont understand much of How Play Framework interact with Java (i use eclipse as IDE) 我真的不太了解Play框架如何与Java交互(我使用eclipse作为IDE)
so please bear with me if my explanation is a bit incomprehensible 所以如果我的解释有点令人费解,请多多包涵

what i am trying to do is a 1-page webpage (localhost:9000) that could look like this: 我想做的是一个一页的网页(localhost:9000),看起来像这样:

http://i.stack.imgur.com/JtRFF.png (i only have 1 rep so i think can't put image; just a link) http://i.stack.imgur.com/JtRFF.png (我只有1个代表,所以我认为不能放图片;只是一个链接)

THINGS I NEED TO KNOW: 我需要知道的事情:
1) How Does HTTP Methods interact with Playframework 1)HTTP方法如何与Playframework交互
2) How can I Delete, Edit entries 2)如何删除,编辑条目



currently What i only know is how to populate students 目前,我只知道如何为学生提供服务


code from controllers.Application.Java: 来自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));
}


code from models.Student.java 来自models.Student.java的代码

@Entity
public class Student extends Model{

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

} }


code from views.index.scala.html 来自views.index.scala.html的代码
(I use Java not scala but i really don't know about the difference so i retained the intex.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>
}


code from conf.evolutions.routes 来自conf.evolutions.routes的代码

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

This completely depends on your needs. 这完全取决于您的需求。 You will probably need to learn some ajax, and then learn how to work with requests in the play framework. 您可能需要学习一些ajax,然后学习如何在play框架中处理请求。 You can learn ajax basics here . 您可以在这里学习ajax基础知识。 Here you can learn to parse the data you can send with ajax. 在这里,您可以学习解析可使用ajax发送的数据。

For example: 例如:

Javascript 使用Javascript

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

Java Java的

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

Also, you will need to import play.mvc.Http . 另外,您将需要导入play.mvc.Http I am not sure about the javascript, my ajax is a little rusty. 我不确定javascript,我的ajax有点生锈。

This will simply echo the response back , but it shows the basics. 这将简单地回显响应,但它显示了基础知识。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM