.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Articles >> General >> Post New Resource Bookmark and Share   

 Subscribe to Articles

Introduction to java server faces

Posted By:satyapriyanayak       Posted Date: February 03, 2015    Points: 200    Category: General    URL: http://www.dotnetspark.com  

In this article we will know about java server faces.
 

Introduction to JSF (Java server faces) framework

Before going forward we should know about Model-1 and Model-2 Architecture.


Model-1 Architecture

In this Architecture all kinds of requirements get fulfilled in one layer. The user provides request to the jsp and the jsp provides response to the user. This Architecture uses a set of jsp to implement presentation logic, business logic, controlling logic. It is suitable for small-scale requirement due to its simplicity. It does not provide reusability.

Model-2 Architecture

This layer is divided into three parts they are as below

1.Model
2.Controller.
3.View.

Model: - This layer implements business logic and contains data. State of users can be stored in this layer. This layer can be created by using a set of java beans.

Controller: - This layer implements controlling logic. It takes decision to make visible appropriate view to the appropriate users. This layer can be created by using a servlet and a set of helper classes. The users gives request to the servlet present in the layer.

View: - This layer implements presentation logic of the application. It provides response to the user. A set of Jsp can be used to create this layer.

Introduction to Java server faces

Model: - Jsp implement business logic with the help of some java beans called as managed bean. The manage bean contains states of the application through the properties and validates users input to create appropriate navigation case for the controlling layer.

Controller: - This layer contains only one servlet to accept request from users provides a predefined servlet known as faces servlet.

View: - This layer can be created in jsf by using a set of jsp or x.html files. Jsf provides a set of tags to support MVC based presentation.

Installation of jsf

1.Get the copy of Jsf binary distribution file (Mojarra-2.0.2-fcs-binary.zip) and extract into any folder of the file system. Get the file from the below link
http://www.4shared.com/file/fU8yFzet/mojarra-202-FCS-binary.html

2.Find jsf-api.jar and jsf-impl.jar inside 'lib' folder of the extracted folder and make those two files available in 'lib' folder of the context.

Configuration of jsf

1.Create a configuration file named as faces-config.xml in WEB-INF folder. This file contains information about all managed bean and navigation cases.

Tips
Search for faces-config.xml inside extracted folder of Jsf.Copy any of this file and make it available inside WEB-INF folder of the context.Open this  file and remove all the tags present in between <Faces-config> and </Faces-config>

2.Provide name of the configuration file to the context by using <context-param> in web.xml file. Deploy the controlling servlet by using the required tag in web.xml.The controlling servlet must be a permanent servlet.

Web.xml file
<!--
  Licensed to the Apache Software Foundation (ASF) under one or more
  contributor license agreements.  See the NOTICE file distributed with
  this work for additional information regarding copyright ownership.
  The ASF licenses this file to You under the Apache License, Version 2.0
  (the "License"); you may not use this file except in compliance with
  the License.  You may obtain a copy of the License at
      http://www.apache.org/licenses/LICENSE-2.0
  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
-->
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    version="2.5">
<context-param>
<param-name>javax.faces.CONFIG_FILES</param-name>
<param-value>/WEB-INF/faces-config.xml</param-value>
</context-param>    
    <servlet>
        <servlet-name>x</servlet-name>    
    <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
        <load-on-startup> 1 </load-on-startup>
    </servlet>    
    <servlet-mapping>
        <servlet-name>x</servlet-name>
        <url-pattern>*.jsf</url-pattern>
    </servlet-mapping>
</web-app>

Tag library of jsf

1.Core tag library: - This provides facility to render or convert jsf tags to html tags.
Uri:http://java.sun.com/jsf/core

2.Html tag library: - This provides facility to create MVC based html tags.
Uri:http://java.sun.com/jsf/html

Creating jsf based jsp

1.Create a file by using .jsp extension.
2.Include the core and html tag libraries by using their uri into the tag lib directive.
3.Use the view tag of core tag library as the parent tag of all jsf tags.
4.Use the required jsf tags to make the content visible to the user.
5.Access the jsp by using extension of the controlling servlet (x.jsf).

Creation of a context file

Create any folder in any drive as (E:\jsf). Inside that folder store your .jsp files. Give the Context path name as javajsf and docBase as E:\jsf, here docBase means the total path where we are storing our .jsp files. Store the java bean file inside 'classes' folder of the context for predefined context (root) the classes folder required to be created inside the WEB-INF folder. These changes are done in the server.xml file, which is present in (E:\Program Files\Apache Software Foundation\Tomcat 6.0\conf) directory.

Running first application in jsf

Create a file named as hello.jsp in the context folder.

hello.jsp

<%@taglib uri="http://java.sun.com/jsf/html" prefix="h"%>
<%@taglib uri="http://java.sun.com/jsf/core" prefix="f"%>

      
<html>
   <body>
    <f:view>
   <h1><h:outputText value="Hello, Welcome to JSF 1.2 World!"/></h1>
      </f:view>
   </body>
</html>

web.xml settings

<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
   version="2.5">
<context-param>
<param-name>javax.faces.CONFIG_FILES</param-name>
<param-value>/WEB-INF/faces-config.xml</param-value>
</context-param>  
    <servlet>
        <servlet-name>x</servlet-name>    
    <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
        <load-on-startup> 1 </load-on-startup>
    </servlet>    
    <servlet-mapping>
        <servlet-name>x</servlet-name>
        <url-pattern>*.jsf</url-pattern>
    </servlet-mapping>
</web-app>

Running the application

Run the tomcat then write the below line in the Url
http://localhost:8081/javajsf/hello.jsf

Here javajsf is the Context path, which we mentioned in the server.xml file, which is present in (E:\Program Files\Apache Software Foundation\Tomcat 6.0\conf) directory.



 


 Subscribe to Articles

     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend