在带有RequestDispatcher的正向URL中使用哈希符号 [英] Using hash symbol in forward URL with RequestDispatcher

查看:66
本文介绍了在带有RequestDispatcher的正向URL中使用哈希符号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将请求转发到包含哈希符号('#')的另一个URL:

I'm trying to forward a request to another URL which includes the hash symbol ('#'):

request.getRequestDispatcher("/some/path.jsp#somehash").forward(request, response);

但是,Tomcat告诉我请求的资源不可用".如果我从网址中删除哈希,一切正常.不允许使用哈希,还是我不正确对待它们?

Tomcat, however, tells me that "the requested resource is not available". If I remove the hash from the URL, everything works fine. Are hashes not allowed or am I not treating them right?

推荐答案

#符号是浏览器,而不是服务器.当您在浏览器中键入带有#的URL时,浏览器不会将该部分发送到服务器.它发送不带URL的URL,然后在返回页面时跳转到命名锚.

The # symbol is a browser thing, not a server thing. When you type a URL with a # into the browser, the browser doesn't send that part to the server. It sends the URL without it, then jumps to the named anchor when it gets the page back.

当您要求容器为您获取该URL时,它对#的对待与对任何其他URL的对待都没有任何不同-它没有特殊含义,因此它寻找一个名为/some/path.jsp#somehash的JSP页面. ,当然不存在.

When you ask the container to get that URL for you, it doesn't treat the # any differently to any other URL - it has no special meaning for it, so it looks for a JSP page called /some/path.jsp#somehash, which of course doesn't exist.

您需要以某种方式在客户端上保留跳转到锚定逻辑.也许您可以在结果页面上放置一些javascript,以滚动到文档中的该点.

You'll need to keep that jump-to-anchor logic on the client somehow. Perhaps you could put some javascript on the resulting page to scroll to that point in the document.

这篇关于在带有RequestDispatcher的正向URL中使用哈希符号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆