填写的值不保存在数据库中

时间:2014-04-07 17:38:09

标签: 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=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>
        }
        }

当我提交此表单时,除了电子邮件字段的值外,它会保存数据库中的每个值。我无法理解我做错了什么 提前谢谢。

1 个答案:

答案 0 :(得分:2)

email输入字段不包含name属性。因此,您的浏览器无法正确地将数据发送到服务器。

您需要使用表单助手来呈现此输入,或在name="email"中添加<input>

    <input type="email" name="email" class="form-control" id="exampleInputEmail1"  placeholder="@user" readonly>