在登录前将他们正在查看的内容重定向回实际页面 [英] Redirecting back to actual page what they were viewing before login
问题描述
如果我的用户被重定向到登录页面,如何使用,以便在他们登录后,他们被重定向到-original-destination页面(他们首次点击的页面)?
If my user is redirected to a login page, how to make it so that, after they log in, they get redirected to the -original- destination page (the one they first clicked on)?
推荐答案
将最初请求的URL作为请求参数传递给重定向到登录页面。
Pass the originally requested URL as request parameter on the redirect to the login page.
String from = URLEncoder.encode(request.getRequestURI(), "UTF-8");
if (request.getQueryString() != null) {
from += "?" + request.getQueryString();
}
response.sendRedirect("login.jsp?from=" + from);
在 login.jsp
中,将其传递给登录表单将目标提交为隐藏输入字段。
In login.jsp
, pass it to the login form submit target as a hidden input field.
<input type="hidden" name="from" value="${fn:escapeXml(param.from)}" />
(注意: fn:escapeXml()
在HTML中重新显示用户控制的数据时阻止您使用XSS
(note: fn:escapeXml()
prevents you from XSS when redisplaying user-controlled data in HTML)
在登录操作中,检查它是否存在然后相应地处理。
In the login action, check if it is there and then handle accordingly.
String from = request.getParameter("from");
if (from != null && !from.isEmpty()) {
response.sendRedirect(from);
} else {
response.sendRedirect("home.jsp");
}
这篇关于在登录前将他们正在查看的内容重定向回实际页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!