background preloader

JSF

Facebook Twitter

Dynamic JSF

Primefaces. Jakob Korherr's Blog » JSF 2.0: View parameters. One very cool new feature of JSF 2.0 are view parameters.

Jakob Korherr's Blog » JSF 2.0: View parameters

If you are now wondering what I mean with a view parameter (or request parameter) – here’s the answer: Do you know those things at the end of an URL that often look like ? Id=3&page=2, then you know what I mean. If not, take a look at this. The great advantage of these view parameters is that JSF’s URLs are now bookmarkable, because all values that are needed to display a certain page can be provided via the URL. JSF 2.0 introduces a new UIInput component especially for view parameters called UIViewParameter. To define an UIViewParameter in your view you just have to add a <f:viewParam /> element inside your view metadata facet.

If you now access this page with #{myBean.input} will be set to “abc”. Very important for this feature to work is to use the JSF 2.0 built-in Facelets-2 view declaration language and not JSP or Facelets-1.x. On my opinion, view parameters are a really cool new feature of JSF. S Blog: Easier Multi-Field Validation with JSF 2.0. One of the most frequent needs when developing application forms is multi-field validation (or cross-field, but I’m not using this term because when I put it on Google I actually got some post-war pictures).

s Blog: Easier Multi-Field Validation with JSF 2.0

I’m talking about situations where we need to compare whether an initial date is earlier than an end date or a value is lower than another one. Isn’t it an obvious feature in every business-oriented framework? Not really. Unfortunately, the JSF specification doesn’t support it by default. Therefore, until its latest production release (JSR 245 – JSF 2.1), JSF did not offer an out-of-the-box multi-field validation feature. We probably can hope for something coming in JSF 2.2, since the JSR 344 mentions “Multi-field validation”. I didn’t like any solution I found. In the following example, I check whether an allocated budget is smaller than a budget limit. Step 1: create an attribute in the managed bean for each field to be validated: That’s it! Merry Christmas and Happy New Year! Validator for multiple fields. Notice The JSF utility library OmniFaces has several useful multi-field validators such as <o:validateEqual>, which may end up to be easier than homegrowing one.

Validator for multiple fields

See also the javadoc of the base class ValidateMultipleFields. Introduction Validators in JSF are nice. They, however, have its shortcomings. The cleanest solution would be to create a custom component which renders two or more components and use a specific validator for that, but that would involve more work. The benefits and pitfalls of @ViewScoped. Introduction To prepare for a new set of JSF 2.0 targeted articles (have patience, I'd like to wait for Eclipse Helios and Tomcat 7 to be finished), I've played intensively with JSF 2.0 and Facelets the last weeks (to be precise, with Mojarra 2.0.2).

The benefits and pitfalls of @ViewScoped

The new JSF 2.0 annotations and implicit navigation (the outcome will implicitly go to /outcomevalue.xhtml page) are great and very useful. No hassling with faces-config.xml anymore. It's awesome. JSF 2.0 also introduces an important new scope and offers the possibility to define your own custom scopes. Package com.example; import java.io.Serializable; import javax.faces.bean.ManagedBean; import javax.faces.bean.ViewScoped; @ManagedBean@ViewScopedpublic class Bean implements Serializable { // ... } Note that the bean needs to implement Serializable as it will be stored in the view map which is in turn stored in the session. The new view scope should solve exactly those issues.

Back to top Really simple CRUD Amazingly simple, isn't it?