无法在Grails / Groovy Web应用程序中执行更新()

时间:2016-05-25 07:22:17

标签: grails groovy

我正在尝试使用psql创建CRUD Web服务。我有一个域名时实现了这一点。但现在我不需要域类,我开始重拍这个。我可以创建和删除数据库中的数据,但是当涉及到编辑时,我会收到此错误:

URI
    /Test/customer/edit/2
Class
    org.codehaus.groovy.runtime.typehandling.GroovyCastException
Message
    Cannot cast object 'null' with class 'null' to class 'long'. Try 'java.lang.Long' instead

我有这个控制器

package test

class CustomerController {
    def customerService

    def index = {
        redirect action: "list"
    }
    def create() {}

    def edit () {
    [customer: customerService.updateAction(params.id,params.name,params.thl,params.dt1)]
}
    def list() {
        [customers : customerService.listAction()]
    }

    def save() {
        println params
        [customer: customerService.insertAction(params.id,params.name,params.thl,params.dt1)]
        redirect action: "list"
    }
    def update(){
    [customer: customerService.updateAction(params.id,params.name,params.thl,params.dt1)]
    redirect action: "list"
}

    def delete(){
        [customer: customerService.deleteAction(params.id)]
        redirect action: "list"
    }
}

网络服务

package test

import groovy.sql.Sql
import grails.transaction.Transactional

@Transactional
class CustomerService {

    def DataSource

    def listAction(){
        def sql = new Sql(dataSource)
        return sql.rows ("SELECT * FROM  mn")
    }

    def insertAction(String id, String name,String thl,String dt1){
        def sql = new Sql(dataSource)
        sql.execute("INSERT INTO  mn (id, name, thl, dt1) VALUES (${id as long},$name,${thl as long},$dt1)")
    }

    ddef updateAction (String id,String name,String thl,String dt1){
    def sql = new Sql(dataSource)
    sql.executeUpdate("UPDATE mn SET  id=${id as long}, name=$name, thl=${thl as long}, dt1=$dt1 where id=${id as long}")
}
    def deleteAction(String id){
        def sql= new Sql(dataSource)
        sql.execute("delete from mn where id=${id as long}")
    }
}

和这个gsp

 <body>
<g:render template="/customer/Header"/>
<g:render template="/customer/Navbar"/>
<h1>Edit contact</h1>
<g:form controller="customer" action="update" method="post">
    <div class="container">
    <div class="row">
        <div class="col-lg 2 col-md-2 col-sm-2 col-xs-2">
            <div class="form-group">
                <label for="id">Id</label>
                <input type="text" class="form-control" name="id" id="id" placeholder="Id">
            </div>
        </div>
        <div class="col-lg 2 col-md-2 col-sm-2 col-xs-2">
            <div class="form-group">
                <label for="name">Name</label>
                <input type="text" class="form-control" name="name" id="name" placeholder="Name">
            </div>
        </div>
        <div class="col-lg 2 col-md-2 col-sm-2 col-xs-2">
            <div class="form-group">
                <label for="thl">thlefvno</label>
                <input type="text" class="form-control" id="thl" name="thl" placeholder="thlefvno">
            </div>
        </div>
    </div>
        <div class="row">
            <div class='col-lg 6 col-md-6 col-sm-6 col-xs-6'>
                <div class="form-group">
                    <label class="control-label col-sm-2" for ="dt1">Date :</label>
                    <div class='input-group date' id="dt1">
                        <input id="dt1" name="dt1" type='text' class="form-control">
                        <span class="input-group-addon">
                            <span class="glyphicon glyphicon-calendar"></span>
                        </span>
                    </div>
                </div>
            </div>
            <script type="text/javascript">
                $('#dt1').datetimepicker({
                });
            </script>
        </div>

        <div class="row">
            <div class='col-lg 6 col-md-6 col-sm-6 col-xs-6'>
                <g:actionSubmit value="update" class="btn btn-info" role="button" />
            </div>
        </div>
    </div>

</g:form>
</body>
</html>

解决方案:

服务

def getCustomerById (Long id) {
        def sql = new Sql(dataSource)
        sql.execute("update mn set  id=$id WHERE id=${id as long}")
    }

控制器

def edit () {
        [customer: customerService.getCustomerById(params.id as long)]
}

1 个答案:

答案 0 :(得分:1)

当您从edit键入CustomerController customerService.updateAction时,系统会调用此操作。 params 对象中没有名称,thl dt1 ,因为您没有在网址中提供它们。只有 id 2 现在,您尝试使用以下参数调用customerService.updateAction(2,null,null,null)updateAction${thl as long}中您将 thl 转换为:def edit () { [customer: customerService.getCustomerById(id as String)] } ,但 tgl null 且无法转换为 long ,因为 long 是原始类型,不能为null。您可以将其强制转换为 Long (这是一个对象,对象可以为null)但在这种情况下我不认为它是好的解决方案。
首先,您应该从updateAction中删除转换逻辑。如果你需要long而不是String - 将它转换为控制器然后传递给方法 第二个观察:你真的需要在编辑动作中执行更新吗?我想你只需要通过id获取你的客户并将其传递给gsp:

getCustomerById

在这种情况下,您应该创建if ( _access( trigger->c_str(), 0 ) != -1 ) { ... } 方法