gpt4 book ai didi

grails - 无法在Grails/Groovy Web应用程序中执行Update()

转载 作者:行者123 更新时间:2023-12-02 15:20:17 24 4
gpt4 key购买 nike

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

我有这个 Controller
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)]
}

最佳答案

当您键入/Test/customer/edit/2时,将调用edit中的操作。 params对象中没有名称thl和dt1,因为您没有在url中提供它们。只有id为 2
现在,您尝试使用以下参数调用CustomerController:customerService.updateActioncustomerService.updateAction(2,null,null,null)中,您可以像这样转换thl:updateAction,但是tgl为null,因此不能转换为long,因为long是原始类型,不能为null。您可以将其强制转换为Long(这是一个对象,objects可以为null),但是在这种情况下,我认为这不是一个好的解决方案。
首先,您应该从updateAction中删除转换逻辑。如果您需要长字符串而不是字符串-将其转换为 Controller ,然后传递给该方法。
第二个观察:您是否真的需要在编辑操作中执行更新?我想您只需要通过id获取您的客户并将其传递给gsp:

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

在这种情况下,您应该创建 ${thl as long}方法

关于grails - 无法在Grails/Groovy Web应用程序中执行Update(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37430360/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com