如何使用包含嵌套类的请求XML在grails中将数据绑定到bindData [英] How to bind data with bindData in grails with request XML that contains a nested class

查看:93
本文介绍了如何使用包含嵌套类的请求XML在grails中将数据绑定到bindData的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的XML

 <?xml version =1.0encoding =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 s作为输出,所以它没有奏效。然而,当我使用下面的代码时:

  def grailsWebDataBinder 
def save(){
MyModel model = new MyModel()
grailsWebDataBinder.bind模型,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)

> MyModel.java:

  @XmlRootElement 
@Entity
@Table(name =MY_MODEL
,schema =MY_SCHEMA

public class MyModel实现java.io.Serializable {

private MyModelId id;
private BigDecimal otherStuff;
$ b $ 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 。其他的东西;
}

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

}

MyModelId.java:

  @XmlRootElement 
@Embeddable
公共类MyModelId实现java.io.Serializable {

私人BigDecimal idPart1;
私人字符串idPart2;
$ b $ public MyModelId(){
}

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

$ b @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; $()this.getIdPart1()== castOther.getIdPart1())||(this.getIdPart1()!= null&&& castOther.getIdPart1()!= null&& amp ; 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());
返回结果;
}

}


解决方案

以下工作

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


I have xml like this

<?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>

Now, I would like to use bind data like this:

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)

but I get all nulls as output, so it hasn't worked. However, when I use the following code

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)

I get 233 as output then a null pointer exception for the id attribute.

What am I doing wrong?

(I'm using grails 2.3.7)

UPDATE

Here are the domain classes (with composite 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;
   }   

}

解决方案

The following works

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

这篇关于如何使用包含嵌套类的请求XML在grails中将数据绑定到bindData的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆