为什么getQueryString()在使用h:commandButton的jsf支持bean中不起作用 [英] why getQueryString() does not work in jsf backing bean with h:commandButton

查看:147
本文介绍了为什么getQueryString()在使用h:commandButton的jsf支持bean中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在菜单栏的顶部构建了一个代码登录段.如果用户通过导航进入页面中的任何页面,然后突然按下登录按钮,则我希望看到该用户已通过身份验证,并且同时停留在其最初来自的页面上.所以我在支持bean上使用了它:

I have build a login snippet of code that is on the top of menu bar. If a user is in any of the page by navigating and presses all of sudden the login button, I'll like to see that person authenticated and at the same time stay on the page where he originally comes from. so I used this on the backing bean:

HttpServletRequest request = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();

然后,如果说出mypage.htm?a = 2& b = 2与request.getQueryString()一起获得a = 2& b = 2

then if there is let say mypage.htm?a=2&b=2 get a=2&b=2 with the request.getQueryString()

但是getQueryString返回null,我如何才能完全从后备bean获得原始URL?

but getQueryString returns null, how can I can have the original URL entirely from the backing bean?

推荐答案

它返回null,因为命令按钮未提交到包含查询字符串的URL.查看生成的<h:form>的HTML输出.您会看到生成的HTML <form action>根本不包含查询字符串.

It returned null because the command button does not submit to an URL which includes the query string. Look in the generated HTML output of <h:form>. You'll see that generated HTML <form action> does not include the query string at all.

您需要在登录链接/按钮中自己传递当前查询字符串作为请求参数.

You need to pass the current query string yourself as a request parameter in the login link/button.

获取当前请求URI和查询字符串,并将其添加为登录链接/按钮中的from参数(注意:使用常规链接/按钮,它不必是命令链接/按钮):

Grab the current request URI and query string and add it as from parameter in login link/button (note: use a normal link/button, it doesn't need to be a command link/button):

<h:button value="login" outcome="/login">
    <f:param name="from" value="#{request.requestURI}#{empty request.queryString ? '' : '?'}#{request.queryString}" />
</h:button>

在登录页面中,将from参数设置为view scoped bean属性:

In login page, set the from parameter as view scoped bean property:

<f:metadata>
    <f:viewParam name="from" value="#{login.from}" />
</f:metadata>

在登录操作方法中,重定向到它:

In login action method, redirect to it:

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.
    }
}

这篇关于为什么getQueryString()在使用h:commandButton的jsf支持bean中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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