Saturday, June 9, 2012

Simple JSF Project


In ySchool-mini week-3assignment I did a simple JSF (JAVA Server Faces) project.
In this project I use the same scenario that I used in Assignment-2.

Scenario 3: 
a)  Student marks can be entered entered in the following way
     Student Name (StudentName loaded in ComboBox), Grade (1-13 ComboBox), Subject, Marks
b)  User can enter these values and click "Add"
c)  Here all those fields are mandatory and marks cannot be more than 100
d)  System should validate those fields and show it in the response page

The full coding is uploaded  in github. https://github.com/Niroshan06/yschool_mini_nirosh_ass3

My project structure is look like this.

Project Structure
  • First I created a maven project using netbeans.
  • Then I added a JSF page name addMarks.
                   Right click on project----> New----> JSF page

JSF Page
  • In addMarks.xhtml following code is generate automatically.
                       <html xmlns="http://www.w3.org/1999/xhtml"
                                 xmlns:h="http://java.sun.com/jsf/html"
                                 xmlns:f="http://java.sun.com/jsf/core">

      The first is standard practice for XHTML pages. The second two directives allow the Facelets page to use the JSF Core and HTML tag libraries that are provided in the JSF specification, which in turn allows the Facelets page to use the underlying JSF UI components in the page.

  •  I added more codes in addMarks.xhtml to create the main page.
Main page
  • For the"Subject" text box I wrote the following code
                   <h:inputText label="Subject"
                           id="subject"
                           value="#{userBean.subject}"
                           required="true"/>
                  <h:message for="subject" />

      Here the required attribute is set to "true". So when we click Add button with empty Subject, it will give an error message for subject.
      In value attribute, it provides direct linkage to the subject property of the managed bean userBean. Basically, a managed bean is an officially registered Java class for a JSF application.

  • Then I created managed bean, name "userBean".
                  Right click on package---->New---->other---->JavaServer Faces---->JSF Managed Bean
Managed Bean
  • In userBean.java I added four variables String stuName, int grade, String subject and int marks. Then created getter and setter methods for this variables.
  • When I run the project......

Subject is empty.





No comments:

Post a Comment