0

我有一个创建和保存方法如下:

def create () {
  def myColorInstance = new Color()
  return (colorInstance: myColorInstance]
}

def save () {
  Date someDate = params.date("somedate", "MM/dd/yyyy")
  int someInt = params.int("someInt")
  color = colorService.add(params.colorname, someDate, someInt)
  if (color.hasErrors())
    render (view: "create", model: [colorInstance: color])
  else
    redirect (action: "list")
} 

在我的布局页面上,我有以下内容:

 <g:hasErrors>
       <div class="alert alert-error">Please try submitting again</div>
 </g:hasErrors>

我得到的行为是,当用户输入某些内容并且验证失败时。当他们第二次提交(再次没有输入任何内容)时,他们看到了消息Please try submitting again并且 url 更改为http://localhost:8080/myapp/color/saveso,然后我的应用程序失败并显示消息:“无法将具有类 'null' 的对象'null' 转换为类 'int'。尝试' java.lang.Integer' 代替"

处理这种情况的最佳方法是什么?我希望用户在顶部看到错误消息,他们应该能够再次更正错误并尝试再次提交,它应该可以工作..

4

1 回答 1

1

您不能将 an 设置int为 null,这params.int("")可能会返回。Integer但是,可以为空。

Integer someInt = params.int("someInt")

这将解决您的异常,但它返回 null 的原因与您的视图没有首先传递数据有关。

仅供参考,这是不正确的语法:

def create () {
  def myColorInstance = new Color()
  return (colorInstance: myColorInstance]
}

应该:

def create () {  
  [colorInstance: new Color()]
}
于 2013-03-01T23:31:22.563 回答