如何在grails中正确保存双重类型?

时间:2013-01-24 00:44:33

标签: grails groovy double double-precision

基本上保存的域不尊重浮点,因为它使其成为另一种格式

我有下一个域名

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

请求位置save()

params到达如params.latitude = 29.089177和params.longitude = -110.961227 保存后,值将存储在数据库中的locationInstance.latitude = 29089177和locationInstance.longitude = -110961227中。

要在视图中显示,请显示为纬度= 2.9089177E7和经度= -1.10961227E8

会出现什么问题,我有另一个带有主题类的项目并且运行正常。 并尝试将属性的名称和类型更改为float并且未解析

谢谢!

1 个答案:

答案 0 :(得分:0)

只需在conf / spring / resources.groovy

中添加
beans = {
    localeResolver(org.springframework.web.servlet.i18n.SessionLocaleResolver) {
        defaultLocale = new Locale("es","MX")
        java.util.Locale.setDefault(defaultLocale)
    }
} 

保持坐标格式,保存时不会更改值。