如何让用户在Grails中更改域类中的值(How do you make the user change a value in the domain class in Grails)

有没有办法通过让用户输入新值来更改域类中的值? 因此,如果我们有一个视图页面和一个输入框,输入框中输入的内容将修改域类中的特定值

例如:

class User { String userName }

在视图页面中:

<input type="userName" action="changeUsername">

Is there a way to change a value in the domain class by having the user input the new value ? so if we have a view page, and an input box, whatever is entered in the input box will modify a specific value in the domain class

for e.g:

class User { String userName }

in views page:

<input type="userName" action="changeUsername">

最满意答案

请听阿丽达的建议:

看看Grails脚手架 ,它是了解Grails如何工作的绝佳资源.- Alidad

话虽如此,我们可以看一下运行grails generate-all testapp.User时grails generate-all testapp.User以及它与你的问题的关系。

generate-all创建一个基于您的域类User的Controller和Views。

package testapp class User { String userName static constraints = { userName() } }

请注意静态约束(这是添加验证的好地方),但是为了生成与特定值相关的视图和控制器,您需要的只是将其添加到域类中的静态约束中。

您希望能够更新域类。 那么为我们生成的代码已经生成了! 如果你看一下views / user / edit.gsp

<g:form url="[resource:userInstance, action:'update']" method="PUT" > <g:hiddenField name="version" value="${userInstance?.version}" /> <fieldset class="form"> <g:render template="form"/> </fieldset> <fieldset class="buttons"> <g:actionSubmit class="save" action="update" value="${message(code: 'default.button.update.label', default: 'Update')}" /> </fieldset> </g:form>

这将创建一个表单来更新我们在这种情况下选择userInstance的当前用户。

另外一个重要的观察是<g:form url="[resource:userInstance, action:'update']" method="PUT" >这告诉我们将根据资源调用哪个控制器,并且将基于动作(你可以在这里看一下控制器调用的好设计Grails WebServices REST )

这将调用controllers / testapp / UserController.groovy - 更新,它接受一个User实例

@Transactional def update(User userInstance) { if (userInstance == null) { notFound() return } if (userInstance.hasErrors()) { respond userInstance.errors, view:'edit' return } userInstance.save flush:true request.withFormat { form multipartForm { flash.message = message(code: 'default.updated.message', args: [message(code: 'User.label', default: 'User'), userInstance.id]) redirect userInstance } '*'{ respond userInstance, [status: OK] } } }

您可以看到一些检查以确保实例存在且没有错误(基于您在域类中添加的验证)然后保存更新的用户实例,然后返回消息。 将“工作”移出控制器并进入服务类也是一种很好的做法。

但是有很多不同的方法来给猫皮肤涂抹。 视图和控制器之间用于更新Domain类的通信可以通过许多不同方式完成。

嗯,我希望这是有道理的,只是玩耍,玩得开心!

Please take Alidad's advice:

Take a look at Grails scaffolding, it is a great resource to understand how Grails works.– Alidad

That being said we can look at what is generated when running grails generate-all testapp.User and how it relates to your question.

generate-all with create a Controller and Views based off of your domain class User.

package testapp class User { String userName static constraints = { userName() } }

Notice the static constraints (this is a great place to add validation) but all you need in order to generate Views and controllers related to specific values is to add it to your static constraints within your domain class.

You want to be able to update a domain class. Well the code to do this has been generated for us! if you look at views/user/edit.gsp

<g:form url="[resource:userInstance, action:'update']" method="PUT" > <g:hiddenField name="version" value="${userInstance?.version}" /> <fieldset class="form"> <g:render template="form"/> </fieldset> <fieldset class="buttons"> <g:actionSubmit class="save" action="update" value="${message(code: 'default.button.update.label', default: 'Update')}" /> </fieldset> </g:form>

This will create a form to update the current user we have selected userInstance in this case.

Also an important observation is <g:form url="[resource:userInstance, action:'update']" method="PUT" > this tells us which controller will be called based on the resource and method will be called based on the action (You can look more into good design for controller calling here Grails WebServices REST)

This will call controllers/testapp/UserController.groovy - update which takes a User instance

@Transactional def update(User userInstance) { if (userInstance == null) { notFound() return } if (userInstance.hasErrors()) { respond userInstance.errors, view:'edit' return } userInstance.save flush:true request.withFormat { form multipartForm { flash.message = message(code: 'default.updated.message', args: [message(code: 'User.label', default: 'User'), userInstance.id]) redirect userInstance } '*'{ respond userInstance, [status: OK] } } }

Which you can see does some checks to make sure the instance exists and doesn't have errors (which is based off of the validation you add in your Domain Class) then saves your updated User Instance and then returns a message back. It is also a good practice to move the "work" out of your controller and into a service class.

But there are a lot of different ways to skin a cat. This communication between view and Controller to update a Domain class can be done in many different ways.

Well I hope that makes sense, just play around and have fun!

如何让用户在Grails中更改域类中的值(How do you make the user change a value in the domain class in Grails)

有没有办法通过让用户输入新值来更改域类中的值? 因此,如果我们有一个视图页面和一个输入框,输入框中输入的内容将修改域类中的特定值

例如:

class User { String userName }

在视图页面中:

<input type="userName" action="changeUsername">

Is there a way to change a value in the domain class by having the user input the new value ? so if we have a view page, and an input box, whatever is entered in the input box will modify a specific value in the domain class

for e.g:

class User { String userName }

in views page:

<input type="userName" action="changeUsername">

最满意答案

请听阿丽达的建议:

看看Grails脚手架 ,它是了解Grails如何工作的绝佳资源.- Alidad

话虽如此,我们可以看一下运行grails generate-all testapp.User时grails generate-all testapp.User以及它与你的问题的关系。

generate-all创建一个基于您的域类User的Controller和Views。

package testapp class User { String userName static constraints = { userName() } }

请注意静态约束(这是添加验证的好地方),但是为了生成与特定值相关的视图和控制器,您需要的只是将其添加到域类中的静态约束中。

您希望能够更新域类。 那么为我们生成的代码已经生成了! 如果你看一下views / user / edit.gsp

<g:form url="[resource:userInstance, action:'update']" method="PUT" > <g:hiddenField name="version" value="${userInstance?.version}" /> <fieldset class="form"> <g:render template="form"/> </fieldset> <fieldset class="buttons"> <g:actionSubmit class="save" action="update" value="${message(code: 'default.button.update.label', default: 'Update')}" /> </fieldset> </g:form>

这将创建一个表单来更新我们在这种情况下选择userInstance的当前用户。

另外一个重要的观察是<g:form url="[resource:userInstance, action:'update']" method="PUT" >这告诉我们将根据资源调用哪个控制器,并且将基于动作(你可以在这里看一下控制器调用的好设计Grails WebServices REST )

这将调用controllers / testapp / UserController.groovy - 更新,它接受一个User实例

@Transactional def update(User userInstance) { if (userInstance == null) { notFound() return } if (userInstance.hasErrors()) { respond userInstance.errors, view:'edit' return } userInstance.save flush:true request.withFormat { form multipartForm { flash.message = message(code: 'default.updated.message', args: [message(code: 'User.label', default: 'User'), userInstance.id]) redirect userInstance } '*'{ respond userInstance, [status: OK] } } }

您可以看到一些检查以确保实例存在且没有错误(基于您在域类中添加的验证)然后保存更新的用户实例,然后返回消息。 将“工作”移出控制器并进入服务类也是一种很好的做法。

但是有很多不同的方法来给猫皮肤涂抹。 视图和控制器之间用于更新Domain类的通信可以通过许多不同方式完成。

嗯,我希望这是有道理的,只是玩耍,玩得开心!

Please take Alidad's advice:

Take a look at Grails scaffolding, it is a great resource to understand how Grails works.– Alidad

That being said we can look at what is generated when running grails generate-all testapp.User and how it relates to your question.

generate-all with create a Controller and Views based off of your domain class User.

package testapp class User { String userName static constraints = { userName() } }

Notice the static constraints (this is a great place to add validation) but all you need in order to generate Views and controllers related to specific values is to add it to your static constraints within your domain class.

You want to be able to update a domain class. Well the code to do this has been generated for us! if you look at views/user/edit.gsp

<g:form url="[resource:userInstance, action:'update']" method="PUT" > <g:hiddenField name="version" value="${userInstance?.version}" /> <fieldset class="form"> <g:render template="form"/> </fieldset> <fieldset class="buttons"> <g:actionSubmit class="save" action="update" value="${message(code: 'default.button.update.label', default: 'Update')}" /> </fieldset> </g:form>

This will create a form to update the current user we have selected userInstance in this case.

Also an important observation is <g:form url="[resource:userInstance, action:'update']" method="PUT" > this tells us which controller will be called based on the resource and method will be called based on the action (You can look more into good design for controller calling here Grails WebServices REST)

This will call controllers/testapp/UserController.groovy - update which takes a User instance

@Transactional def update(User userInstance) { if (userInstance == null) { notFound() return } if (userInstance.hasErrors()) { respond userInstance.errors, view:'edit' return } userInstance.save flush:true request.withFormat { form multipartForm { flash.message = message(code: 'default.updated.message', args: [message(code: 'User.label', default: 'User'), userInstance.id]) redirect userInstance } '*'{ respond userInstance, [status: OK] } } }

Which you can see does some checks to make sure the instance exists and doesn't have errors (which is based off of the validation you add in your Domain Class) then saves your updated User Instance and then returns a message back. It is also a good practice to move the "work" out of your controller and into a service class.

But there are a lot of different ways to skin a cat. This communication between view and Controller to update a Domain class can be done in many different ways.

Well I hope that makes sense, just play around and have fun!