代码之家  ›  专栏  ›  技术社区  ›  Rob Cowell

为什么我的嵌套POJO从运行中的表单返回null!1.x

  •  3
  • Rob Cowell  · 技术社区  · 7 年前

    在游戏中!framework1.5.1,为什么我的thingy.Owner返回空值?自动绑定不应该解决这个问题吗?

    用户类

        package models;
    
    @Entity
    @Table(name="objtest_user")
    public class User extends Model
    {
        @Required
        public String username;
    
        @Password
        @Required
        public String password;
    
        public String fullname;
    
        public User(String username, String password, String fullname)
        {
            this.username = username;
            this.password = password;
            this.fullname = fullname;
        }
    
        @Override
        public String toString()
        {
            return this.fullname;
        }
    }
    

    还有这个引用用户类的Thingy类

        package models;
    
    import java.util.*;
    import javax.persistence.*;
    
    import play.db.jpa.*;
    import play.data.validation.*;
    
    @Entity
    public class Thingy extends Model
    {
        @Required
        public String Name;
    
        @ManyToOne
        public User Owner;
    
        public Thingy(String name, User owner)
        {
            this.Name = name;
            this.Owner = owner;
        }
    
        @Override
        public String toString()
        {
            return Name;
        }
    }
    

    还有这个模板表单

    #{extends 'main.html' /}
    #{set title:'Home' /}
    
    <p>Current user = ${currentUser}</p>
    
    
    #{form @saveThingy(), id:'saveThingy'}
        <input type="text" id="thingy.Name" name="thingy.Name"/>
        <input type="hidden" id="thingy.Owner" name="thingy.Owner" value="${currentUser}"/>
        <input type="submit" id="Save" value="Save"/>
    #{/form}
    

    控制器方法

    public static void saveThingy(Thingy thingy)
    {
        System.out.println("Name = " + thingy.Name);
        System.out.println("Owner = " + thingy.Owner);
    
        thingy.save();
    }
    
    1 回复  |  直到 7 年前
        1
  •  4
  •   Codemwnci    7 年前

    尝试更改以下行

    <input type="hidden" id="thingy.Owner" name="thingy.Owner" value="${currentUser}"/>
    

    <input type="hidden" id="thingy.Owner" name="thingy.Owner.id" value="${currentUser.id}"/>
    

    如果你查一下文件( https://www.playframework.com/documentation/1.2.x/controllers#params