开发者

using groovyxmlslurper from EL in JSP

I've been tasked with creating a JSP tag that will allow developers to pass a URI to an xml document, and have an object returned that can be navigated using EL.

I have been using groovy and grails quite a bit so I thought of try开发者_Python百科ing something like

rval =  new XmlSlurper().parseText(myXml);

and throwing that into the request so that back in the JSP they might do something like:

<mytag var="var"/>
${var.rss[0].title} 

but that approach doesn't work.

Does anyone have any suggestions?


Gizmo is correct that the problem is that JSPs assume everything is Java, but I doubt that switching to GSP is a practical answer. To work around this, you need to know how Groovy code gets translated to Java. The Groovy code:

var.rss[0].title

Is roughly equivalent to this Java:

var.getProperty("rss").getAt(0).getProperty("title")

It may also be necessary to cast each result to a GPathResult, e.g.,

((GPathResult)((GPathResult)var.getProperty("rss")).getAt(0)).getProperty("title")

Java sucks, huh?


It does not work because the JSP is compiled using the java compiler, not the groovy compiler. You should use a GSP instead, otherwise you won't be able to use the groovy mechanism that internally call methods when you use a GPath expression.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜