【发布时间】: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 获取记录会更好