Grails call Controller method from View

爱⌒轻易说出口 提交于 2019-12-13 00:25:54

问题


I have a quick question for grails. I have a view page that iterates over an array of objects to display them. The HTML/Grails tag code looks like this:

<g:each in="${ICFList}" status="i" var="icf">
<tr>
    <td>${icf.printName?.encodeAsHTML()}</td>
    <td>${icf.activeNote?.encodeAsHTML()}</td>
</tr>
</g:each>

This code works and displays what I need. However, I don't want to store a printName variable inside my icf object anymore. the object also contains a one character code to identify it, and I have a method that looks up the printName value based on the code. Basically, my question is this:

Is there a way to do something like this

<td>${icf.getNameFromCode(${icf.code})}</td>

without having to store the name anywhere? The getNameFromCode method is defined in the controller object for this particular view, but I can't figure out how to access it from the gsp. Any help is greatly appreciated.


回答1:


I don't think you can call a controller method from the view.

What I'd suggest is either to move the getNameFromCode to the domain class or implement the logic in a taglib and use a custom tag to display your data. Which option to choose depends on whether that method refers to something that's intrinsic to the model or something that's tied to the view.

The latest Getting started with Grails screencast explains how to create and use custom tags.




回答2:


Answering the question formally, you can use g:include:

<g:include controller="book" action="list" />

though the fields of single entity feel so much more like a Model.




回答3:


It can also be achieved by making AJAX calls to this controller, but I think Gustavo's answer is what you are looking for.



来源:https://stackoverflow.com/questions/6346975/grails-call-controller-method-from-view

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!