Java 表单中填写的值未保存在数据库中
我试图在表单中的Shop.email字段中设置预定义值,但它存储除一个字段(即电子邮件)之外的所有字段。 Shop.javaJava 表单中填写的值未保存在数据库中,java,database,playframework,ebean,Java,Database,Playframework,Ebean,我试图在表单中的Shop.email字段中设置预定义值,但它存储除一个字段(即电子邮件)之外的所有字段。 Shop.java package models; @Entity public class Shop extends Model { @Id @SequenceGenerator(name="shop_gen", sequenceName="shop_id_seq", allocationSize=1) @GeneratedValue(strategy=
package models;
@Entity
public class Shop extends Model {
@Id
@SequenceGenerator(name="shop_gen", sequenceName="shop_id_seq", allocationSize=1)
@GeneratedValue(strategy=GenerationType.SEQUENCE, generator="shop_gen")
@Column(name="id")
public Long id;
@Required
public String name;
@Required
public String addressLine1;
public String addressLine2;
public String addressLine3;
@Required
public String city;
@Required
public String town;
@Required
public String phoneNumber;
@ManyToOne
@JoinColumn(name="email",insertable=false, updatable=false,nullable=false)
public Member email;
public static Model.Finder<Long,Shop> find = new Model.Finder(Long.class, Shop.class);
}
package controllers;
public class ShopController extends Controller {
static Form<Shop> shopForm = Form.form(Shop.class);
public static Result blank() {
String loggedInUserEmail = session("email");
Shop shop = new Shop();
shop.email = Member.get(loggedInUserEmail);
shopForm.fill(shop);
return ok(views.html.shop.create.render(shopForm, loggedInUserEmail));
}
public static Result submit() {
Form<Shop> filledForm = shopForm.bindFromRequest();
if (filledForm.hasErrors()) {
String loggedInUserEmail = session("email");
return badRequest(views.html.shop.create.render(filledForm,
loggedInUserEmail));
} else {
Shop shop = filledForm.get();
Shop.create(shop);
return redirect(routes.ProductController.blank());
}
}
}
封装模型;
@实体
公共类商店扩展模型{
@身份证
@SequenceGenerator(name=“shop\u gen”,sequenceName=“shop\u id\u seq”,allocationSize=1)
@GeneratedValue(策略=GenerationType.SEQUENCE,generator=“shop\u gen”)
@列(name=“id”)
公共长id;
@必需的
公共字符串名称;
@必需的
公共字符串地址行1;
公共字符串地址行2;
公共字符串地址行3;
@必需的
公共字符串城市;
@必需的
公共线镇;
@必需的
公共字符串电话号码;
@许多酮
@JoinColumn(name=“email”,insertable=false,updateable=false,nullable=false)
公众成员电子邮件;
publicstaticmodel.Finder=newmodel.Finder(Long.class,Shop.class);
}
ShopController.java
package models;
@Entity
public class Shop extends Model {
@Id
@SequenceGenerator(name="shop_gen", sequenceName="shop_id_seq", allocationSize=1)
@GeneratedValue(strategy=GenerationType.SEQUENCE, generator="shop_gen")
@Column(name="id")
public Long id;
@Required
public String name;
@Required
public String addressLine1;
public String addressLine2;
public String addressLine3;
@Required
public String city;
@Required
public String town;
@Required
public String phoneNumber;
@ManyToOne
@JoinColumn(name="email",insertable=false, updatable=false,nullable=false)
public Member email;
public static Model.Finder<Long,Shop> find = new Model.Finder(Long.class, Shop.class);
}
package controllers;
public class ShopController extends Controller {
static Form<Shop> shopForm = Form.form(Shop.class);
public static Result blank() {
String loggedInUserEmail = session("email");
Shop shop = new Shop();
shop.email = Member.get(loggedInUserEmail);
shopForm.fill(shop);
return ok(views.html.shop.create.render(shopForm, loggedInUserEmail));
}
public static Result submit() {
Form<Shop> filledForm = shopForm.bindFromRequest();
if (filledForm.hasErrors()) {
String loggedInUserEmail = session("email");
return badRequest(views.html.shop.create.render(filledForm,
loggedInUserEmail));
} else {
Shop shop = filledForm.get();
Shop.create(shop);
return redirect(routes.ProductController.blank());
}
}
}
包控制器;
公共类ShopController扩展控制器{
静态表单shopForm=Form.Form(Shop.class);
公共静态结果空白(){
字符串loggedInUserEmail=会话(“电子邮件”);
商店=新商店();
shop.email=Member.get(loggedInUserEmail);
商店表格。填写(商店);
返回ok(views.html.shop.create.render(shopForm,loggedInUserEmail));
}
公共静态结果提交(){
Form filledForm=shopForm.bindFromRequest();
if(filledForm.hasErrors()){
字符串loggedInUserEmail=会话(“电子邮件”);
返回badRequest(views.html.shop.create.render(filledForm,
loggedInUserEmail);
}否则{
Shop Shop=filledForm.get();
创建(Shop);
返回重定向(routes.ProductController.blank());
}
}
}
createShop.scala.html
@(userForm: Form[models.Shop], user: String)
@import helper._
@import helper.twitterBootstrap._
@main(Html("Create Shop")) {
<fieldset>
<legend>Add a new shop</legend>
<p>To add a shop to this website fill in the form given below.Add as much information about your shop so the customer may know abot your shop more.</p>
@form(action = routes.ShopController.submit(), 'id -> "shopCreationForm", 'class -> "form-horizontal", 'role->"form") {
@inputText(userForm("name"), '_label -> "Shop Name",'class -> "form-control")
@inputText(userForm("addressLine1"), '_label -> "Address Line 1",'class -> "form-control")
@inputText(userForm("addressLine2"), '_label -> "Address Line 2",'class -> "form-control")
@inputText(userForm("addressLine3"), '_label -> "Address Line 3",'class -> "form-control")
@inputText(userForm("city"), '_label -> "City",'class -> "form-control")
@inputText(userForm("town"), '_label -> "Town",'class -> "form-control")
@inputText(userForm("phoneNumber"), '_label -> "Phone",'class -> "form-control")
<div class="form-group">
<label for="exampleInputEmail1">Owner Email</label>
<input type="email" class="form-control" id="exampleInputEmail1" placeholder="@user" readonly>
</div>
<div class="actions">
<input type="submit" class="btn btn-primary" value="Create">
<a href="@routes.ApplicationController.index" class="btn">Cancel</a>
</div>
</fieldset>
}
}
@(userForm:Form[models.Shop],user:String)
@导入助手_
@导入helper.twitterBootstrap_
@主(Html(“创建店铺”)){
增加一家新商店
若要将店铺添加到此网站,请填写以下表格。添加尽可能多的店铺信息,以便客户了解更多有关店铺的信息
@表单(action=routes.ShopController.submit(),'id->“shopCreationForm”,'class->“form horizontal”,'role->“form”){
@inputText(用户表单(“名称”),“\u标签->店铺名称”,“类->表单控件”)
@inputText(用户表单(“地址行1”),“\u标签->地址行1”,“类->表单控件”)
@inputText(用户表单(“地址行2”),“\u标签->地址行2”,“类->表单控件”)
@inputText(用户表单(“地址行3”),“_标签->地址行3”,“类->表单控件”)
@inputText(用户表单(“城市”),“\u标签->城市”,“类->表单控件”)
@inputText(用户表单(“城镇”),“\u标签->城镇”,“类->表单控件”)
@inputText(用户表单(“电话号码”),“_标签->电话”,“类->表单控件”)
所有者电子邮件
}
}
当我提交此表单时,它会保存数据库中除电子邮件字段值以外的所有值。我无法理解我做错了什么
提前感谢。电子邮件输入字段不包含
名称
属性。因此,浏览器无法将数据正确发送到服务器
您需要使用表单帮助器来呈现此输入,或者在
中添加name=“email”
:
<input type="email" name="email" class="form-control" id="exampleInputEmail1" placeholder="@user" readonly>
如果我正在执行name=“email”操作,则在提交表单时,它会加载带有填充值的同一页面,并且数据库中不会保存任何内容,这可能是因为表单未验证。由于您没有在模板的电子邮件字段中显示错误,因此无法看到它们。能否显示ShopController.submit()
controller的代码?我想email
字段值无效。您应该尝试在模板中输出电子邮件
输入周围的错误。如果您使用帮助程序@input…
,则会自动执行此操作,但由于您手动输出字段,因此无法看到它们。在执行此操作时,为NullPointerException@inputText(userForm(“email”)、'\u label->“Owner email”、'id->“createdOn”、'class->“form control”、'readonly->“readonly”、'placeholder->user)