【问题标题】:Grails select not returning the object but a stringGrails选择不返回对象而是返回字符串
【发布时间】:2012-11-09 00:28:16
【问题描述】:

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

【问题讨论】:

    标签: grails


    【解决方案1】:

    这个:

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

    应该是:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-07-18
      • 2011-07-29
      • 2012-11-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多