、可以做什么?和用来做什么?
谁能澄清我们如何在一般情况下或在现实世界中使用此代码片段?
<f:metadata>
<f:viewParam id="id" value="#{bean.id}" />
<f:viewAction action="#{bean.init}" />
</f:metadata>
Can anyone clarify how we can use in general, or a in real world example, this snippet?
<f:metadata>
<f:viewParam id="id" value="#{bean.id}" />
<f:viewAction action="#{bean.init}" />
</f:metadata>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
处理 GET 参数
管理 GET 参数的设置、转换和验证。它类似于
,但适用于 GET 参数。以下示例
基本上执行以下操作:
id
获取请求参数值。required
、validator
和converter
属性并嵌套
就像
一样)#{bean.id}
值,或者如果value
属性不存在,则将其设置为名称id
上的请求属性,以便它可用通过视图中的#{id}
。因此,当您以
foo.xhtml?id=10
打开页面时,参数值10
会在视图渲染之前以这种方式在 bean 中设置。至于验证,以下示例将参数设置为
required="true"
并仅允许 10 到 20 之间的值。任何验证失败都会导致显示一条消息。对 GET 参数执行业务操作
您可以使用
为此。然而
是自 JSF 2.2 以来的新增内容(
自 JSF 2.0 以来已存在)。如果您无法升级,那么最好的选择是使用
代替。然而,这会在每个请求时被调用。您需要显式检查请求是否不是回发:
当您还想跳过“转换/验证失败”情况时,请执行以下操作:
使用
this way 本质上是一种解决方法/hack,这正是 JSF 2.2 中引入
的原因。将视图参数传递到下一个视图
您可以通过将
includeViewParams
属性设置为true
或添加includeViewParams=true
来“传递”导航链接中的视图参数代码>请求参数。它使用上面的
示例基本上生成以下带有原始参数值的链接。
此方法仅要求
next.xhtml
在同一参数上也有一个
,否则不会通过。在 JSF 中使用 GET 表单
还可以与“纯 HTML”GET 表单结合使用。基本上使用这个
@RequestScoped
bean:请注意
用于
,而不是普通的HTML!另请注意,当
#{bean.query}
为空时,输入值会显示#{param.query}
,因为否则当存在时,提交的值根本不会显示验证或转换错误。请注意,此构造对于 JSF 输入组件无效(它已经在“幕后”执行此操作)。另请参阅:
Process GET parameters
The
<f:viewParam>
manages the setting, conversion and validation of GET parameters. It's like the<h:inputText>
, but then for GET parameters.The following example
does basically the following:
id
.required
,validator
andconverter
attributes and nest a<f:converter>
and<f:validator>
in it like as with<h:inputText>
)#{bean.id}
value, or if thevalue
attribute is absent, then set it as request attribtue on nameid
so that it's available by#{id}
in the view.So when you open the page as
foo.xhtml?id=10
then the parameter value10
get set in the bean this way, right before the view is rendered.As to validation, the following example sets the param to
required="true"
and allows only values between 10 and 20. Any validation failure will result in a message being displayed.Performing business action on GET parameters
You can use the
<f:viewAction>
for this.with
The
<f:viewAction>
is however new since JSF 2.2 (the<f:viewParam>
already exists since JSF 2.0). If you can't upgrade, then your best bet is using<f:event>
instead.This is however invoked on every request. You need to explicitly check if the request isn't a postback:
When you would like to skip "Conversion/Validation failed" cases as well, then do as follows:
Using
<f:event>
this way is in essence a workaround/hack, that's exactly why the<f:viewAction>
was introduced in JSF 2.2.Pass view parameters to next view
You can "pass-through" the view parameters in navigation links by setting
includeViewParams
attribute totrue
or by addingincludeViewParams=true
request parameter.which generates with the above
<f:metadata>
example basically the following linkwith the original parameter value.
This approach only requires that
next.xhtml
has also a<f:viewParam>
on the very same parameter, otherwise it won't be passed through.Use GET forms in JSF
The
<f:viewParam>
can also be used in combination with "plain HTML" GET forms.With basically this
@RequestScoped
bean:Note that the
<h:message>
is for the<f:viewParam>
, not the plain HTML<input type="text">
! Also note that the input value displays#{param.query}
when#{bean.query}
is empty, because the submitted value would otherwise not show up at all when there's a validation or conversion error. Please note that this construct is invalid for JSF input components (it is doing that "under the covers" already).See also:
从视图发送参数到另一个视图,从发送者视图到接收者视图使用 viewParam 和 includeViewParams=true
在发送者中
Sender.xhtml
“includeViewParams=true”
中返回单击按钮事件的字符串单击按钮触发 senderMB.clickBtnDetail(dto),其中 dto 来自 senderMB._arrData
Sender.xhtml
在 senderMB.clickBtnDetail(dto) 中,我们使用从按钮事件 (dto) 获得的参数分配 _strID,这里这是 Sender_DTO 并分配给
senderMB._strID点击后链接将变成
http://localhost:8080/my_project/view/Receiver.xhtml?*ID=12345*
在接收器中
接收器.xhtml
在 Receiver 中,我们声明 f:viewParam 从 get 请求(接收)中获取参数,接收者的参数名称必须与发送者(页面)相同
Receiver.xhtml
它将从发送者 View 中获取参数 ID 并分配给receiver_MB._strID
在 Receiver 中,我们希望在页面渲染之前在 sql 查询中使用此参数,以便我们使用 preRenderView 事件。我们不会使用构造函数,因为构造函数将在接收 viewParam 之前被调用
这样我们就可以将
Receiver.xhtml
添加到 f:metadata 标签中
Receiver.xhtml
现在我们想在读取数据库方法中使用这个参数,可以使用它
Send params from View to an other View, from Sender View to Receiver View use viewParam and includeViewParams=true
In Sender
Sender.xhtml
“includeViewParams=true”
in return String of click button eventClick button fire senderMB.clickBtnDetail(dto) with dto from senderMB._arrData
Sender.xhtml
In senderMB.clickBtnDetail(dto) we assign _strID with argument we got from button event (dto), here this is Sender_DTO and assign to senderMB._strID
The link when clicked will become
http://localhost:8080/my_project/view/Receiver.xhtml?*ID=12345*
In Recever
Receiver.xhtml
In Receiver we declare f:viewParam to get param from get request (receive), the name of param of receiver must be the same with sender (page)
Receiver.xhtml
It will get param ID from sender View and assign to receiver_MB._strID
In Receiver, we want to use this param in sql query before the page render, so that we use preRenderView event. We are not going to use constructor because constructor will be invoked before viewParam is received
So that we add
Receiver.xhtml
into f:metadata tag
Receiver.xhtml
Now we want to use this param in our read database method, it is available to use