【发布时间】:2015-05-13 06:34:17
【问题描述】:
您好,我是 Play 框架的新手。我想在 Play 框架中使用 MYSQL 数据库实现 CRUD 操作。
我能够成功删除、更新整个表,但无法成功删除单个记录。我想从 html 传递值。
index.scala.html
@(message: String)
@main("Welcome to Play") {
<ul id="bars">
</ul>
<form action="@routes.Application.addBar()" method="post">
<label for="name"> Enter Your name</label> <input name="name"/> <br>
<label for="place"> Enter Your place</label> <input name="place"/> <br>
<input type="submit"/>
</form>
<form action="@routes.Application.getBars()" method="get">
<label for="retrieve"> Retrieve the details from table </label>
<input type="submit"/>
</form>
<form action="@routes.Application.deleteBars()" method="get">
<label for="Delete"> Delete all the details from table </label>
<input type="submit"/>
</form>
<form action="@routes.Application.updateBar()" method="post">
<label for="name"> Enter Your name</label> <input name="name"/> <br>
<label for="place"> Enter Your place</label> <input name="place"/> <br>
<input type="submit"/>
</form>
}
应用程序.java
package controllers;
import com.avaje.ebean.Ebean;
import com.avaje.ebean.SqlUpdate;
import java.util.List;
import models.Bar;
import play.*;
import play.data.Form;
import play.db.ebean.Model;
import play.mvc.*;
import play.twirl.api.Content;
import views.html.*;
import play.libs.Json;
public class Application extends Controller {
public static Result index() {
int rowCount1 = Bar.find.findRowCount();
return ok(index.render("Your new application is ready."));
}
public static Result addBar() {
Bar bar = Form.form(Bar.class).bindFromRequest().get();
bar.save();
return redirect(routes.Application.index());
}
public static Result Login() {
Bar bar = Form.form(Bar.class).bindFromRequest().get();
bar.save();
return redirect(routes.Application.index());
}
public static Result getBars(){
List<Bar> bars = new Model.Finder(String.class, Bar.class).all();
return ok(Json.toJson(bars));
}
public static Result deleteBars() {
SqlUpdate down = Ebean.createSqlUpdate("DELETE FROM bar");
down.execute();
return redirect(routes.Application.index());
}
public static Result updateBar() {
SqlUpdate down = Ebean.createSqlUpdate("UPDATE bar SET place = 'asa'");
down.execute();
return ok();
}
}
路线如下:
GET / controllers.Application.index()
POST /bars2 controllers.Application.addBar()
GET /bars controllers.Application.getBars()
GET /bars1 controllers.Application.deleteBars()
POST /updateBar/ controllers.Application.updateBar()
GET /updateBar1/ controllers.Application.Login()
# Map static resources from the /public folder to the /assets URL path
GET /assets/*file controllers.Assets.at(path="/public", file)
我想做的是:
- 从 HTML 中传递名称以进行删除,以便我可以删除记录。
- 从 HTML 中传递名称、位置以进行更新。
【问题讨论】:
标签: java mysql playframework