[英]How to forward a request from a servlet to a specific tab of a JSP page?
If I use the following url in the getRequestDispatcher method I get the below error from Tomcat 如果我在getRequestDispatcher方法中使用以下网址,则会从Tomcat中获取以下错误
404 The requested resource is not available
404所请求的资源不可用
RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/nodes/node_configuration.jsp#tabs-3");
This url if hit directly on the address bar would normally take me to the third tab of a jQuery-built tab structure (used the source code from https://jqueryui.com/tabs/
). 如果直接在地址栏上击中该URL,通常会将我带到jQuery构建的标签结构的第三个标签(使用了
https://jqueryui.com/tabs/
的源代码)。 If I remove the #tabs-3
it will not throw an error but it will take me to the first tab always. 如果我删除
#tabs-3
它不会抛出错误,但是总会带我到第一个选项卡。
I tried to workaround this problem by modifying the jsp by passing the tab as a url parameter. 我试图通过将选项卡作为url参数传递来修改jsp来解决此问题。 So, now the url would be
nodes/node_configuration.jsp?selectedTab=#tabs-3
. 因此,现在的网址将是
nodes/node_configuration.jsp?selectedTab=#tabs-3
。 This works fine in the address bar, but it is neglected by the dispatcher. 这在地址栏中工作正常,但被调度程序忽略。
Since I could not do it the easy way I did it the hard way. 由于我无法轻松完成此操作,因此我难以完成。 In the servlet I pass the selected tab as a request object attribute:
在servlet中,我将选定的选项卡作为请求对象属性传递:
request.setAttribute("selectedTab", "3");
Here is the script in the jsp: 这是jsp中的脚本:
<script>
$(function() {
var param = document.getElementById("selectedTabInput").value;
if (param != 0) {
$('#tabs').tabs({
active : param
});
} else {
$('#tabs').tabs();
}
});
</script>
I am taking the selected tab from a hidden input value inside the body of the jsp: 我从jsp主体内的隐藏输入值中获取选定的选项卡:
<input type="hidden" id="selectedTabInput" value="${requestScope.selectedTab}">
You have to realize, that the tab is a front-end (browser side) concern. 您必须意识到,选项卡是前端(浏览器端)方面的问题。 So you have to pass the tab name (or id) from the back-end to the front-end, eg as a script variable, and then switch to the proper tab using a script (with jQueryUI Tabs using eg the
active
option). 因此,您必须将标签名称(或id)从后端传递到前端,例如作为脚本变量,然后使用脚本切换到适当的标签(使用jQueryUI Tabs使用例如
active
选项)。
getRequestDispatcher()
expects a servlet path (ie the path part in a URL without the app's context path, and without all things starting with ? and #). getRequestDispatcher()
需要servlet路径(即URL中的路径部分,没有应用程序的上下文路径,并且所有内容都以?和#开头)。 You cannot pass a whole URL into it. 您不能将整个URL传递给它。
You could pass the hash thing into the JSP using a request attribute, ie request.setAttribute( "hashTarget", "tabs-3");
您可以使用请求属性,即
request.setAttribute( "hashTarget", "tabs-3");
将哈希值传递到JSP中request.setAttribute( "hashTarget", "tabs-3");
Then you have to read this in the JSP and start a JavaScript to act on it. 然后,您必须在JSP中阅读此内容,然后启动JavaScript对其执行操作。 (Like proposed in the previuos answer.)
(就像在先前的答案中提出的一样。)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.