2
我試圖在表單中的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=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);
}
ShopController.java
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());
}
}
}
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>
}
}
當我提出這種形式保存在每個值數據庫除了電子郵件的價值field.I無法理解我在做什麼錯誤 在此先感謝。
如果我在做什麼名字=「電子郵件」,然後點擊提交表單加載相同頁面填充值並且沒有任何內容保存在數據庫中 – akku
這可能是因爲表單未驗證。由於您不會在模板的電子郵件字段中顯示錯誤,因此您無法看到它們。你可以顯示'ShopController.submit()'控制器的代碼嗎? – mguillermin
更新了我上面的'ShopController.java'。 – akku