如何获取选择标记以返回对象本身而不是字符串?
每当我创建一个新的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
  }
}

和 Controller Action
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"]
}

将此表单提交给 Controller 操作
<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>

最佳答案

这个:

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

应该:
<g:select name="businessType.id" from="${businessTypes}" optionValue="name" optionKey="id" />

关于grails - Grails选择不返回对象而是字符串,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/13300313/

10-13 08:58