Archive for the ‘JavaServer Faces (JSF)’ Category
JSF: Handling Dates in JSF Forms
Last Updated on Thursday, 19 May 2016 Written by Chad Darby Tuesday, 3 May 2016
In this article, I show you code examples on how to handle dates in JSF forms.
The project consists of three files:
The user enters the student data into the form: first name, last name and hire date. The input format for the hire date is day, month, year. As an example For 1 May 2016, you would enter: 1-5-2016.
Once the form is submitted, then the app will show the output.
Here’s the code. Enjoy!
FAQ: What is the difference between JSF and JSP?
Last Updated on Sunday, 31 January 2016 Written by Chad Darby Thursday, 28 January 2016
In a nutshell, JSP is suitable for small web projects and JSF is best for large scale enterprise projects.
JSP is an older technology … but it is still viable for smaller web apps. I like to use it for training since it is very similar to PHP. Most Java newbies can pick it up with no problem because it is simple and straight-forward.
Think of JSP as your low-end, economy car. It will get you to your destination but no special bells and whistles. It is ike flying “economy/coach” class.
If you are building a modern, large-scale, enterprise app then I’d recommend JSF. The framework provides data binding, validation, bean support etc.
Think of JSF as your luxury vehicle. It will get you to your destination in style … however with additional up-front luxury costs. It is like flying “first class”.
Hopefully this helps.
JSF: Setting itemLabel with selectItems
Last Updated on Wednesday, 22 June 2016 Written by Chad Darby Tuesday, 13 October 2015
JSF provides UI components for drop-down lists. Drop-down lists are called selectOneMenu in JSF.
When you use the components, you may have the requirement to use different values for the itemLabel and itemValue. You can accomplish this by making use of a Map collection (java.util.Map). The Map collection is a collection of key-value pairs.
In JSF, you provide a reference to a Map and then JSF will build the list. It will use the Map key for the itemLabel and the Map value for the itemValue.
When the user selects an item, then the selected item’s value will be set on the bean.
Ok, enough talking, let’s look at an example.
In this example, I will present the user with a JSF form. The form will read the user’s first name, last name and country.
JSF Bean Code
The country drop down list is populated with a Map from the bean. The bean will define a Map collection that uses the Map key for the itemLabel and the Map value for the itemValue. Here’s the code snippet
Now the JSF page can make use of this bean to get the itemLabel and itemValues. Remember JSF will use the map key as the itemLabel and the map value as the itemValue
Here’s the snippet of code for the JSF page.
So when the form is displayed, the user will set the map keys in for the item label. For example, the user will see the full name of the country as shown below:
Then once they submit the form, JSF will store the value of the selected item into the bean.
Here’s a snippet of code from the response form that shows the user’s selected value:
So if the user selects “Germany”, then JSF will store the value of “DEU” for the bean property studentTwo.country.
Well, that covers how you can provide different itemLabels and itemValues for a drop-down list.
Do You Need More Details?
- Do you need to connect to a database?
- Display SQL results in a HTML table?
- Perform SQL insert, updates and deletes?
- Database Integration with JDBC
- Displaying Data in Lists and Tables
- Validating HTML Forms
- Implementing Business Logic with Managed Beans
- Deploying applications with WAR Files
Follow luv2code with the links below: