如何将url参数传递给JSF

如何将url参数传递给JSF

本文介绍了如何将url参数传递给JSF?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我还没有找到通过URL参数将参数传递给JSF页面的方法.

I haven't managed to find a way to pass parameters to JSF pages through URL parameters.

http://www.example.com/jsfApp.jsp?param1=value1&param2=value2

有人可以为此指向我正确的方向吗?

Could someone point me at the right direction with this?

推荐答案

在使用JSP时,我假设您正在使用JSF1.x.

As you're using JSPs, I'll assume that you're using JSF 1.x.

要使用查询参数创建链接,请在f:paramf:param之间使用h:outputLink:

To create a link with query parameters, use h:outputLink with f:param:

<h:outputLink value="page.jsf">
    <f:param name="param1" value="value1" />
    <f:param name="param2" value="value2" />
</h:outputLink>

value可以在EL的帮助下动态设置.

The value can be set dynamically with help of EL.

要自动在托管bean中设置它们,您需要在faces-config.xml中将它们分别定义为managed-property:

To set them in the managed bean automagically, you need to define each as managed-property in faces-config.xml:

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

隐式EL变量#{param}引用了您从Servlet API中所知道的请求参数映射.该Bean当然应该已经具有param1param2属性,并定义了适当的getters/setters.

The imlicit EL variable #{param} refers to the request parameter map as you know it from the Servlet API. The bean should of course already have both the param1 and param2 properties with the appropriate getters/setters definied.

如果要在设置逻辑后立即执行某些逻辑,请使用@PostConstruct批注:

If you'd like to execute some logic directly after they are been set, make use of the @PostConstruct annotation:

@PostConstruct
public void init() {
    doSomethingWith(param1, param2);
}

有关在JSF中传递参数和类似内容的更多提示,您可以找到这篇文章很有用.

For more hints about passing parameters and that kind of stuff around in JSF, you may find this article useful.

JSF 2.x方法将在后备bean类中使用@ManagedProperty,或者在目标视图中使用<f:viewParam>.另请参阅以下问题: ViewParam与@ManagedProperty(value =#{param.id}"; )

The JSF 2.x approach would be using either @ManagedProperty in the backing bean class, or <f:viewParam> in the target view. See also this question: ViewParam vs @ManagedProperty(value = "#{param.id}")

这篇关于如何将url参数传递给JSF?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-06 06:19