Skip to main content

Posts

Showing posts from October, 2017

5. JSF Validating Inputs part 2/2

0. Introduction I am an old poor beginner and I am very pleased with the help of Java gurus like Marty Hall and his coreservlet page . So while learning his invaluable lessons, I try to give an example. I will cover 3 aspects: Validating by means of Action Controller of a managed bean  Using custom validator method with validator attribute  Validation through Bean Validation JSR 380 1. Validation using a managed bean action controller In order to display messages, these steps may be needed: A xhtml tag <h:message for="control_to_validate_id"> But it is preferred a <h:messages ..> to put messages at the top of the form Add messages to display by means of java code: FacesMessage errorMessage = new FacesMessage("This an error .."); errorMessage.setSeverity(FacesMessages.SERVERITY_ERROR);  FacesContext.getCurrentInstance().addMessage( null , errorMessage); The severity can be: SEVERETY_ERROR (for errors) SEVERETY_INFO (info

4. JSF: Validating Inputs. Part 1/2

0. Introduction As Marty Hall indicates, there are several resources for validating inputs: 1. required="true" requiredMessage="…" for fields that should be filled. 2. converterMessage="..." for no-string fields that require a conversion. 3. f:validateBlah tags, validatorMessage  for values in a range or that matches a regular expression. 4. h:message, h:messages for values displayin messages. 5. Validating manually by means of the action controller. 6. h:input Blah validator="#{somebean.somemethod}" using a custom validator. 7. Using Apache MyFaces for URL and other stuff validation.( problematic!!! ) 1. A simple form with different validations Here is a simple form using the first 6 validators. I ts name is:    test-05-validation-page.xhtml 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56

3. Using Languages with resource bundles in JSF

Main steps 1. Create a folder in the src/main/java . In my case " bundles ". But as I am using Maven, it is preferred to use src/main/resources . 2. Create a file with ".properties" extension  in the created folder. In my case " labels.properties " 1 2 3 4 login = LOGIN DEMO message = message: password = password: user = user: 3. Create additional files referring to the locales. For instance " labels_es.properties " for Spanish locales. 1 2 3 4 login = DEMO DE ACCESO AL SISTEMA message = mensaje: password = contraseƱa : user = usuario: 4. There are basically 3 ways to inform a xhtml file how to access the resource bundles. The first one is using the file faces-config.xml , the second one is using <f:loadbundle ..> in the xml file and the third one is accessing the resource bundle by means of a request scoped  bean referenced in the xhtml file . 4.1 (Using faces-context.xml)   (See  Veera

2. Create a Maven JSF project and a simple login page

Updated on Oct-25-2107 0. Introduction Remember to install previously: Java JDK 8 Eclipse Oxygen Lombok  Apache Tomcat 9 1. Create a Maven Project In the top menu select : File - New - Maven Project Press Next Now fill the next form as follows It is important to select: Group id: org.ximodante.jsf  (or another packet you like) Packaging: war Artifact Id and Name: JSFv02 or any name for the project Press Finish 2. The pom.xml file Eclipse complains about the simple generated file. It seems that a web.xml is required if the packaging is selected to war. The simple generated file is  1 2 3 4 5 6 7 8 9 <project xmlns= "http://maven.apache.org/POM/4.0.0" xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation= "http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" > <modelVersion> 4.0.0 </modelVersion> <groupId> org.ximodante