java - 表单中填写的值未保存在数据库中

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

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

最佳答案

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

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

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

关于java - 表单中填写的值未保存在数据库中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22919349/

相关文章:

java - 如何显示具有两个父节点的xml

java - Spring无法创建可执行jar

java - 在 Play Framework 中创建自定义的 404/500 错误页面

java - 防止在反向路由中转义斜线

java - play.libs.WS.WSRequestHolder.get() 切断部分请求?

java - 为什么在 Java 中初始化数组对象时不需要提供括号?

java - 将属性文件放在 IBM websphere 8.5 中的最佳位置?

php - 使用通用函数查询数据库是不好的做法吗?

java - 如何在查看寻呼机中的图像顶部添加文本?

mysql - mysql 查询数据成倍增加