我尝试渲染一个 View ,该 View 工作正常,但似乎无法获得传递给它的模型对象。我不知道原因,因为根据所有手册和示例,这应该非常简单。
模型对象
class Race {
def distance = "1/4 mile"
def racer1
def racer2
}
RaceController
在这里渲染def doFullRace(Race race) {
render (view: 'raceProgress', model: [race: race])
}
和
raceProgress.gsp
应该足够容易地显示它 <html>
<body>
<div id="raceStart" align="center">
...
<p>${race.racer1} is racing ${race.distance} against ${race.racer2}</p>
</div>
</body>
</html>
但我反而得到了
关于我错过的基本知识有什么想法吗?
最佳答案
您具有以下内容:
def doFullRace(Race race) {
render (view: 'raceProgress', model: [race: race])
}
如果满足以下所有条件,则
race
成为null
的方法之一:Race
是域类doFullRace
的请求包括一个名为id
的请求参数id
匹配的params.id
记录从http://docs.grails.org/3.3.9/guide/theWebLayer.html#commandObjects ...
If the command object’s type is that of a domain class and there is an id request parameter then instead of invoking the domain class constructor to create a new instance a call will be made to the static get method on the domain class and the value of the id parameter will be passed as an argument.
和...
If the command object’s type is a domain class and there is no id request parameter or there is an id request parameter and its value is empty then null will be passed into the controller action unless the HTTP request method is "POST", in which case a new instance of the domain class will be created by invoking the domain class constructor. For all of the cases where the domain class instance is non-null, data binding is only performed if the HTTP request method is "POST", "PUT" or "PATCH".
关于grails - 呈现 View ,但gsp中没有对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54653467/