如何在页面加载时处理支持 bean 中的 GET 查询字符串 URL 参数? [英] How do I process GET query string URL parameters in backing bean on page load?

查看:25
本文介绍了如何在页面加载时处理支持 bean 中的 GET 查询字符串 URL 参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经阅读了如何使用 JSF 发送参数,但是如果用户在访问他们的登录页面时在 URL 中键入他们的 companyId 会怎样?例如,

I've read how to send parameters using JSF but what if the user types their companyId in the URL when accessing their login page? For example,

http://my.company.url/productName/login.faces?companyId=acme.

按照我们现在的做法,有一些 scriptlet 代码从请求中获取值,然后在会话中设置它.该参数从登录页面开始改变他们的外观,因此每个客户可以有不同的登录页面视图.在我切换到 JSF 之前,我们一直在使用 extjs.

The way we do it now, there is a bit of scriptlet code that grabs the value from the request and then set it in the session. That parameter changes their look and feel starting from the login page forward so each customer could have a different login page view. We are using extjs until I switch over to JSF.

有没有办法使用 JSF 2 或者 PrimeFaces 来做到这一点?

Is there a way to do that using JSF 2 or perhaps PrimeFaces?

推荐答案

是的,您可以使用 将请求参数设置为托管 bean 属性.

Yes, you can use the <f:viewParam> to set a request parameter as a managed bean property.

<f:metadata>
    <f:viewParam name="companyId" value="#{bean.companyId}" />
</f:metadata>

如有必要,您可以使用 (仅限 JSF 2.2+)或 调用 bean 操作代码>.

You can if necessary invoke a bean action using <f:viewAction> (JSF 2.2+ only) or <f:event type="preRenderView">.

<f:metadata>
    <f:viewParam name="companyId" value="#{bean.companyId}" />
    <f:viewAction action="#{bean.onload}" />
</f:metadata>

使用 时,您甚至可以返回导航结果.

When using <f:viewAction> you can even return a navigation outcome.

public String onload() {
    // ...

    return "somepage";
}

如果还没有在 JSF 2.2 上,您可以使用 ExternalContext#redirect().另请参阅如何在 preRenderView 侦听器方法中执行导航.

When not on JSF 2.2 yet, you can use ExternalContext#redirect() for that. See also among others How to perform navigation in preRenderView listener method.

请注意,这不是 PrimeFaces 特有的.它只是标准 JSF 的一部分.PrimeFaces 只是一个组件库,提供增强的 ajax 和外观支持.

Note that this is not specific to PrimeFaces. It's just part of standard JSF. PrimeFaces is merely a component library which provides enhanced ajax and skinnability support.

这篇关于如何在页面加载时处理支持 bean 中的 GET 查询字符串 URL 参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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