grails - Grails-如何将参数从另一个 Controller 保存到 Controller

标签 grails parameter-passing

我有一个具体的问题;

我是grails的新手,我正在尝试使用这种情况编写一些测试应用程序;

我有2个域类

1辆
2-注册

我想添加一些带有品牌,型号等的汽车。用户将从列表中选择一些汽车,然后单击“租车”按钮。当用户单击以租车时,按钮将在此处打开注册/创建页面:我要获取汽车:用户选择的汽车的ID,用户将输入自己的一些额外信息,例如“您的名字”,当用户单击注册/保存按钮时,列表中,我需要查看car: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"])
        model()
        fuelType(inList:["FUEL", "DIESEL", "AUTOGAS"])
        pricePerDay(min:0.0, max:1000.0)
        busy(inList:["YES", "NO"])

    }
}

我有注册课,
package rentme

class Registration {

    String yourName



    static constraints = {
        yourName()
    }
}

所以我做了

我在g:each中添加了cars / index.gsp这个链接;
<g:each in="${carList}" var="car">
                    <p>${car.id} </p>
                    <g:link action="create" controller="registration" params="${[car: car, carId : car.id ]}">
                        Rent Car
                    </g:link>

                </g:each>

并像这样修改了注册 Controller ;
 def index(Integer max) {
        params.max = Math.min(max ?: 10, 100)
        respond Car.list(params), model:[carCount: Car.count()]
    }

现在发生了什么事;当我单击“租借”按钮页面时,重定向到具有汽车用户选择的捕捉ID的注册/创建页面;

这是我的注册/ 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">
                    <g:submitButton name="create" class="save" value="${message(code: 'default.button.create.label', default: 'Create')}" />
                </fieldset>
            </g:form>

当填写您的姓名字段并单击保存按钮时,一切都很好,所以就像它在工作,但不是:)

因为当我检查数据库时,我没有看到选择的汽车ID。
mysql> SELECT * FROM registration;
+----+---------+--------------+
| id | version | your_name    |
+----+---------+--------------+
| 12 |       0 | Cihan Zengin |
| 13 |       0 | Cihan        |
| 14 |       0 | ndlkdkldnkl  |
+----+---------+--------------+

但是,当我单击注册/索引或注册/显示时,只有您的名字,但我也想查看car:id。

怎么做?请有人告诉我,因为我听不懂,在互联网上找不到类似的东西

最佳答案

假设每次注册只有一辆汽车,每人只有一个人。

首先,对于注册类:

package rentme

class Registration {

    String yourName
    Car rentedCar

    static constraints = {
        yourName()
    }
}

如您所见,我在内部添加了Car,因此对于每个注册对象,都会有一个Car对象。在您的注册表数据库中,将自动添加新列“CAR_ID”。

接下来,对于cars / 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>
action="create"controller="registration"表示将调用create()中的registrationController方法,如果找到或未找到,将呈现registration / create.gsp。在registrationController中,我不确定您的索引方法是什么,只需添加以下内容:
def create() {
    def selectedCar = Car.get(params.carId)

    [car: selectedCar]
}

def save() {
    def selectedCar = Car.get(params.carId)

    def registration = new Registration(
        yourName: params.yourName,
        car: selectedCar
    ).save()
}

然后,在registration / 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()方法从car / index.gsp中获取carId的参数,并使用它来获取Car对象,并在registration / create.gsp中进行渲染

registration / create.gsp中的隐藏字段可确保在提交表单时将汽车ID传递给 Controller ​​。因此,单击SubmitButton时,将调用Registration Controller中的save()方法,并保存带有检索到的Car Object的Registration对象。

我们在这里所做的工作有很多不良做法,但应该可以解决。让我知道是否仍然不清楚:)

关于grails - Grails-如何将参数从另一个 Controller 保存到 Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41714835/

相关文章:

grails - Grails GSP渲染问题

email - Grails 邮件 Exchange 服务器配置

swift - Swift中传递参数的顺序

Powershell 启动进程脚本调用第二个脚本 - 如何只制作一个脚本

powershell - 什么是 [cmdletbinding()] 以及它如何工作?

c# - 使用参数化查询填充数据列表

grails - 如何更改我的 View 文件以获得适当的输出?

grails - Sonar 5.1.1 与 Groovy Plugin 1.1.1 项目始终报告 0.0 天的技术债务

ios - 额外参数错误

grails - Java类和Hibernate映射的Grails约束