[英]How to pass url parameters to JSF?
I haven't managed to find a way to pass parameters to JSF pages through URL parameters. 我还没有找到通过URL参数将参数传递给JSF页面的方法。
http://www.example.com/jsfApp.jsp?param1=value1¶m2=value2
Could someone point me at the right direction with this? 有人能指出我正确的方向吗?
As you're using JSPs, I'll assume that you're using JSF 1.x. 在使用JSP时,我假设你使用的是JSF 1.x.
To create a link with query parameters, use h:outputLink
with f:param
: 要使用查询参数创建链接,请使用h:outputLink
和f:param
:
<h:outputLink value="page.jsf">
<f:param name="param1" value="value1" />
<f:param name="param2" value="value2" />
</h:outputLink>
The value
can be set dynamically with help of EL. 可以借助EL动态设置该value
。
To set them in the managed bean automagically, you need to define each as managed-property
in faces-config.xml
: 要在托管bean中自动设置它们,您需要在faces-config.xml
中将每个定义为managed-property
:
<managed-bean>
<managed-bean-name>bean</managed-bean-name>
<managed-bean-class>com.example.Bean</managed-bean-class>
<managed-bean-scope>request</managed-bean-scope>
<managed-property>
<property-name>param1</property-name>
<value>#{param.param1}</value>
</managed-property>
<managed-property>
<property-name>param2</property-name>
<value>#{param.param2}</value>
</managed-property>
</managed-bean>
The imlicit EL variable #{param}
refers to the request parameter map as you know it from the Servlet API. 非线性EL变量#{param}
引用了Servlet API中的请求参数映射。 The bean should of course already have both the param1
and param2
properties with the appropriate getters/setters definied. bean当然应该同时具有param1
和param2
属性以及相应的getter / setter。
If you'd like to execute some logic directly after they are been set, make use of the @PostConstruct
annotation: 如果您想在设置后直接执行某些逻辑,请使用@PostConstruct
注释:
@PostConstruct
public void init() {
doSomethingWith(param1, param2);
}
For more hints about passing parameters and that kind of stuff around in JSF, you may find this article useful. 有关在JSF中传递参数和类型的更多提示,您可能会发现本文很有用。
The JSF 2.x approach would be using either @ManagedProperty
in the backing bean class, or <f:viewParam>
in the target view. JSF 2.x方法将在支持bean类中使用@ManagedProperty
,或在目标视图中使用<f:viewParam>
。 See also this question: ViewParam vs @ManagedProperty(value = "#{param.id}") 另请参阅此问题: ViewParam与@ManagedProperty(value =“#{param.id}”)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.