播放框架:提交按钮似乎不起作用
Play framework: Submit button doesn't seem to work
我正在尝试自定义 Play framework -Computer database with Java 示例。但是,我似乎无法使 "edit" 页面上的提交按钮起作用(已注释)。表格如下:
@(id: Long, computerForm: Form[Provider])
@import helper._
@implicitFieldConstructor = @{ FieldConstructor(twitterBootstrapInput.render) }
@main {
<h1>Provide fb*emphasized text*</h1>
@form(routes.Application.update(id)) {
<fieldset>
@inputText(computerForm("fb"), '_label -> "Feedback", '_help -> "")
@inputText(computerForm("rating"), '_label -> "Rating", '_help -> "")
</fieldset>
<div class="actions">
<!-- This button does nothing!-->
<input type="submit" value="Submit fb!" class="btn primary"> or
<a href="@routes.Application.list()" class="btn">Cancel</a>
</div>
}
}
这是 Application.java
中的 update
方法:
public static Result update(Long id) {
Form<Provider> computerForm = form(Provider.class).bindFromRequest();
if(computerForm.hasErrors()) {
return badRequest(editForm.render(id, computerForm));
}
computerForm.get().update(id);
flash("success", "Provider " + computerForm.get().fullname + " has been updated");
return GO_HOME;
}
我做错了什么?
编辑 - 更多详细信息。
型号:Provider.java
/**
* Company entity managed by Ebean
*/
@Entity
public class Provider extends Model {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
public Long id;
@Constraints.Required
public String fullname;
public String tech;
public String experience;
public String email;
public String feedback;
public int rating;
/**
* Generic query helper for entity Provider with id Long
*/
public static Model.Finder<Long, Provider> find = new Model.Finder<Long, Provider>(Long.class, Provider.class);
public static Map<String, String> options() {
LinkedHashMap<String, String> options = new LinkedHashMap<String, String>();
for (Provider c : Provider.find.orderBy("tech").findList()) {
options.put(c.id.toString(), c.fullname);
}
return options;
}
public static Page<Provider> page(int page, int pageSize, String sortBy, String order, String filter) {
return find.where().ilike(sortBy, "%" + filter + "%").findPagingList(pageSize).setFetchAhead(false)
.getPage(page);
}
}
问题 2:当我点击按钮时发生了什么:没有任何可见的东西。但是当我仔细观察时,它似乎重定向到同一页面(更新页面)。但是基于控制器,我希望它基于 GO_HOME
变量返回主页。
Application.java
package controllers;
import play.mvc.*;
import play.data.*;
import static play.data.Form.*;
import views.html.*;
import models.*;
/**
* Manage a database of Providers
*/
public class Application extends Controller {
/**
* This result directly redirect to application home.
*/
public static Result GO_HOME = redirect(
routes.Application.list(0, "fullname", "asc", "")
);
/**
* Handle default path requests, redirect to computers list
*/
public static Result index() {
return GO_HOME;
}
/**
* Display the paginated list of Providers.
*
* @param page Current page number (starts from 0)
* @param sortBy Column to be sorted
* @param order Sort order (either asc or desc)
* @param filter Filter applied on computer names
*/
public static Result list(int page, String sortBy, String order, String filter) {
return ok(
list.render(
Provider.page(page, 10, sortBy, order, filter),
sortBy, order, filter
)
);
}
/**
* Display the 'edit form' of a existing Computer.
*
* @param id Id of the computer to edit
* // TODO : Fix wiring and button actions
*/
public static Result edit(Long id) {
Form<Provider> computerForm = form(Provider.class).fill(
Provider.find.byId(id)
);
return ok(
editForm.render(id, computerForm)
);
}
/**
* Handle the 'edit form' submission
*
* @param id Id of the computer to edit
*/
public static Result update(Long id) {
Form<Provider> computerForm = form(Provider.class).bindFromRequest();
if(computerForm.hasErrors()) {
return badRequest(editForm.render(id, computerForm));
}
computerForm.get().update(id);
flash("success", "Computer " + computerForm.get().fullname + " has been updated");
return GO_HOME;
}
/**
* Display the 'new Provider form'.
*/
public static Result create() {
Form<Provider> computerForm = form(Provider.class);
return ok(
createForm.render(computerForm)
);
}
/**
* Handle the 'new Provider form' submission
*/
public static Result save() {
Form<Provider> computerForm = form(Provider.class).bindFromRequest();
if(computerForm.hasErrors()) {
return badRequest(createForm.render(computerForm));
}
computerForm.get().save();
flash("success", "Provider " + computerForm.get().fullname + " has been created");
return GO_HOME;
}
}
请原谅,我最熟悉 Scala,但我怀疑您在提交时没有通过表单验证并返回相同的页面:
if(computerForm.hasErrors()) {
return badRequest(createForm.render(computerForm));
}
在你的 Provider
class 看起来你是 defining a constraint 对于 fullname
:
@Constraints.Required
public String fullname;
但是您没有在表单中包含 fullname
。您能否从模型中删除约束或将字段添加到您的表单中?
也与您的失败无关,您在视图中引用了表单字段 fb
,但 Provider 模型没有该值?应该是 feedback
?
我正在尝试自定义 Play framework -Computer database with Java 示例。但是,我似乎无法使 "edit" 页面上的提交按钮起作用(已注释)。表格如下:
@(id: Long, computerForm: Form[Provider])
@import helper._
@implicitFieldConstructor = @{ FieldConstructor(twitterBootstrapInput.render) }
@main {
<h1>Provide fb*emphasized text*</h1>
@form(routes.Application.update(id)) {
<fieldset>
@inputText(computerForm("fb"), '_label -> "Feedback", '_help -> "")
@inputText(computerForm("rating"), '_label -> "Rating", '_help -> "")
</fieldset>
<div class="actions">
<!-- This button does nothing!-->
<input type="submit" value="Submit fb!" class="btn primary"> or
<a href="@routes.Application.list()" class="btn">Cancel</a>
</div>
}
}
这是 Application.java
中的 update
方法:
public static Result update(Long id) {
Form<Provider> computerForm = form(Provider.class).bindFromRequest();
if(computerForm.hasErrors()) {
return badRequest(editForm.render(id, computerForm));
}
computerForm.get().update(id);
flash("success", "Provider " + computerForm.get().fullname + " has been updated");
return GO_HOME;
}
我做错了什么?
编辑 - 更多详细信息。
型号:Provider.java
/**
* Company entity managed by Ebean
*/
@Entity
public class Provider extends Model {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
public Long id;
@Constraints.Required
public String fullname;
public String tech;
public String experience;
public String email;
public String feedback;
public int rating;
/**
* Generic query helper for entity Provider with id Long
*/
public static Model.Finder<Long, Provider> find = new Model.Finder<Long, Provider>(Long.class, Provider.class);
public static Map<String, String> options() {
LinkedHashMap<String, String> options = new LinkedHashMap<String, String>();
for (Provider c : Provider.find.orderBy("tech").findList()) {
options.put(c.id.toString(), c.fullname);
}
return options;
}
public static Page<Provider> page(int page, int pageSize, String sortBy, String order, String filter) {
return find.where().ilike(sortBy, "%" + filter + "%").findPagingList(pageSize).setFetchAhead(false)
.getPage(page);
}
}
问题 2:当我点击按钮时发生了什么:没有任何可见的东西。但是当我仔细观察时,它似乎重定向到同一页面(更新页面)。但是基于控制器,我希望它基于 GO_HOME
变量返回主页。
Application.java
package controllers;
import play.mvc.*;
import play.data.*;
import static play.data.Form.*;
import views.html.*;
import models.*;
/**
* Manage a database of Providers
*/
public class Application extends Controller {
/**
* This result directly redirect to application home.
*/
public static Result GO_HOME = redirect(
routes.Application.list(0, "fullname", "asc", "")
);
/**
* Handle default path requests, redirect to computers list
*/
public static Result index() {
return GO_HOME;
}
/**
* Display the paginated list of Providers.
*
* @param page Current page number (starts from 0)
* @param sortBy Column to be sorted
* @param order Sort order (either asc or desc)
* @param filter Filter applied on computer names
*/
public static Result list(int page, String sortBy, String order, String filter) {
return ok(
list.render(
Provider.page(page, 10, sortBy, order, filter),
sortBy, order, filter
)
);
}
/**
* Display the 'edit form' of a existing Computer.
*
* @param id Id of the computer to edit
* // TODO : Fix wiring and button actions
*/
public static Result edit(Long id) {
Form<Provider> computerForm = form(Provider.class).fill(
Provider.find.byId(id)
);
return ok(
editForm.render(id, computerForm)
);
}
/**
* Handle the 'edit form' submission
*
* @param id Id of the computer to edit
*/
public static Result update(Long id) {
Form<Provider> computerForm = form(Provider.class).bindFromRequest();
if(computerForm.hasErrors()) {
return badRequest(editForm.render(id, computerForm));
}
computerForm.get().update(id);
flash("success", "Computer " + computerForm.get().fullname + " has been updated");
return GO_HOME;
}
/**
* Display the 'new Provider form'.
*/
public static Result create() {
Form<Provider> computerForm = form(Provider.class);
return ok(
createForm.render(computerForm)
);
}
/**
* Handle the 'new Provider form' submission
*/
public static Result save() {
Form<Provider> computerForm = form(Provider.class).bindFromRequest();
if(computerForm.hasErrors()) {
return badRequest(createForm.render(computerForm));
}
computerForm.get().save();
flash("success", "Provider " + computerForm.get().fullname + " has been created");
return GO_HOME;
}
}
请原谅,我最熟悉 Scala,但我怀疑您在提交时没有通过表单验证并返回相同的页面:
if(computerForm.hasErrors()) {
return badRequest(createForm.render(computerForm));
}
在你的 Provider
class 看起来你是 defining a constraint 对于 fullname
:
@Constraints.Required
public String fullname;
但是您没有在表单中包含 fullname
。您能否从模型中删除约束或将字段添加到您的表单中?
也与您的失败无关,您在视图中引用了表单字段 fb
,但 Provider 模型没有该值?应该是 feedback
?