gpt4 book ai didi

grails - Grails-无法在空对象上获取属性 'id'

转载 作者:行者123 更新时间:2023-12-02 15:49:31 25 4
gpt4 key购买 nike

这是我有问题。

我有2个域类,我想将一些参数从一个传递给第二个域类,然后保存。

这是头等舱车

package carrentco

class Car {

String brand
String model
String fuelType
BigDecimal pricePerDay

static constraints = {
brand(inList:["AUDI", "BMW", "MERCEDES", "NISSAN", "HONDA", "FORD"])
model()
fuelType(inList:["FUEL", "DIESEL", "AUTOGAS"])
pricePerDay(min:0.0, max:1000.0)

}
}

这是第二个注册
package carrentco

class Registration {

String firstName
String lastName
Date dateOfBirth
String phoneNumber
String email
Date startOfRentDate
Date endOfRentDate
Car rentedCar

static constraints = {
firstName(nullable: false)
lastName(nullable: false)
dateOfBirth()
phoneNumber(phoneNumber: true, nullable: false)
email(blank:false, email:true)
startOfRentDate()
endOfRentDate()
}
}

我想做什么我创建了一些汽车列表,并在cars / index.gsp中添加了每辆汽车的某些按钮(RENT CAR),当用户单击此按钮时,他将去注册/create.gsp,在此用户添加一些自己的详细信息,例如姓氏等。想要从这里自动从car / index.gsp中选择的汽车ID并保存此表格。当管理员查看该注册记录时,他会看到要租用哪个汽车的用户。

这是我的车/ index.gsp
 <g:each in="${carList}" var="car">
<p>${car.id} </p>
<g:link action="create" controller="registration" params="${[carId : car.id ]}">
Rent Car
</g:link>

</g:each>

这是我的注册/ create.gsp
<g:form action="save">
<div><p>Car ID : ${car.id} </p></div>
<fieldset class="form">
<f:all bean="registration"/>
</fieldset>
<fieldset class="buttons">
<input type="hidden" name="carId" id="carId" value="${car?.id}"/>
<input type="text" name="yourName" required>
<g:submitButton name="create" class="save" value="${message(code: 'default.button.create.label', default: 'Create')}" />
</fieldset>
</g:form>

当我编写此代码时,出现此错误,
URI
/registration/create
Class
java.lang.NullPointerException
Message
Request processing failed; nested exception is org.grails.gsp.GroovyPagesException: Error processing GroovyPageView: [views/registration/create.gsp:38] Error executing tag <g:form>: Error evaluating expression [car.id] on line [29]: Cannot get property 'id' on null object
Caused by
Cannot get property 'id' on null object

当我在registration / create.gsp中尝试这个
 <g:form action="save">
<fieldset class="form">
<f:all bean="registration"/>
</fieldset>
<fieldset class="buttons">
<g:submitButton name="create" class="save" value="${message(code: 'default.button.create.label', default: 'Create')}" />
</fieldset>
</g:form>

一切都很好。我可以保存带有汽车ID的注册表,但是在这里我需要从列表中选择哪个汽车ID。所以我不想要这个,我希望它是自动选择的,因为如果我单击从汽车 list 示例汽车ID 2中租车按钮,为什么我需要再次在注册表中选择汽车ID,这不好。

这是我的注册管理员。
package carrentco

import static org.springframework.http.HttpStatus.*
import grails.transaction.Transactional

@Transactional(readOnly = true)
class RegistrationController {

static allowedMethods = [save: "POST", update: "PUT", delete: "DELETE"]

def index(Integer max) {
params.max = Math.min(max ?: 10, 100)
respond Registration.list(params), model:[registrationCount: Registration.count()]
}

def show(Registration registration) {
respond registration
}

def create() {
respond new Registration(params)
def selectedCar = Car.get(params.carId)
[car: selectedCar]
}

@Transactional
def save(Registration registration) {

if (registration == null) {
transactionStatus.setRollbackOnly()
notFound()
return

}

if (registration.hasErrors()) {
transactionStatus.setRollbackOnly()
respond registration.errors, view:'create'
return
}

registration.save flush:true

request.withFormat {
form multipartForm {
flash.message = message(code: 'default.created.message', args: [message(code: 'registration.label', default: 'Registration'), registration.id])
redirect registration
}
'*' { respond registration, [status: CREATED] }
}
}

def edit(Registration registration) {
respond registration
}

@Transactional
def update(Registration registration) {
if (registration == null) {
transactionStatus.setRollbackOnly()
notFound()
return
}

if (registration.hasErrors()) {
transactionStatus.setRollbackOnly()
respond registration.errors, view:'edit'
return
}

registration.save flush:true

request.withFormat {
form multipartForm {
flash.message = message(code: 'default.updated.message', args: [message(code: 'registration.label', default: 'Registration'), registration.id])
redirect registration
}
'*'{ respond registration, [status: OK] }
}
}

@Transactional
def delete(Registration registration) {

if (registration == null) {
transactionStatus.setRollbackOnly()
notFound()
return
}

registration.delete flush:true

request.withFormat {
form multipartForm {
flash.message = message(code: 'default.deleted.message', args: [message(code: 'registration.label', default: 'Registration'), registration.id])
redirect action:"index", method:"GET"
}
'*'{ render status: NO_CONTENT }
}
}

protected void notFound() {
request.withFormat {
form multipartForm {
flash.message = message(code: 'default.not.found.message', args: [message(code: 'registration.label', default: 'Registration'), params.id])
redirect action: "index", method: "GET"
}
'*'{ render status: NOT_FOUND }
}
}
}

这是我的大学项目,所以我需要帮助,因此我无法理解解决该问题需要做什么。

最佳答案

当触发“创建” Action 时,显然会出现问题。异常消息“car”的判断未传递到 View 。从操作中删除“响应”语句。

试试这个:

def create() { 
[car: Car.get(params.carId)]
}

关于grails - Grails-无法在空对象上获取属性 'id',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41790262/

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