为什么getQueryString()在带有h:commandButton的jsf支持bean中不起作用
我已经在菜单栏顶部构建了一段登录代码。如果用户通过导航进入任何页面并突然按下登录按钮,我希望看到该用户经过身份验证,同时停留在他最初来自的页面上。所以我在支持bean上使用了这个:为什么getQueryString()在带有h:commandButton的jsf支持bean中不起作用,jsf,jsf-2,Jsf,Jsf 2,我已经在菜单栏顶部构建了一段登录代码。如果用户通过导航进入任何页面并突然按下登录按钮,我希望看到该用户经过身份验证,同时停留在他最初来自的页面上。所以我在支持bean上使用了这个: HttpServletRequest request = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest(); 然后,如果存在mypage.htm?a=2&b=2,则使用request.ge
HttpServletRequest request = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
然后,如果存在mypage.htm?a=2&b=2,则使用request.getQueryString()获取a=2&b=2
但是getQueryString返回null,我如何能够完全从支持bean获得原始URL
向服务器发送POST请求,更具体地说是向Faces Servlet发送POST请求,JSF生命周期开始。您可能知道,在POST请求中无法访问查询字符串参数
如果要在访问页面时从表单发送GET请求或检索查询字符串参数,则必须使用
更多信息:
它返回了null
,因为命令按钮没有提交到包含查询字符串的URL。查看
的生成HTML输出。您将看到生成的HTML
根本不包含查询字符串
您需要在登录链接/按钮中将当前查询字符串作为请求参数传递给自己
获取当前请求URI和查询字符串,并将其作为登录链接/按钮中的参数添加为(注意:使用普通链接/按钮,不需要是命令链接/按钮):
在登录操作方法中,重定向到它:
public void submit() throws IOException {
User user = userService.find(username, password);
if (user != null) {
// ... Do your login thing.
FacesContext.getCurrentInstance().getExternalContext().redirect(from);
} else {
// ... Do your "unknown login" thing.
}
}
您还可以通过将
包装
更改为includeRequestParams设置为true的方式来解决此问题:
虽然
确实是解决方案的一部分,但我相信你误解了具体问题。巴卢斯,一如既往,谢谢你
<f:metadata>
<f:viewParam name="from" value="#{login.from}" />
</f:metadata>
public void submit() throws IOException {
User user = userService.find(username, password);
if (user != null) {
// ... Do your login thing.
FacesContext.getCurrentInstance().getExternalContext().redirect(from);
} else {
// ... Do your "unknown login" thing.
}
}