2

如何获取选择标记以返回对象本身而不是字符串?每当我去创建一个新的 BusinessArea 时,我都会收到一条错误消息:

“无法将 java.lang.String 类型的属性值转换为属性 businessType 的所需类型 bassscheduler.BusinessType;嵌套异常是 java.lang.IllegalStateException:无法将类型 [java.lang.String] 的值转换为所需类型 [bassscheduler。 BusinessType] 属性 businessType: 找不到匹配的编辑器或转换策略"

难道我做错了什么?我似乎找不到任何关于如何让选择标签返回对象本身而不是字符串的信息

谢谢您的帮助

我有以下型号

class BusinessArea {

  BusinessType businessType
  // The date and time this occurence was created
  Date dateCreated
  // The date and time that this occurrence was last maintained.
  Date lastUpdated
  // The identifier for the OPERATOR or PROGRAM that last maintained this occurrence.
  int lastMaintainer
  // The name of the business area
  String name

  static constraints = {
    lastMaintainer blank: false, nullable: false
    name blank: false, nullable: false
  }
}

和控制器动作

def createBusinessArea() {

  def businessArea = new BusinessArea(params)
  if (!businessArea.save(flush: true)) {
    render view: "index", model : [businessArea: businessArea, activeTemplate: "businessArea"]
    return 
  }
  redirect controller: "admin", action: "index", model : [activeTemplate: "businessArea"]
}

使用此表单提交给控制器操作

<g:form controller="admin" action="createBusinessArea"> 
  <div class="row">
    <legend>Business Area</legend>
<div class="span3">
  <label>Business Area:</label>
  <g:textField name="businessArea"/>
</div>
<div class="span3">
  <label>Business Type:</label>
  <g:select name="businessType" from="${businessTypes}" optionValue="name"/>
</div>
  </div>

  <div class="buttons">
    <div class="row">
      <div class="span7" style="padding-top: 2em">
    <g:submitButton class="btn btn-primary pull-right" name="create" value="Create" />
  </div>
    </div>
  </div>
</g:form>
4

1 回答 1

2

这:

<g:select name="businessType" from="${businessTypes}" optionValue="name"/>

应该:

<g:select name="businessType.id" from="${businessTypes}" optionValue="name" optionKey="id" />
于 2012-11-09T01:24:48.903 回答