如何将数据与grails中的bindData绑定到包含嵌套类的请求XML

时间:2014-04-01 00:43:55

标签: grails

我有像这样的xml

<?xml version="1.0" encoding="UTF-8"?>
<myModel id="mybiz.MyModelIdId@36491253">
  <id>
    <class>mybiz.MyModelId</class>
    <idPart1>1292</idPart1>
    <idPart2>somestring</idPart2>
  </id>
  <otherStuff>233</otherStuff>
</myModel>

现在,我想使用这样的绑定数据:

def save() {
    MyModel model = new MyModel()
    bindData(model,request.XML)
    log.info("other stuff = "+model.otherStuff)
    log.info("idPart1 = "+model.id.idPart1)
    log.info("idPart2 = "+model.id.idPart2)

但我将所有null作为输出,因此它无法正常工作。但是,当我使用以下代码时

def grailsWebDataBinder
def save() {
    MyModel model = new MyModel()
    grailsWebDataBinder.bind model, request.XML
    log.info("other stuff = "+model.otherStuff)
    log.info("idPart1 = "+model.id.idPart1)
    log.info("idPart2 = "+model.id.idPart2)

我得到233作为输出,然后是id属性的空指针异常。

我做错了什么?

(我使用的是grails 2.3.7)

更新

以下是域类(具有复合ID)

MyModel.java:

@XmlRootElement
@Entity
@Table(name="MY_MODEL"
    ,schema="MY_SCHEMA"
)
public class MyModel implements java.io.Serializable {

    private MyModelId id;
    private BigDecimal otherStuff;

    public MyModel() {
    }

    public MyModel(MyModelId id, BigDecimal otherStuff) {
        this.id = id;
        this.otherStuff = otherStuff;
    }

    @EmbeddedId
    @AttributeOverrides( {
        @AttributeOverride(name="idPart1", column=@Column(name="ID_PART1", nullable=false, precision=22, scale=0) ), 
        @AttributeOverride(name="idPart2", column=@Column(name="ID_PART2", nullable=false, length=240) ) } )
    public MyModelId getId() {
        return this.id;
    }

    public void setId(MyModelId id) {
        this.id = id;
    }

    @Column(name="OTHER_STUFF", nullable=false, precision=22, scale=0)
    public BigDecimal getOtherStuff() {
        return this.otherStuff;
    }

    public void setOtherStuff(BigDecimal otherStuff) {
        this.otherStuff = otherStuff;
    }

}

MyModelId.java:

@XmlRootElement
@Embeddable
public class MyModelId implements java.io.Serializable {

    private BigDecimal idPart1;
    private String idPart2;

    public MyModelId() {
    }

    public MyModelId(BigDecimal idPart1, String idPart2) {
       this.idPart1 = idPart1;
       this.idPart2 = idPart2;
    }

    @Column(name="ID_PART1", nullable=false, precision=22, scale=0)
    public BigDecimal getIdPart1() {
        return this.idPart1;
    }

    public void setIdPart1(BigDecimal idPart1) {
        this.idPart1 = idPart1;
    }

    @Column(name="ID_PART2", nullable=false, length=240)
    public String getIdPart2() {
        return this.idPart2;
    }

    public void setIdPart2(String idPart2) {
        this.idPart2 = idPart2;
    }

   public boolean equals(Object other) {
         if ( (this == other ) ) return true;
         if ( (other == null ) ) return false;
         if ( !(other instanceof MyModelId) ) return false;
         MyModelId castOther = ( MyModelId ) other; 

         return ( (this.getIdPart1()==castOther.getIdPart1()) || ( this.getIdPart1()!=null && castOther.getIdPart1()!=null && this.getIdPart1().equals(castOther.getIdPart1()) ) )
 && ( (this.getIdPart2()==castOther.getIdPart2()) || ( this.getIdPart2()!=null && castOther.getIdPart2()!=null && this.getIdPart2().equals(castOther.getIdPart2()) ) );
   }

   public int hashCode() {
         int result = 17;

         result = 37 * result + ( getIdPart1() == null ? 0 : this.getIdPart1().hashCode() );
         result = 37 * result + ( getIdPart2() == null ? 0 : this.getIdPart2().hashCode() );
         return result;
   }   

}

1 个答案:

答案 0 :(得分:0)

以下作品

def id
id = new MyModelId()
bindData(id,request.XML.id)
MyModel model = MyModel.get(id)
bindData(model,request.XML)