作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试使用此逻辑创建一些应用程序;
我有汽车条目,我想在我的汽车列表中的每个条目上添加(租金)按钮,何时单击以单击某人,按钮将打开一些表格以用于输入联系信息,该表格应自动获得汽车编号
这是我的汽车 Realm 类(class)。我不明白如何将按钮添加到index.gsp中,其中列出了有关汽车的条目,以及如何从列表中获取选定汽车的ID号并将其发送到表单
这该怎么做?这个逻辑如何工作?
package rentme
class Car {
String brand
String model
String fuelType
BigDecimal pricePerDay
String busy
static constraints = {
brand(inList:["AUDI", "BMW", "MERCEDES", "NISSAN", "HONDA", "FORD"])
fuelType(inList:["FUEL", "DIESEL", "AUTOGAS"])
pricePerDay(min:0.0, max:1000.0)
busy(inList:["YES", "NO"])
}
}
<g:each in="${carList}" var="car">
<p>${car.id} </p>
<g:link action="registration" controller="registration" params="[carId : ${car.id} ]">
Rent Car
</g:link>
</g:each>
class Registration {
String yourName
static constraints = {
yourName()
}
}
package rentme
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
println params
Car b = Car.get(params.id)
}
def create() {
respond new Registration(params)
}
@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 }
}
}
}
def show(Registration registration) {
respond registration
println params
Car b = Car.get(params.id)
}
<!DOCTYPE html>
<html>
<head>
<meta name="layout" content="main" />
<g:set var="entityName" value="${message(code: 'registration.label', default: 'Registration')}" />
<title><g:message code="default.list.label" args="[entityName]" /></title>
</head>
<body>
<a href="#list-registration" class="skip" tabindex="-1"><g:message code="default.link.skip.label" default="Skip to content…"/></a>
<div class="nav" role="navigation">
<ul>
<li><a class="home" href="${createLink(uri: '/')}"><g:message code="default.home.label"/></a></li>
<li><g:link class="create" action="create"><g:message code="default.new.label" args="[entityName]" /></g:link></li>
</ul>
</div>
<div id="list-registration" class="content scaffold-list" role="main">
<h1><g:message code="default.list.label" args="[entityName]" /></h1>
<g:if test="${flash.message}">
<div class="message" role="status">${flash.message}</div>
</g:if>
<f:table collection="${registrationList}" />
<div class="pagination">
<g:paginate total="${registrationCount ?: 0}" />
</div>
<div><p>${car.id} </p></div>
</div>
</body>
</html>
<div><p>${car.id} </p></div>
Error 500: Internal Server Error
URI
/car/index
Class
org.grails.taglib.GrailsTagException
Message
Request processing failed; nested exception is java.lang.RuntimeException: Error initializing GroovyPageView
Caused by
[views/car/index.gsp:32] Attribute value quote wasn't closed (action="registration" controller="registration" params="[carId : ${car.id} ]").
I don't know this method is good or not but i get this error when i open on browser my registration index page
Line | Method
->> 223 | createGroovyPageView in org.grails.web.servlet.view.GroovyPageViewResolver
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
| 200 | createGrailsView in ''
| 99 | loadView . . . . . . in ''
| 36 | loadView in grails.plugin.scaffolding.ScaffoldingViewResolver
| 244 | createView . . . . . in org.springframework.web.servlet.view.AbstractCachingViewResolver
| 472 | createView in org.springframework.web.servlet.view.UrlBasedViewResolver
| 146 | resolveViewName . . . in org.springframework.web.servlet.view.AbstractCachingViewResolver
| 88 | resolveViewName in org.grails.web.servlet.view.GroovyPageViewResolver
| 57 | resolveViewName . . . in org.grails.web.servlet.view.GrailsLayoutViewResolver
| 1296 | resolveViewName in org.springframework.web.servlet.DispatcherServlet
| 1234 | render . . . . . . . in ''
| 1037 | processDispatchResult in ''
| 980 | doDispatch . . . . . in ''
| 897 | doService in ''
| 970 | processRequest . . . in org.springframework.web.servlet.FrameworkServlet
| 861 | doGet in ''
| 622 | service . . . . . . . in javax.servlet.http.HttpServlet
| 846 | service in org.springframework.web.servlet.FrameworkServlet
| 729 | service . . . . . . . in javax.servlet.http.HttpServlet
| 230 | internalDoFilter in org.apache.catalina.core.ApplicationFilterChain
| 165 | doFilter . . . . . . in ''
| 52 | doFilter in org.apache.tomcat.websocket.server.WsFilter
| 192 | internalDoFilter . . in org.apache.catalina.core.ApplicationFilterChain
| 165 | doFilter in ''
| 55 | doFilterInternal . . in org.springframework.boot.web.filter.ApplicationContextHeaderFilter
| 107 | doFilter in org.springframework.web.filter.OncePerRequestFilter
| 192 | internalDoFilter . . in org.apache.catalina.core.ApplicationFilterChain
| 165 | doFilter in ''
| 105 | doFilterInternal . . in org.springframework.boot.actuate.trace.WebRequestTraceFilter
| 107 | doFilter in org.springframework.web.filter.OncePerRequestFilter
| 192 | internalDoFilter . . in org.apache.catalina.core.ApplicationFilterChain
| 165 | doFilter in ''
| 77 | doFilterInternal . . in org.grails.web.servlet.mvc.GrailsWebRequestFilter
| 107 | doFilter in org.springframework.web.filter.OncePerRequestFilter
| 192 | internalDoFilter . . in org.apache.catalina.core.ApplicationFilterChain
| 165 | doFilter in ''
| 67 | doFilterInternal . . in org.grails.web.filters.HiddenHttpMethodFilter
| 107 | doFilter in org.springframework.web.filter.OncePerRequestFilter
| 192 | internalDoFilter . . in org.apache.catalina.core.ApplicationFilterChain
| 165 | doFilter in ''
| 197 | doFilterInternal . . in org.springframework.web.filter.CharacterEncodingFilter
| 107 | doFilter in org.springframework.web.filter.OncePerRequestFilter
| 192 | internalDoFilter . . in org.apache.catalina.core.ApplicationFilterChain
| 165 | doFilter in ''
| 96 | doFilterInternal . . in org.springframework.web.filter.CorsFilter
| 107 | doFilter in org.springframework.web.filter.OncePerRequestFilter
| 192 | internalDoFilter . . in org.apache.catalina.core.ApplicationFilterChain
| 165 | doFilter in ''
| 107 | doFilterInternal . . in org.springframework.boot.actuate.autoconfigure.MetricsFilter
| 192 | internalDoFilter in org.apache.catalina.core.ApplicationFilterChain
| 165 | doFilter . . . . . . in ''
| 198 | invoke in org.apache.catalina.core.StandardWrapperValve
| 108 | invoke . . . . . . . in org.apache.catalina.core.StandardContextValve
| 472 | invoke in org.apache.catalina.authenticator.AuthenticatorBase
| 140 | invoke . . . . . . . in org.apache.catalina.core.StandardHostValve
| 79 | invoke in org.apache.catalina.valves.ErrorReportValve
| 87 | invoke . . . . . . . in org.apache.catalina.core.StandardEngineValve
| 349 | service in org.apache.catalina.connector.CoyoteAdapter
| 784 | service . . . . . . . in org.apache.coyote.http11.Http11Processor
| 66 | process in org.apache.coyote.AbstractProcessorLight
| 802 | process . . . . . . . in org.apache.coyote.AbstractProtocol$ConnectionHandler
| 1410 | doRun in org.apache.tomcat.util.net.NioEndpoint$SocketProcessor
| 49 | run . . . . . . . . . in org.apache.tomcat.util.net.SocketProcessorBase
| 1142 | runWorker in java.util.concurrent.ThreadPoolExecutor
| 617 | run . . . . . . . . . in java.util.concurrent.ThreadPoolExecutor$Worker
| 61 | run in org.apache.tomcat.util.threads.TaskThread$WrappingRunnable
^ 745 | run . . . . . . . . . in java.lang.Thread
Caused by GrailsTagException: [views/car/index.gsp:32] Attribute value quote wasn't closed (action="registration" controller="registration" params="[carId : ${car.id} ]").
->> 32 | populateMapWithAttributes in views/car/index.gsp
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
最佳答案
默认情况下,将ID分配给域类。因此,在index.gsp中,您可以简单地遍历汽车列表并为每辆汽车创建一个“租用”按钮。您可以像下面这样使用g:each。
<g:each var="car" in="${cars}"> // cars is a list of cars
<p>Brand: ${car.brand}</p>
<p>Model: ${car.model}</p>
<p><button name="rentCar${car.id}" onclick="rentCar(this, ${car.id})">Rent Car</button</p>
</g:each>
<g:each var="car" in="${cars}"> // cars is a list of cars
<p>Brand: ${car.brand}</p>
<p>Model: ${car.model}</p>
<p><g:link action="rent" controller="order" params="[car: ${car}, carId : ${car.id} ]">
Rent Car
</g:link></p>
</g:each>
def index(Integer max) {
params.max = Math.min(max ?: 10, 100)
Car car;
if(params.carId){
car = Car.get(params.carId) // fetch the car using the id passed in params
}
respond Registration.list(params), model:[registrationCount: Registration.count(), car: car]
}
def show(Registration registration) {
respond registration
// println params
// Car b = Car.get(params.id)
}
<g:link action="index" controller="registration" params="[carId : ${car.id} ]">
Rent Car
</g:link>
<div><p>Car ID : ${car.id} </p></div>
关于grails - 如何在grails中创建动态按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41685059/
我是一名优秀的程序员,十分优秀!