grails-controller

Grails Command Object: How to load request.JSON into it?

偶尔善良 提交于 2019-12-05 00:24:25
Question : is there a way to do automatic command object binding with request.JSON data? Given this simple Command object in my grails controller: class ProfileCommand{ int id String companyName static constraints = { companyName blank: false id nullable: false } @Override public String toString() { return "ProfileCommand{id=$id, companyName='$companyName'}"; } } and my controller method signature of: def update(ProfileCommand command) {...} How can I get request.JSON data into my command object? So far, the only way I've been able to do it is to create the command object manually within the

Grails controller rendering method render vs respond

巧了我就是萌 提交于 2019-12-04 15:13:09
问题 I just realised that for a Grails controller there is another rendering method 'respond'. What's the difference between respond and render method if we want to render a view in the controller. 回答1: The respond method uses content negotiation to respond with the most appropriate content type based on the requests 'ACCEPT' header. Accept: text/html, application/xhtml+xml, application/xml;q=0.9, */*;q=0.8, application/json This way the consumer of your site can choose how they wish to be

Grails 2.4.2 - Dynamically referencing default datasource

余生颓废 提交于 2019-12-04 09:21:37
This question has been partly answered here but there is still an issue with referencing the default datasource dynamically. I'm working on an internal application that allows developers to modify configuration settings for one of our multi-tenant applications and push those settings from dev to testing, staging and production. Each one of these will have their own datasource, and the Grails app will be installed on each developer's computer. The local datasource will be the default one, and then dataSource_testing, dataSource_staging and so on will reference the appropriate environments. I

Groovy Grails, How do you stream or buffer a large file in a Controller's response?

五迷三道 提交于 2019-12-04 09:12:04
问题 I have a controller that makes a connection to a url to retrieve a csv file. I am able to send the file in the response using the following code, this works fine. def fileURL = "www.mysite.com/input.csv" def thisUrl = new URL(fileURL); def connection = thisUrl.openConnection(); def output = connection.content.text; response.setHeader "Content-disposition", "attachment; filename=${'output.csv'}" response.contentType = 'text/csv' response.outputStream << output response.outputStream.flush()

Why does Grails recommend singleton scope for controllers with actions as methods?

非 Y 不嫁゛ 提交于 2019-12-04 03:34:59
I know early versions of Grails used prototype scope for controllers because actions were all closures at that time. I know that the current version documentation recommends singleton scoped controllers for controllers that use methods as actions. From the following post it seems that methods and singleton scope are more desirable or recommended, but it's not clear why. ttp://grails.1312388.n4.nabble.com/Default-scope-for-controllers-doc-td4657986.html We have a large project that uses prototype scoped controllers with actions as methods. Changing to the recommended controller scope involves

Grails update instead of delete

感情迁移 提交于 2019-12-03 13:40:49
问题 Is there an easy way in Grails to not allow deleting for any Domain Class? And rather have a delete flag in each domain which gets updated whenever something is deleted. Also, in effect all the list/show methods should not show objects where delete flag is true. I know I can do that by manually editing all my CRUD methods in all the controllers but that seems a little bit too much work when working with Grails where everything can be done by changing some flag somewhere!! My usual list method

how to use session in grails

只谈情不闲聊 提交于 2019-12-03 09:41:04
问题 I am new to grails. And I have to work with session. I have seen the session documentation. But no idea where to put the code in my controller. I have a page for student creation names createStudent. Now I want that this page only be access able when the user will be in session. Now how can I do it. Should I have to set the user in a variable at the time of login. Can anyone please help me on this ? def index() { def user = session["user"] if (user){ redirect(controller: 'admistratorAction',

Grails controller rendering method render vs respond

随声附和 提交于 2019-12-03 09:27:44
I just realised that for a Grails controller there is another rendering method 'respond'. What's the difference between respond and render method if we want to render a view in the controller. The respond method uses content negotiation to respond with the most appropriate content type based on the requests 'ACCEPT' header. Accept: text/html, application/xhtml+xml, application/xml;q=0.9, */*;q=0.8, application/json This way the consumer of your site can choose how they wish to be returned data. This may not be the best option if you want to force a specific return type. For example: You are

Grails update instead of delete

随声附和 提交于 2019-12-03 03:38:27
Is there an easy way in Grails to not allow deleting for any Domain Class? And rather have a delete flag in each domain which gets updated whenever something is deleted. Also, in effect all the list/show methods should not show objects where delete flag is true. I know I can do that by manually editing all my CRUD methods in all the controllers but that seems a little bit too much work when working with Grails where everything can be done by changing some flag somewhere!! My usual list method looks like following, almost all the list methods in my project lets user access things which only

Groovy Grails, How do you stream or buffer a large file in a Controller's response?

风格不统一 提交于 2019-12-03 02:22:41
I have a controller that makes a connection to a url to retrieve a csv file. I am able to send the file in the response using the following code, this works fine. def fileURL = "www.mysite.com/input.csv" def thisUrl = new URL(fileURL); def connection = thisUrl.openConnection(); def output = connection.content.text; response.setHeader "Content-disposition", "attachment; filename=${'output.csv'}" response.contentType = 'text/csv' response.outputStream << output response.outputStream.flush() However I think this method is inappropriate for a large file, as the whole file is loaded into the