Using "get" in an action for Grails controllers causes the action to execute even when not called

Problem. The controller action has a rendering tag without passing in the model. There is an action that begins with the word "get".

Grails app / views / site / home.gsp:

homepage 

SiteController.groovy:

 class SiteController { def index() { render (view: "home") } def getTest() { render "getTest" } } 

The site is accessed at localhost: 8080 / site to perform the SiteController index action.

Expected Result: home page Actual Result: getTest home page

If the action of rendering the index changes to this:

 render(view: "home", model: [:]) 

The expected result is displayed.

If a character is added before the word get in the action name, the expected result is returned.

Interestingly, getTest () is color coded as purple in IDEA. It should also be noted that if you have several methods with the word get at the beginning, they ALL are executed.

This did not happen in Grails 1.3.6. This reproduces in the new Grails 2.2.2 project and seems to me a mistake. Why is this happening?

+6
source share
1 answer

GRAILS-9310 suggests that this is a known limitation that will not be changed, the workaround is not to call your actions get* , The main reason is

If an explicit model is not returned, the controller properties will be used as a model ( Grails docs )

When you define the getTest() method, it means that the controller has the test property, and when rendering the "home" view without an explicit model, the controller properties get an enumeration to form the model map. The getTest() method will be called as part of this enumeration process.

If you really need to getTest appear in the URL, then you will need to specify the actual action something else, and then define a custom URL mapping to direct the URI /controller/getTest to the renamed action.

+8
source

Source: https://habr.com/ru/post/948004/


All Articles