gpt4 book ai didi

grails - 如何在grails中创建动态按钮

转载 作者:行者123 更新时间:2023-12-02 15:19:10 27 4
gpt4 key购买 nike

我正在尝试使用此逻辑创建一些应用程序;

我有汽车条目,我想在我的汽车列表中的每个条目上添加(租金)按钮,何时单击以单击某人,按钮将打开一些表格以用于输入联系信息,该表格应自动获得汽车编号

这是我的汽车 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"])
}
}

这是我尝试过的;

这是 View 文件中的Car / index.gsp。
-在这里,我获得了汽车ID,并尝试将按钮添加到每个ID。我想做这个;按钮将转到另一个页面,该页面将是该汽车的注册页面,并且在此注册页面中,用户将看到所选的汽车的ID号。
  <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)的域名类(class)

包rentme
class Registration {

String yourName

static constraints = {
yourName()
}
}

这是我的注册 Controller ,我使用generate-all命令创建了它
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()方法中添加了获取car.id的这一行代码
 def show(Registration registration) {
respond registration
println params
Car b = Car.get(params.id)
}

这是我在 View registration / index.gsp中的注册页面
<!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&hellip;"/></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>

我在这里添加此代码以获得car.id
 <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>

g:link标记将创建一个指向订单 Controller 的租金操作的链接。然后可以从参数中访问汽车信息。

新更改

请尝试以下更改。让我知道他们是否有效。

1) 注册 Controller
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]
}

在show Action 中注释这些行,如下所示
def show(Registration registration) {
respond registration
// println params
// Car b = Car.get(params.id)
}

2) 汽车/index.gsp
<g:link action="index" controller="registration" params="[carId : ${car.id} ]">
Rent Car
</g:link>

3) 注册/index.gsp
<div><p>Car ID : ${car.id} </p></div>

关于grails - 如何在grails中创建动态按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41685059/

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