View parameters are stateful and they need to be initialized only once.
This is great because they are available in the current view over postbacks,
even if they no longer appear in the URL (they may appear only at initial
request). But, there are a few drawbacks of this behavior ‐ they are not bugs,
but as you will see, they can lead to performance problems:
·
Invoke the setter method at each postback
·
Break stateful character leads to problems with
the required built‐in validator
·
Invoke the converter regardless the null values
OmniFaces fix these issues via ViewParam
component (<o:viewParam>)
which is stateless. How this component works and it is implemented is presented
in Mastering
OmniFaces book.
Starting
with OmniFaces 2.2, this component support a new attribute named, default.
Via this attribute we can initialize (set a default value) a view parameter. Per example, let's check the
below example:
<f:metadata>
<f:viewParam name="phone"
value="#{phoneBean.phone}"/>
<o:viewParam name="prefix" default="001" value="#{phoneBean.prefix}"/>
</f:metadata>
<h:body>
#{phoneBean.prefix}-#{phoneBean.phone}
</h:body>
@Named
@RequestScoped
public class
PhoneBean {
private String prefix;
private String phone;
// getters and setters
}
We can see
the effect of the default attribute by "juggling" with the initial request query string:
- both view
parameters are initialized via query string (default attribute is ignored)
http://localhost:8080/ViewParamWithDefaultValue/?phone=825890&prefix=0721
Output: 0721-825890
- only phone view parameter is initialized via query string (default value is used since prefix
doesn't have a value):
http://localhost:8080/ViewParamWithDefaultValue/?phone=825890
Output: 001-825890
The complete
application is available here.
Niciun comentariu :
Trimiteți un comentariu