I use forward instead of redirect (to hide parameters in the url), the problem is that I want to pass a fragment to forward so my page will scroll to the specified location.
With redirect, this works:
redirect(controller: "mycontroller", action: "myaction", fragment: "anchor")
But with forward, it doesn’t scroll to the anchor location:
forward(controller: "mycontroller", action: "myaction", fragment: "anchor")
How can I solve this?
The reason the fragment works with
redirectis because redirect tells the browser to make another (GET) request to the specified URL. This gives the browser the opportunity to 1. Fetch the new page, and 2. Scroll to the appropriate anchor.With
forward, the servlet container simply tells another piece of code to handle the request – it’s effectively invisible to the browser, so the browser won’t be able to scroll anywhere. That’s whyfragmentisn’t a valid option forforward.In order to accomplish this, you’d have to either:
Any of the above should work under the appropriate conditions; however, I’m not sure we have enough information about the request you’re making to provide a more specific example.