在XML请求中与多个域类进行数据绑定

时间:2012-10-08 05:38:40

标签: grails gorm grails-domain-class

我在用户文档中注意到可以分割用于不同域类的URL参数:

/book/save?book.title=The%20Stand&author.name=Stephen%20King

然后您可以传递其预期域类的值:

def b = new Book(params.book)
def a = new Auther(params.author)

我打算用Grails构建一个Web服务API,并且想知道如果我在请求体中使用XML请求而不是通过URL参数传递参数,这将如何工作。在这种情况下,XML主体会是什么样子?

1 个答案:

答案 0 :(得分:0)

def s = '<xml><book title="The Stand" /><author name="Stephen King"/></xml>'
def x = new XmlSlurper().parseText(s)

x.book.each{b-> new Book(b.attributes()).save()}
x.author.each{a-> new Author(a.attributes()).save()}

在Controller Grails中增强了HttpServletRequest,因此您可以使用

request.XML 

是XmlSlurper的GPathResult类的实例,它允许解析传入的XML请求。

参考:http://grails.org/doc/2.1.0/ref/Servlet%20API/request.html