Parameter Passing / Request Parameters in JSF 2.0 (转)

This Blog is a compilation of various methods of passing Request Parameters in JSF (2.0 +)

(1)  f:viewParam

One of the features added in JSF 2.0 is "View Parameters"; Simply speaking it allows adding "Query string" or "Request Parameter" to the intended URL. One of the benefits is that it facilitates URL bookmarking.
In JSF 2.0 "View Parameters" is implemented by UIViewParameter. This class extends UIInput (jee6 api docs) and hence inherits features like Converter‘s, Validator‘s, full JSF lifecycle support, value binding and listeners.
"View Parameters" are defined in a Facelet page using <f:viewParam> tag. This tag is used inside metadata facet of a view <f:metadata>, which causes a UIViewParameter to be attached as metadata for the current view.

(source index.xhtml)

The above tag/config tells JSF engine to bind the value of "day" request parameter from the url (the url would be something like http://hostname/index.xhtml?day=Sunday) to the "day" property of "viewParamManagedBean". As mentioned above, "UIViewParameter" extends "UIInput", so it inherits all attributes like "required", when required="true", if the url does not include the required param, the page is not displayed.

This example attaches an component event listener (source page2.xhtml)

Its noteworthy that "setDay()" method of BackingBean is called in update model value phase, so its not available in @PostConstruct and so as a result to do initialization/preloading based on the set value, you need event listener as described above.

You can also include the view params in the links by simply using attribute includeViewParams="true"

You can also use "includeViewParams" in backing bean to include viewParams in the navigation link as follows.

(2) f:param

"View Params" is a good way of passing along parameters i.e. parameter that your page receives. However to send a parameter from a facelet to backing bean, or that matter to any other facelet you can use following approach:

and in the backing bean declare a @ManagedProperty and link its value to the request parameter by

To send a parameter to another JSF page

In the managed bean of page2, you can declare a managed property (as shown in example above) and capture value of passed in parameter.

(3) Method Expression

You can send parameter as an argument to an action method i.e. bean.actionMethod(Type param1, Type param2)
 Here is a sample JSF code with datatable displaying day‘s of a week, and a select button in each row. The action method is passed an argument, which is the declared variable of the datatable:

Backing Bean:

When you include a f:param tag in “h:commandButton” or "h:commandLink", the parameter is turned into request parameter,

which in the backing bean can be retrieved as

(4) f:Attribute

Parameter/Values can be passed to an actionListener method (of a commandUI) using f:attribute
In JSF page

In the Backing Bean capture the value using

One Could also use f:Attribute to pass an attribute to an component. for e.g.

1 <h:outputLabel for="sportsPer" value="Do You Play Tennis:" />
2   <h:selectOneRadio id="sportsPer" value="#{someBackingBeanView.likesTennis}" validator="#{someBackingBeanView.validatePlayer}">
3     <f:selectItem itemLabel="No" itemValue="N" />
4     <f:selectItem itemLabel="Yes" itemValue="Y" />
5     <f:attribute name="clubNameAttr" value="#{clubName.submittedValue}" />
6 </h:selectOneRadio>
7 Where "clubNameAttr" is the bind id of a form field whose value is required in the validator method (multi/complex form field validator).
8 <h:inputText id="clubNameId" binding="clubName" value=" #{someBackingBeanView.clubName}"/>

Now in the validator method, one can get the value of attribute "clubNameAttr"

1 public void validatePlayer(FacesContext context, UIComponent component, Object value) throws ValidatorException {
2 String clubNameAttr = (String)component.getAttributes().get("clubNameAttr");
3 //do something ...
4 }

(5) f:setPropertyActionListener

Parameter/Values can also be passed to an actionListener method (of a commandUI) using f:setPropertyActionListener
In JSF page

In the Backing Bean

One thing to note is that the setDay() is called after the listener method (listenerToGetAttributes(..)) completes. So if the scope of "DayOfWeekManagedBean" is higher than Request, then you will see that the value of "day" in listener method is of the previously clicked button and NOT the current (because listeners are invoked in the sequence they are defined).

(6) JSTL tag <c:set>

There is no equivalent tag for <c:set>in JSF, f:param is NOT the same. This one comes in handy when f:param is not available in backing bean; for example when passing a parameter to <ui:include> tag. If you add <f:param> for example <f:param  name="activeIndex" value="0"/>, the value will be available in the included JSF page as #{activeIndex}, but NOT in the constructor of the backing bean of the included page. In such scenario, one can use <c:set>

In the Backing Bean capture the value using

原文链接:http://incepttechnologies.blogspot.com/p/view-parameters-in-jsf-20.html

时间: 2024-10-18 10:53:22

Parameter Passing / Request Parameters in JSF 2.0 (转)的相关文章

More than the maximum number of request parameters

前些时间,我们的的一个管理系统出现了点问题,原本运行的好好的功能,业务方突然讲不行了,那个应用已经运行了好多年了,并且对应的代码最近谁也没改动过,好奇怪的问题,为了解决此问题,我们查看了日志,发现请求的参数压根没有传到后台控制器之中,奇怪了!明明前端传递了好多参数过来了呀!线上系统比较重要,又需要马上使用必须赶紧解决呀! 提交请求时的页面反应,如下所示: 于是我们想在本地模拟同样的操作,可气的是本地压根没任何问题,好头疼,对于这样的问题确实是最令人头疼的,同样的代码一边运行不了,一边屁事没有,后

Jersey(1.19.1) - Extracting Request Parameters

Parameters of a resource method may be annotated with parameter-based annotations to extract information from a request. A previous example presented the use @PathParam to extract a path parameter from the path component of the request URL that match

Unity: Passing Constructor Parameters to Resolve

ebruary 11, 2012 by mikael koskinen 2 comments In this tutorial we will go through of couple different ways of using custom constructor parameters when resolving an instance with Unity: By using the built-in ParameterOverride By creating a custom Res

JNI: Passing multiple parameters in the function signature for GetMethodID

http://stackoverflow.com/questions/7940484/jni-passing-multiple-parameters-in-the-function-signature-for-getmethodid ASK : I am trying to execute a function in Java (from C) that has the following signature: public void execute(int x, int y, int acti

更改Request Parameters中的值

1. 定义ParameterRequestWrapper 继承HttpServletRequestWrapper public class ParameterRequestWrapper extends HttpServletRequestWrapper { private Map<String,String[]> params; public ParameterRequestWrapper(HttpServletRequest request, Map<String,String[]&

SqueezeNet:AlexNet-level Accuracy with 50x fewer parameters and less than 0.5Mb model size

- Fire modules consisting of a 'squeeze' layer with 1*1 filters feeding an 'expand' layer with 1*1 and 3*3 filters(通過餵食一個包含1*1和3*3濾波器的'擴展'層,激勵包含一個'擠壓'層的模塊) - AlexNet level accuracy on ImageNet with 50x fewer parameters(具有AlexNet水平的精度,卻少了50倍的參數量) - Ca

C# - Passing Reference-Type Parameters

A variable of a reference type does not contain its data directly; it contains a reference to its data. When you pass a reference-type parameter by value, it is possible to change the data pointed to by the reference, such as the value of a class mem

simpleBLEPeripheral.c 文件分析

这个配置或者说任务, 让这个蓝牙设备成为了一个简单的BLE外设. 这里定义了外设的广播数据, 以及最重要, char被改变之后的回调, 引出后来的coreHandler里面的, ack 以及写e2prom. /************************************************************************************************** **********************************************

The OAuth 2.0 Authorization Framework-摘自https://tools.ietf.org/html/rfc6749

Internet Engineering Task Force (IETF) D. Hardt, Ed. Request for Comments: 6749 Microsoft Obsoletes: 5849 October 2012 Category: Standards Track ISSN: 2070-1721 The OAuth 2.0 Authorization Framework Abstract The OAuth 2.0 authorization framework enab