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