jAstrologer JSF Tutorial - Validaiton Pt. 1

OK, so we've got our basic jAstrologer web application that we've built in the last two exercises. You can download the project here if you haven't run through the last two exercises. Make sure you undeploy any other applications with the /jAstrologer context root that you may have already deployed to the server. So far the web application does the following:

  • Presents greeting.jsp, which uses two inputText components to ask for the user's name and birthday.
  • Saves the values entered by the user in the properties of the UserBean backing bean.
  • Reads the values of the properties from UserBean and displays them in success.jsp.

But was it really a success? In the present state, we don't know. We just take any input (or no input at all) and call it a success. That's why we need to build in a little validation to our web application. We'll start off today with some really basic stuff: making a field required and using a converter to convert the Birthday field from a String to a nice Date object.

Making a Field Required

The first thing we need to do is make sure that the user enters something for the name field. We can easily do this by using the required attribute of the inputText component.

  1. Open greeting.jsp and change the name inputText component as follows:
    	<p>Enter your name: <h:inputText value="#{UserBean.name}" 
    	        id="name" required="true"/>
    	<h:message for="name" /></p>

    What we have done is to give an ID to the name text field, so we can then specify for which component the message shows. We have then specified that the field is required, so the web application will show the error message if the user does not enter anything.

  2. Run the project and click Submit without entering a name. You get the following error:

    no name error

Using a Converter

Now we need to start treating our birthday field as a date and not just a random string. The JSF framework provides a number for converters that you can use to convert text input into object types, like booleans and so forth. In the process of converting the data, it also checks that the data is valid for the type it's converting it into. This is especially handy for our birthday input field because we can specify the date format, validate the input, and get a nice Date object all at once.

  1. Open greeting.jsp and change the birthday inputText component as follows:
            <p>Enter your birthday: <h:inputText value="#{UserBean.birthday}" 
            		id="birthday" required="true">
                <f:convertDateTime pattern="dd/MM/yyyy" />
            </h:inputText> (dd/mm/yyyy)</p>
            <h:message for="birthday" />

    What we have done is to give an id to the birthday text field, so we can then specify for which component the message shows. We have then set the converter to the pattern dd/MM/yyyy. Anything the user enters that does not match this format will cause the web application to redisplay greeting.jsp with an error message. We have also specified that the field is required, like we did with the name field.

  2. Now we need to change the type of the birthday property in UserBean.java to a Date object. Open UserBean.java and make the following changes in bold:
        private String name;
        private Date birthday;
        ...
        
        public Date getBirthday() {
            return birthday;
        }    
    
        public void setBirthday(Date birthday) {
            this.birthday = birthday;
        }    
  3. Run the project. When you try to click Submit without entering a date, you get the following error message:

    If you enter an invalid date, you get the following error:

Comments:

Post a Comment:
Comments are closed for this entry.
About

johnc

Search

Archives
« April 2014
SunMonTueWedThuFriSat
  
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
   
       
Today