Grails选择不返回对象而是一个字符串 [英] Grails select not returning the object but a string

查看:105
本文介绍了Grails选择不返回对象而是一个字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何获得一个select标签来返回对象本身而不是一个字符串?
每当我去创建一个新的BusinessArea时,我会收到一个错误消息:

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



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



感谢您的帮助



我有以下模型

  class BusinessArea {

BusinessType businessType
//此次创建的日期和时间
日期dateCreated
//上次维护此事件的日期和时间。
lastUpdated日期
//上次维护此操作的OPERATOR或PROGRAM的标识符。
int lastMaintainer
//商业区域的名称
字符串名称
$ b $静态约束= {
lastMaintainer空白:false,可空:false
名称空白:false,可为空:false
}
}

和控制器操作

$ $ p $ code $ 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行动= createBusinessArea > 
< div class =row>
<传说>商业区< /图例>
< div class =span3>
< label>商业领域:< / label>
< / div>
< div class =span3>
< label>商业类型:< / label>
< / div>
< / div>

< div class =buttons>
< div class =row>
< div class =span7style =padding-top:2em>
< / div>
< / div>
< / div>
< / g:表格>


解决方案

b $ b

 < g:select name =businessTypefrom =$ {businessTypes}optionValue =name/> 

应该是:

 < g:select name =businessType.idfrom =$ {businessTypes}optionValue =nameoptionKey =id/> 


How can I get a select tag to return the object itself and not a string? Whenever I go to create a new BusinessArea I get an error that says:

"Failed to convert property value of type java.lang.String to required type bassscheduler.BusinessType for property businessType; nested exception is java.lang.IllegalStateException: Cannot convert value of type [java.lang.String] to required type [bassscheduler.BusinessType] for property businessType: no matching editors or conversion strategy found"

Am I doing something wrong? I cant seem to find anything on how to get the select tag to return the object itself and not a string

Thanks for the help

I have the following model

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
  }
}

and 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"]
}

With this form submitting to the controller action

<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>

解决方案

This:

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

should be:

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

这篇关于Grails选择不返回对象而是一个字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆