如何使用RequestDispatcher从servlet转发到内部页面链接(How to forward to an internal page link from a servlet with R

编程入门 行业动态 更新时间:2024-10-28 19:33:16
如何使用RequestDispatcher从servlet转发到内部页面链接(How to forward to an internal page link from a servlet with RequestDispatcher) java

我有一个jsp页面,页面中间有一个下拉控件。 当用户更改下拉列表的选择时,页面将提交给servlet并转发回同一页面,其中包含有关要显示的用户选择的详细信息。 我希望页面自动导航回下拉菜单,而不是用户必须自己回滚到它。

在检索适当数据的代码之后的servlet中,我有:

String forwardTo = "/WEB-INF/jsp/task-create.jsp#taskTypeSelection" request.getRequestDispatcher(forwardTo).forward(request, response);

我希望servlet转到的jsp中的链接放在下拉控件上方,如下所示:

<a name="taskTypeSelection" id="taskTypeSelection"></a> <select class="form-control" id="taskTypeID" name="taskTypeID"> <option value="">Select a task type.</option> <option...

但是,这不起作用,当Dispatcher尝试转发时,我收到404错误。 在使用RequestDispatcher的当前设置时,有没有办法使这项工作? 我认为如果我使用HttpServletResponse.sendRedirect,这将不是一个问题,但我使用RequestDispatcher增加安全性,因为我已将所有jsps放在WEB-INF文件夹中因此无法访问sendRedirect(至少这是我的理解。很多这对我来说都是新的)。

谢谢!

I have a jsp page with a drop-down control mid-way down the page. When the user changes the selection of the drop-down the page submits to a servlet and forwards back to the same page with details about the user selection to be displayed. I'd like the page to automatically navigate back to the drop-down instead of the user having to scroll back to it on their own.

In the servlet after the code that retrieves the appropriate data, I have:

String forwardTo = "/WEB-INF/jsp/task-create.jsp#taskTypeSelection" request.getRequestDispatcher(forwardTo).forward(request, response);

The link in the jsp I want the servlet to go to is placed above the drop-down control and looks like:

<a name="taskTypeSelection" id="taskTypeSelection"></a> <select class="form-control" id="taskTypeID" name="taskTypeID"> <option value="">Select a task type.</option> <option...

However, this doesn't work and I get a 404 error when the Dispatcher tries to forward. Is there a way to make this work while using my current setup with the RequestDispatcher? I figure this would be less of a problem if I used HttpServletResponse.sendRedirect, but I am using RequestDispatcher for added security and because I have put all my jsps in the WEB-INF folder and therefore are inaccessible for sendRedirect (at least that is my understanding. A lot of this is new to me).

Thanks!

最满意答案

这更像是基于Quan Nguyen帮助的解决方法,而不是原始问题的答案。

在servlet中,我只是设置一个请求变量,“scrollTo”到名称,如果我想要转发的页面去的链接ID。 然后,我创建了一个JS脚本(基于jQuery滚动到元素 )来查找触发文档ready()事件的“scrollTo”请求变量(使用JSTL el)。 如果找到一个,则它将页面滚动到指示的jsp中的链接。

Servlet的:

request.setAttribute("scrollTo", "taskSelection");

JS脚本:

$(document).ready(function(){ var scrollTo = "${scrollTo}"; if(scrollTo !== "" && scrollTo!== null && scrollTo !== undefined){ $('html, body').animate({ scrollTop: $("#" + scrollTo).offset().top }, 0); } });

This is more of a workaround based on Quan Nguyen's help than an answer to the original question.

In the servlet, I just set a request variable, "scrollTo" to the name if the link id that I want the forwarded page to go to. Then, I created a JS script (based off jQuery scroll to element) to look for the "scrollTo" request variable (using JSTL el) that fires on the document's ready() event. If one is found then it scrolls the page to the link in the jsp that was indicated.

Servlet:

request.setAttribute("scrollTo", "taskSelection");

JS Script:

$(document).ready(function(){ var scrollTo = "${scrollTo}"; if(scrollTo !== "" && scrollTo!== null && scrollTo !== undefined){ $('html, body').animate({ scrollTop: $("#" + scrollTo).offset().top }, 0); } });

更多推荐

本文发布于:2023-07-16 03:56:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1123390.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何使用   页面   链接   RequestDispatcher   link

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!