基本的に、保存するドメインは浮動小数点を尊重せず、別の形式になります
私は次のドメインを持っています
class Location {
double latitude
double longitude
static belongsTo=[ product: Product ]
static constraints = {
latitude nullable:false, blank:false
longitude nullable:false, blank:false
}
}
見る:
<g:form class="form-horizontal" action="create" id="${locationInstance?.product?.id}" >
<fieldset>
<f:with bean="locationInstance">
<f:field property="lat"/>
<f:field property="lon"/>
</f:with>
</fieldset>
</g:form>
<button type="button" class="submit btn btn-primary">
<i class="icon-plus"></i>
<g:message code="default.add.label" args="[message(code: 'location.label', default:'Location')]" default="Add Map"/>
</button>
コントローラ:
import grails.converters.JSON
class LocationController {
def create() {
def model = [productInstance:Product.findByIdAndUser(params.remove("id"),negoexService.currentUser)]
def responseReturn = [success:false]
def redirectParams = [controller:"product", action:"list"]
if (!model.productInstance) {
responseReturn.message = message(code: "default.not.found.message", args: [message(code: "product.label", default: "Product"), params.id])
}else{
redirectParams.action = "edit"
redirectParams.id = model.productInstance.id
params.product = model.productInstance
params.remove("action")
params.remove("controller")
model.locationInstance = new Location(params)
println params // here are showed 29.089177 and -110.961227
println model.locationInstance.lat // out is 2.9089177E7
println model.locationInstance.lon // out is -1.10961227E8
if(request.method=="POST"){
if (model.locationInstance.save(flush: true)) {
responseReturn.success = true
responseReturn.message = message(code: "default.created.message", args: [message(code: "location.label", default: "Location"), model.locationInstance])
}else{
responseReturn.errors = model.locationInstance.errors.allErrors
}
}
}
withFormat {
html {
if(request.xhr && !model.productInstance){
render view:"/messageAjax", model:responseReturn
}else{
if(responseReturn.message){flash.message = responseReturn.message}
if(!model.productInstance || responseReturn.success){
redirect redirectParams
}else{
render view:"create"+(request.xhr?"Ajax":""), model:model
}
}
}
json {render responseReturn as JSON}
js {render responseReturn as JSON}
xml {render responseReturn as XML}
}
}
Location save()をリクエストするには
保存後、パラメータはparams.latitude=29.089177およびparams.longitude=-110.961227のように到着し、値はlocationInstance.latitude=29089177およびlocationInstance.longitude=-110961227のようにデータベースに保存されます。
ビューに表示されるのは、緯度=2.9089177E7および経度=-1.10961227E8のように表示されます。
何が問題になるでしょう、私はテーマクラスを持つ別のプロジェクトを持っていて、正しく実行されます。プロパティの名前とタイプをfloatに変更して、解決しないようにしてください
ありがとう!