在 grails 中使用 HTML 构建器而不是 GSP

发布于 2024-08-13 13:03:31 字数 907 浏览 8 评论 0原文

有没有办法使用 groovy builders 在 Grails 应用程序中构建 JSP 文件,保持足够的集成?

为了更好地解释:默认情况下,Grails 使用 gsp 文件,这些文件很好,但相当冗长。

<div class="clear">
  <ul id="nav">  
    <li><g:link controller="snippets" action="list">Snippets</g:link></li>
    <li><g:link controller="users" action="list">Users</g:link></li>
    <li><g:link controller="problems" action="list">Problems</g:link></li>
    <li><g:link controller="messages" action="list">Messages</g:link></li>
  </div>
<div id="content">

有没有办法使用 groovy.xml.MarkupBuilder ,将前面的部分变成

div(class:'clear') {
  ul(id:'nav') {
    li { g_link(controller:'snippets', action:'list', 'Snippets') }
// and so on

当然,g_link 的发明只是为了给出这个想法。

is there a way to use groovy builders to build JSP files in a Grails application keeping things enough integrated?

To explain better: by default Grails uses gsp files that are nice but quite verbose..

<div class="clear">
  <ul id="nav">  
    <li><g:link controller="snippets" action="list">Snippets</g:link></li>
    <li><g:link controller="users" action="list">Users</g:link></li>
    <li><g:link controller="problems" action="list">Problems</g:link></li>
    <li><g:link controller="messages" action="list">Messages</g:link></li>
  </div>
<div id="content">

is there a way to use groovy.xml.MarkupBuilder tha would turn the previous piece into

div(class:'clear') {
  ul(id:'nav') {
    li { g_link(controller:'snippets', action:'list', 'Snippets') }
// and so on

Of course g_link is invented just to give the idea..

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

看透却不说透 2024-08-20 13:03:31

在 grails 用户的 Web 层部分 下搜索构建器指导。其中有一个示例向您展示了如何使用 xml 构建器来完成此操作。

Do a search for builder under the web layer section of the grails user guide. There is an example in there that shows you exactly how to do this using the xml builder.

深居我梦 2024-08-20 13:03:31

我没有给你一个完整的答案,但我怀疑关键是获得“视图解析器”的访问权限。在普通的 SpringMVC 应用程序中,这些在 views.properties (或 views.xml)中配置如下:

csv=com.example.MyCSVResolver
xml=com.example.MyXMLResolver
audio=com.example.MySpeechResolver

在常规的 SpringMVC 应用程序中,您返回类似 new 的内容来自控制器操作的 ModelAndView(myModel, 'csv')

这将导致调用 CSVResolver 类,并向其传递 myModel 中的数据。除了包含要呈现的数据之外,myModel 可能还包含一些格式选项(例如列宽)。

Spring 在视图文件中搜索与视图名称匹配的键。如果没有找到匹配项,默认情况下它只会呈现带有视图名称的 JSP 并向其传递模型数据。

现在回到 Grails...请记住,Grails 实际上只是 SpringMVC 上的一个 Groovy API,并且 SpringMVC 的大多数功能都可以从 Grails 访问。因此,如果您知道如何修改视图文件,只需更改控制器操作以返回适当的 ModelAndView 实例,它就应该按上述方式工作。

I don't have a complete answer for you, but I suspect the key will be gaining access to the "view resolvers". In a normal SpringMVC app, these are configured in views.properties (or views.xml) as follows:

csv=com.example.MyCSVResolver
xml=com.example.MyXMLResolver
audio=com.example.MySpeechResolver

In a regular SpringMVC app, you return something like new ModelAndView(myModel, 'csv') from a controller action.

This would cause the CSVResolver class to be invoked passing it the data in myModel. In addition to containing the data to be rendered, myModel would likely also contain some formatting options (e.g. column widths).

Spring searches the views file for a key matching the view name. If it doesn't find a match, by default it just renders a JSP with the view name and passes it the model data.

Now back to Grails....remember that Grails is really just a Groovy API over SpringMVC and most of the features of SpringMVC can be accessed from Grails. So if you can figure out how to modify the views file, just change your controller actions to return an appropriate ModelAndView instance, and it should work as described above.

悲凉≈ 2024-08-20 13:03:31

GSP 允许您在 <% %> 内运行任意 Groovy 代码括号。所以你可以有这样的东西(借用 BlackTiger 链接到的页面的示例):

<%      StringWriter w = new StringWriter()
        def builder = new groovy.xml.MarkupBuilder(w)
        builder.html{
            head{
                title 'Log in'
            }
            body{
                h1 'Hello'
                builder.form{ }   
            } 
        }     
    out << w.toString() 
%>

请注意,上面调用了 g:form 标签,你可以向它传递额外的东西。

所以你所要求的当然是可能的,尽管我不确定它最终是否会取得胜利。我建议您也许更多地关注 TagLibs 与模板和 SiteMesh 布局的结合 - 绝对可以极大地简化事情。

GSP allows you to run arbitrary Groovy code inside <% %> brackets. So you can have something like this (borrowing example from page linked to by BlackTiger):

<%      StringWriter w = new StringWriter()
        def builder = new groovy.xml.MarkupBuilder(w)
        builder.html{
            head{
                title 'Log in'
            }
            body{
                h1 'Hello'
                builder.form{ }   
            } 
        }     
    out << w.toString() 
%>

Note that the above calls g:form tag, and you can pass additional stuff to it.

So what you are asking for is certainly possible, though I am not sure if it will end up being a win. I'd suggest you perhaps look more at TagLibs in combination with Templates and SiteMesh Layouts - can definitely simplify things tremendously.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文