ここにいくつか問題があります。Grails - ヌルオブジェクトのID「id」を取得できません
私は2つのドメインクラスを持っています。私は1つのドメインクラスから2つ目のドメインクラスにいくつかのパラメータを渡して、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)
}
}
そして、ここでは私がやるたい何秒1件の登録
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()
}
}
です。私はいくつかの車のリストを作成し、私は車/ index.gspいくつかのボタンを各車(レンタカー)に追加するユーザーがこのボタンをクリックすると彼は登録/ create.gspに行くここのユーザーは名前の姓などのような彼自身の詳細を追加car/index.gspから彼が選択した車のIDを自動的に取得してこのフォームを保存します。管理者がこの登録記録を見ると、どの車のユーザーがレンタルしたいのかがわかります。ここ
は
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
、私はこのコードを書くとき、私はこのエラーを取得する私の車/ 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>
です
登録時に試したとき/ 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>
すべてがokeyです。私は車のIDで登録フォームを保存することができますが、ここで私はどの車のIDからリストから選択する必要があります。だから私はこれを望んでいない私はこれが自動的に選択されたいので、私は車のリストの車のid 2から車のボタンを借りるためにクリックすると、なぜ私はそれが良くない車の登録フォームに再度登録する必要があります。
ここは私の登録コントローラです。
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 }
}
}
}
私の大学のプロジェクトですので、私はこの問題を解決するために何が必要なのか理解できません。