gpt4 book ai didi

java - Play Framework : Submit button doesn't seem to work

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:54:00 27 4
gpt4 key购买 nike

我正在尝试自定义 Play framework -Computer database with Java例子。但是,我似乎无法使“编辑”页面上的提交按钮正常工作(已注释)。这是表格:

@(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:当我点击按钮时会发生什么:什么都不可见。但是当我仔细观察时,它似乎重定向到同一页面(更新页面)。但基于 Controller ,我希望它返回到基于 GO_HOME 变量的主页。

应用程序.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 类中看起来您是 defining a constraint 全名:

@Constraints.Required
public String fullname;

但是您没有在表单中包含 fullname。您能否从模型中删除约束或将字段添加到您的表单中?

同样与您的失败无关,您在 View 中引用了表单字段 fb,但 Provider 模型没有该值?应该是feedback吧?

关于java - Play Framework : Submit button doesn't seem to work,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36929944/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com