【问题标题】:Can't executeUpdate() in Grails/Groovy web application无法在 Grails/Groovy Web 应用程序中执行更新()
【发布时间】:2016-05-25 07:22:16
【问题描述】:

我正在尝试使用 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)]
}

【问题讨论】:

  • 您的 UpdateAction 方法需要 id 的时间,但您将其作为字符串发送。尝试在传递之前进行转换:customerService.updateAction(params.id as Long,params.name,params.thl,params.dt1)
  • 我更改了更新操作,现在我得到了这个:URI /Test/customer/update Class org.codehaus.groovy.runtime.typehandling.GroovyCastException 消息无法使用类“null”转换对象“null”上课'长'。改用“java.lang.Long”
  • 你不提交id,在你的输入中添加name属性:
  • 虽然 sql.execute("update mn set id=$id WHERE id=${id as long}") 有效,但这不是一个好的解决方案,因为你这样做不必要的更新。如果您研究如何在不更新的情况下通过 id 获取记录会更好

标签: grails groovy


【解决方案1】:

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

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

在这种情况下,您应该创建getCustomerById 方法

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-26
    • 1970-01-01
    • 2013-11-03
    相关资源
    最近更新 更多