About this project:
This project contains the POC related to JSF stuff, so a person can get started with ease.
Deploy: This is a maven project so "mvn clean install" will create a war file which can be deployed in tomcat 7. It was observed that Glassfish-2.1.1 does not support JSF-2.0
How to deploy a webapp in tomcat using maven?
- Tomcat Authentication First, add an user with administrator access right for Tomcat. To add Tomcat user, edit this file – "%TOMCAT_PATH%/conf/tomcat-users.xml".
File : tomcat-users.xml
Later, you will use the user "admin" to do the deployment.- Maven Authentication In Maven side, you need to add the same user authentication information in "%MAVEN_PATH%/conf/settings.xml".
File : settings.xml
//... TomcatServer admin password //... 3. Maven-Tomcat-Plugin Declare "Maven-Tomcat plugin" and related Tomcat server detail in your pom.xml file.
org.codehaus.mojo tomcat-maven-plugin http://127.0.0.1:8080/manager TomcatServer /mkyongWebApp See a full pom.xml file.
4.0.0
com.mkyong
mkyongweb-core
war
1.0
mkyongweb-core Maven Webapp
http://maven.apache.org
org.codehaus.mojo
tomcat-maven-plugin
http://127.0.0.1:8080/manager
TomcatServer
/mkyongWebApp
maven-compiler-plugin
- Deploy to Tomcat Issue "mvn tomcat:deploy" to package your project in a WAR file, and deploy it to Tomcat server. To verify it, just access to the Tomcat’s manager page and make sure "/mkyongWebApp" path is existed.
URL : http://127.0.0.1:8080/manager/
Configure Managed Bean with XML
With XML configuration, you can use the old JSF 1.x mechanism to define the managed bean in a normal faces-config.xml file. Best Practice It’s recommended to put the managed beans in a separate XML file because the faces-config.xml is used to set the application level configurations.
So, you should create a new XML file and put the managed beans detail inside, and declared the XML file in the javax.faces.CONFIG_FILES initialize parameter, which is inside the WEB-INF/web.xml file.
web.xml
... javax.faces.CONFIG_FILES WEB-INF/manage-beans.xml ...
URL to look out for:
-
Hello World : http://localhost:8080/JavaServerFaces/hello.jsf
-
Hello World AJAX : http://localhost:8080/JavaServerFaces/helloAjax.jsf
-
JSF 2.0 redirect and view URL bar : http://localhost:8080/JavaServerFaces/page1.jsf
-
Conditional Navigation Flow : http://localhost:8080/JavaServerFaces/start.jsf (also see PaymentController and faces-config.xml)
-
Form-Action Navigation Flow : http://localhost:8080/JavaServerFaces/startFormActionFlow.jsf (also see PageController and faces-config.xml)
-
JSF 2.0 and message-bundle properties : http://localhost:8080/JavaServerFaces/helloMessageBundle.jsf
-
JSF 2.0 and message-I18n properties Internationalization : http://localhost:8080/JavaServerFaces/startI18n.jsf
-
JSF 2.0 and input Text control demo : http://localhost:8080/JavaServerFaces/ui-controls/textboxControl.xhtml
-
JSF 2.0 and input Secret control demo : http://localhost:8080/JavaServerFaces/ui-controls/textboxControl.xhtml
-
JSF 2.0 and input Textarea control demo : http://localhost:8080/JavaServerFaces/ui-controls/textboxControl.xhtml
-
JSF 2.0 and input Checkbox control demo : http://localhost:8080/JavaServerFaces/ui-controls/checkboxControl.xhtml
-
JSF 2.0 and input Radiobutton control demo : http://localhost:8080/JavaServerFaces/ui-controls/radioButtonControl.xhtml
-
JSF 2.0 and input Listbox control demo : http://localhost:8080/JavaServerFaces/ui-controls/listboxControl.xhtml
-
JSF 2.0 and input Multiselect Listbox control demo : http://localhost:8080/JavaServerFaces/ui-controls/multiselectListboxControl.jsf
-
JSF 2.0 and input SelectOneMenu DropDown Control : http://localhost:8080/JavaServerFaces/ui-controls/selectOneMenuControl.jsf
-
JSF 2.0 and OutputText Tag : http://localhost:8080/JavaServerFaces/ui-controls/outputText/outputText.xhtml
-
JSF 2.0 and OutputFormat Tag (Display parameterized text) : http://localhost:8080/JavaServerFaces/ui-controls/outputFormat/outputFormat.xhtml
-
JSF 2.0 and Graphic Image Tag : http://localhost:8080/JavaServerFaces/ui-controls/graphicImage/graphicImageControl.xhtml
-
JSF 2.0 and Output Style Sheet : http://localhost:8080/JavaServerFaces/ui-controls/outputStyleSheet/outputStyleSheetControl.xhtml
-
JSF 2.0 and Output Script : http://localhost:8080/JavaServerFaces/ui-controls/jsRes/outputScript.xhtml
-
JSF 2.0 link, commandLink and outputLink example : http://localhost:8080/JavaServerFaces/ui-controls/link/login.xhtml
-
JSF 2.0 panelGrid example, In JSF , "h:panelGrid" tag is used to generate HTML table tags to place JSF components in rows and columns layout, from left to right, top to bottom (To test enter Alphabets not number) : http://localhost:8080/JavaServerFaces/ui-controls/panelGrid/panelGrid.jsf
-
JSF 2.0 message and messages example, To view this output, enter invalid inputs like, less-than 5 letter string, and age above 200 : http://localhost:8080/JavaServerFaces/ui-controls/message/message.jsf
-
JSF 2.0 In JSF, "f:param" tag allow you to pass a parameter to a component, but it's behavior is different depends on which type of component it's attached : http://localhost:8080/JavaServerFaces/ui-controls/param/param.xhtml
-
JSF 2.0 attribute example, In JSF, "f:attribute" tag allow you to pass a attribute value to a component, or a parameter to a component via action listener. : http://localhost:8080/JavaServerFaces/ui-controls/attribute/attribute.xhtml
-
JSF 2.0 setPropertyActionListener example, In JSF, "f:setPropertyActionListener" tag is allow you to set a value directly into the property of your backing bean : http://localhost:8080/JavaServerFaces/ui-controls/setPropertyActionListener/setPropertyActionListener.xhtml
-
JSF 2.0 and dataTable sample : http://localhost:8080/JavaServerFaces/table/displayTable.jsf
-
JSF 2.0 and add new order sample : http://localhost:8080/JavaServerFaces/table/addOrder.jsf
-
JSF 2.0 and delete order using AJAX and table refresh : http://localhost:8080/JavaServerFaces/table/displayTable.jsf (Click on the delete link (present against each order) after adding a new order)
-
JSF 2.0 and edit using AJAX and complete flow for CRUD on orders : http://localhost:8080/JavaServerFaces/table/displayTable.jsf
-
JSF 2.0 and templating : i. http://localhost:8080/JavaServerFaces/templating/viewTemplatedPage.jsf ii. http://localhost:8080/JavaServerFaces/templating/page1.jsf (With custom data)
-
JSF 2.0 and convertnumber "f:convertNumber" is a standard converter, which converts String into a specified "Number" format. In addition, it's also used as a validator to make sure the input value is a valid number : http://localhost:8080/JavaServerFaces/numberConverter/numberConverter.jsf
-
JSF 2.0 and convertDateTime, coverts String to formatted dateTime : http://localhost:8080/JavaServerFaces/dateTimeConverter/dateTimeConverter.jsf
-
JSF 2.0 and validateLength : http://localhost:8080/JavaServerFaces/validateLength/validateLength.xhtml
-
JSF 2.0 validateLongRange example used to set the min and max range allowed : http://localhost:8080/JavaServerFaces/validateLongRange/validateLongRange.jsf
-
JSF 2.0 validateDoubleRange example, used to set min and max floating range allowed : http://localhost:8080/JavaServerFaces/validateDoubleRange/validateDoubleRange.jsf
-
JSF 2.0 validateRequired example, validator for checking empty fields : http://localhost:8080/JavaServerFaces/validateRequired/validateRequired.jsf
-
JSF 2.0 validateRegex example, validator for check inputs as per the regex set for that field (The above regex pattern is required 6 to 20 characters string with at least one digit, one upper case letter, one lower case letter and one special symbol ("@#$%"). ) : http://localhost:8080/JavaServerFaces/validateRegex/validateRegex.jsf
-
JSF 2.0 and custom validator messages : http://localhost:8080/JavaServerFaces/customValidatorMessage/customValidatorMessage.jsf
The standard JSF conversion and validation error messages are too detail, technical or sometime, not really human readable. In this article, it shows you how to customize standard conversion or validation error message in JSF 2.0. Summary Guide i. Find your message key from jsf-api-2.x.jar, "Messages.properties" file. ii. Create your own properties file, and put the same message key you found in above "Messages.properties" file, and override it with your custom error message. iii. Register your properties file in "faces-config.xml", put it as application level. iv. Done. -
Custom converter in JSF 2.0 : http://localhost:8080/JavaServerFaces/customConverter/customConverter.jsf
Steps i. Create a converter class by implementing javax.faces.convert.Converter interface. ii. Override both getAsObject() and getAsString() methods. iii. Assign an unique converter ID with @FacesConverter annotation. iv. Link your custom converter class to JSF component via f:converter tag. -
Custom validator in JSF 2.0 : http://localhost:8080/JavaServerFaces/customValidator/customValidator.jsf
Steps
i. Create a validator class by implements javax.faces.validator.Validator interface. ii. Override validate() method. iii. Assign an unique validator ID via @FacesValidator annotation. iv. Reference custom validator class to JSF component via f:validator tag.
-
Multiple Components Validator in JSF 2.0 :
Two ways :
i. Register PostValidateEvent, puts validation inside : http://localhost:8080/JavaServerFaces/multipleFieldsValidation_PostValidate/default.xhtml
The javax.faces.event.PostValidateEvent is a system event, that fire after all components are validated. The idea is register a PostValidateEvent, and attach to a validation method. see following :
<f:event listener="#{bean.methodToValidateMultipleFields}" type="postValidate" />
In PostValidateEvent, the "listener" method must have this signature public void method-name(ComponentSystemEvent event)". The rest of the code should be self-explanatory.
ii. Create a standard validator and get other components via f:attribute : http://localhost:8080/JavaServerFaces/multipleFieldValidator_Attribute/default.xhtml
-
JSF 2.0 and actionListener : In JSF, "Action Events" are fired by clicking on a button or link component, e.g h:commandButton or h:commandLink.
Ways i. Method binding : http://localhost:8080/JavaServerFaces/actionListener/actionListener.xhtml In button or link component, you can specified a bean's method directly in the "actionListener" attribute.
ii. ActionListener : http://localhost:8080/JavaServerFaces/actionListener/actionListenerImpl.xhtml In button or link component, add a "f:actionListener" tag inside, and specified an implementation class of ActionListener interface, and override its processAction().
-
JSF 2.0 PostConstructApplicationEvent and PreDestroyApplicationEvent example : Since JSF 2.0, you can register javax.faces.event.PostConstructApplicationEvent and javax.faces.event.PreDestroyApplicationEvent system event to manipulate the JSF application life cycle.
i. PostConstructApplicationEvent - Perform a custom post-configuration after application has started. ii. PreDestroyApplicationEvent - Perform a custom cleanup task before application is about to be shut down.
Class to lookout for : FacesAppListener in current codebase, and to check its functionality, start server and check server startup logs with application deployed.
-
JSF 2.0 PreRenderViewEvent example : http://localhost:8080/JavaServerFaces/preRenderView/default.xhtml In JSF 2.0, you can attach a javax.faces.event.PreRenderViewEvent system event to perform custom task before a view root (JSF page) is display. Create a normal bean, contains a method signature "public void method-name(ComponentSystemEvent event)", later you will ask listener to call this method.
-
JSF 2.0 valueChangeListener example :
Ways i. Method binding – In input component, specified a bean's method directly in the "valueChangeListener" attribute. http://localhost:8080/JavaServerFaces/valueChangeListenerDemo/valueChangeListenerDemo.xhtml
ii. ValueChangeListener interface – In input component, add a "f:valueChangeListener" tag inside, and specified an implementation class of ValueChangeListener interface. http://localhost:8080/JavaServerFaces/valueChangeListenerDemo/valueChangeListenerImpl.xhtml