重定向的链接路径 [英] Link path for redirect

查看:97
本文介绍了重定向的链接路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试重定向我的响应,但是我被卡在了链接路径上.

I'm trying to redirect my response but I got stuck at the link path.

以下命令将我带到tomcat的本地主机,并在其中搜索页面,但是按预期找不到任何内容.

The following command takes me to the tomcat's localhost and searches for the page there but it can't find anything, as expected.

response.sendRedirect("/myPage.html");

为了解决此问题,我必须将我的根文件夹名称(来自webaps的名称)放在链接路径中,但是我认为这不是一个好主意.

In order to manage this problem I have to put my root folder name (the one from webaps) in the link path, but I don't think that's a really good idea.

response.sendRedirect("/mySite/myPage.html");

为什么会这样?除了getRequestURL()或类似的方法,还有其他方法可以解决此问题吗?

Why is this happening? Is there any other way to solve this problem, besides getRequestURL() or something like that?

推荐答案

相对重定向URL是相对于当前请求URL(如您在浏览器地址栏中看到的).前导/将带您到域根目录.该行为是完全可预测的并且是正常的.如果myPage.html与当前请求URL位于同一文件夹中,则只需删除开头的/:

A relative redirect URL is relative to the current request URL (the one as you see in the browser address bar). The leading / will take you to the domain root. The behaviour is perfectly predictable and normal. If the myPage.html is in the same folder as the current request URL, then you can just remove the leading /:

response.sendRedirect("myPage.html");

或者如果由于当前请求URL可能来自您的Web应用程序中的任何位置而永远无法可靠地预测,那么只需动态地包含上下文路径即可:

Or if that is never reliably predictable because the current request URL can come from anywhere in your webapp, then just dynamically include the context path:

response.sendRedirect(request.getContextPath() + "/myPage.html");

这篇关于重定向的链接路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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