Suppose I have a controller and an action:
public Questions extends BaseController {
public static void show(id) {
// how to get the anchor here??
}
}
A url with anchor like http://aaa.com/questions/show?id=123#555
will map to this ac开发者_高级运维tion, and how can we get the anchor 555
in action show
?
To add anchor to url, i use redirect method and Router class, so for your case :
public Questions extends BaseController {
public static void show(id) {
some stuff...
Map<String, Object> args = new HashMap<String, Object>();
args.put("id", questionID);
String url = Router.getFullUrl("Questions.show", args);
redirect(url + "#555");
}
}
If I understand you correctly, it is not possible to send the anchor part of a url back to the server as part of the http request. It is used locally only, for anchoring a page for intra page navigation.
See this post for more info Retrieving Anchor Link In URL for ASP.Net
However, if you want to add the anchor in your view, rather than using redirects and using the router in the controller, you can simply do it in the view (where it best belongs)
<a href='@{Questions.show(id)}#555'>link to question</a>
精彩评论