Pages

Tuesday, May 29, 2012

Java Persistence API (JPA) Application

Java  Persistence API (JPA) provides POJO(Plan Old Java Object) standard and object relational mapping (ORM) for data persistence among applications.


JPA Architecture


Creating a simple JPA Application using MySql

Creating a database in MySql

  • Create a database StudentDetails in Mysql.
          create database StudentDetails;

Establishing a database connection to NetBeans

In NetBeans under the Services tab, Right click on the database and select New  Connection
  

Monday, May 21, 2012

Java Server Faces (JSF)


Java Server Faces (JSF) is a server side user interface component framework for Java technology based web applications.
JSF is a component oriented and event driven framework for web applications.

Create a simple JSF application in Maven

  • Create the maven web application via command prompt eg:- SimpleJSF  and open it in Net beans.

       The file layout of the project is some thing like this.


  • Insert the following dependencies to your pom.xml file to include the jsf2.0 libraries
       <dependency>
            <groupId>com.sun.faces</groupId>
            <artifactId>jsf-api</artifactId>
            <version>2.0.0-b13</version>
            <scope>compile</scope>
       </dependency>


      <dependency>
            <groupId>com.sun.faces</groupId>
            <artifactId>jsf-impl</artifactId>
            <version>2.0.0-b13</version>
            <scope>compile</scope>
       </dependency>

  • Create a new JSF file (index.xhtml) in the project. (New---> JSF Page)


  • In web.xml include the following tags for servlet mapping and set the welcome page.

       <context-param>

           <param-name>javax.faces.PROJECT_STAGE</param-name>

           <param-value>Development</param-value>

       </context-param>

  

       <servlet>
          <servlet-name>Faces Servlet</servlet-name>
          <servlet-class>javax.faces.webapp.FacesServlet
          </servlet-class>
          <load-on-startup>1</load-on-startup>
       </servlet>

      <servlet-mapping>
           <servlet-name>Faces Servlet</servlet-name>
           <url-pattern>*.xhtml</url-pattern>
      </servlet-mapping>
  
     <welcome-file-list>
          <welcome-file>index.xhtml </welcome-file>
      </welcome-file-list>


  • After that create a managedbean (New-->JSFMAnagedBean-->studentManagedBean)

      In JSF 2.0, Java bean that can be accessed from JSF page is called   
      Managed Bean.


  
   The managed bean can be a normal Java bean, which contains the getter        
  and setter methods,business logic or even a backing bean.

  • After that you create the following classes



    The Structure of the application is like this:



      Here Student.java is the Java bean class.



  • If you want to insert the prime faces in your application. You should insert the following dependency and repository
       <dependency>
            <groupId>org.primefaces</groupId>
            <artifactId>primefaces</artifactId>
            <version>3.2</version>
     </dependency>



    <repository>
            <id>prime-repo</id>
            <name>PrimeFaces Maven Repository</name>
            <url>http://repository.primefaces.org</url>
            <layout>default</layout>
    </repository>

Friday, May 4, 2012

MVC Architecture

MVC is an architectural design pattern. 
  • Model - business logic.
  • View - User Interface
  • Controller - handles user input
Why MVC ?
  • View is free of any business logic so looks simple and readable.
  • Business logic within the model can be reused for more than one views.
Simple example for MVC with JSP
  •  A web page for register the student details. 
  •  Here Student name, Parent name, Grade, and Address are input 
  • fields.
  •  User can enter these values by clicking "Insert"
  •  Here Student name and Grade are mandatory fields.
  •  System should validate this fields and show the proper error message.

For this example 
  • Create jsp pages for input interface, Error, Success response messages. - view
  • Create a java class (Student.java) for a student model - model
  • Create a servlet class for redirect a  proper view - controller  

Student Registration page.



When we click "Insert" button after enter all details. 


If we don't enter the Mandatory fields. This error message will display



Mapping a servlet page
Servlets need to be configured ans it needs to be mapped to a URI (URL/URN).
So we have to include the following mapping to the web.xml file.

<servlet>
    <servlet-name>ServletPage</servlet-name>
    <servlet-class>org.ymini.lukshica.controller.ServletPage</servlet-class>
 </servlet>
  
 <servlet-mapping>
    <servlet-name>ServletPage</servlet-name>
    <url-pattern>/Test</url-pattern>
 </servlet-mapping>


Mappings are used by the servlet container to translate a particular request URI to a particular servlet.