我在用户文档中注意到可以拆分用于不同域类的 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 主体会是什么样子?
最佳答案
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()}
Grails在Controller中增强了HttpServletRequest,所以可以使用
request.XML
这是 XmlSlurper 的 GPathResult 类的实例,它允许解析传入的 XML 请求。
引用:http://grails.org/doc/2.1.0/ref/Servlet%20API/request.html
关于grails - XML 请求中与多个域类的数据绑定(bind),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12775872/