воскресенье, 15 апреля 2012 г.

Understanding JSF

Introduction

JSF - Java Server Faces is a Java Web framework based on idea of components driven development. JSF is a standardized Java technology. On my personal experience starting from Java servlets and JSP, JSF really makes difference. The core is the concept which lies behind the idea of JSF. The core idea is reusable components which simplifies the development process a lot.

The core features of JSF which you have to remember are:

1. Managed beans
2. Powerful template system
3. Variety of XML-based tag libraries
4. Ajax in-built support (especially if you will try Richfaces, PrimeFaces or IceFaces)
5. Expression Language (EL) - the technique to access your Managed beans from your JSF pages.

These are the things that I consider the main features of JSF 2. You will see how the majority of those features is used in the following application.

Simple "Hello World" JSF Application

For this tutorial you need to have installed Eclipse with Jboss Tools and configured Jboss Application Server. If you do not have it, please follow the previous tutorials. Now open the Eclipse.

1. File >> New >> Other >> Web >> Dynamic Web Project
2. Project name: helloworld
3. Choose your Jboss runtime
4. Dynamic web module version: 3.0
5. Configuration: Default for your targeted runtime
6. Next
7. Now you have to choose your folders on the build path. I use the following structure:
    src/main/java (For your java classes)
    src/main/resources (For some of your configuration files)
8. In the next window tick on "Generate web.xml"
8.1 Now the wizard will ask if you prefer Java EE perspective. Personally me, I prefer Web Development perspective.


So now you have a basic structure of a web application. But there are a few things that we have to change.
Change your web.xml in WebContent/WEB-INF to the following:

<?xml version="1.0" encoding="UTF-8"?><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_3_0.xsd" version="3.0">
<display-name>Helloworld</display-name>

<context-param> <param-name>javax.faces.PROJECT_STAGE</param-name> <param-value>Development</param-value> </context-param>
<session-config> <session-timeout>6</session-timeout> </session-config>

<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>
<!-- Use Seam Catch for these --> </web-app>


In WebContent/WEB-INF create file faces-config.xml with the following content:


<?xml version="1.0" encoding="UTF-8"?><faces-config version="2.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xi="http://www.w3.org/2001/XInclude" 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-facesconfig_2_0.xsd"> <name>Helloworld1</name>
<application> <resource-bundle> <base-name>resources</base-name> <var>resources</var> </resource-bundle> </application></faces-config>
 Create index.html in the root of your WebContent folder with the following content:


<html><head><meta http-equiv="Refresh" content="0; URL=pages/home.xhtml"/></head></html>
Create home.xhtml in the same directory which will contain the following:

 <ui:composition xmlns="http://www.w3.org/1999/xhtml" xmlns:ui="http://java.sun.com/jsf/facelets" xmlns:f="http://java.sun.com/jsf/core" xmlns:h="http://java.sun.com/jsf/html">



<h:outputText value="Hello World!" />
</ui:composition>
Project >> Clean >> Your project
Now in the bottom you will find Server tab. Add your server and add your application there. Now right click on your application and Full Publish.

Now run your server and go to localhost:8080/helloworld. If you had any questions or problems please write in comments.

Best regards,
Netlink community member



2 комментария:

  1. И это Вы называете Simple? Куча крючков неизвестно зачем нужных. Действительно Simple это http://www.hybridserverpages.com/index_ru.html.

    ОтветитьУдалить
  2. Thanks for the quick tutorial.

    Two things I noticed that were wrong or need added to the instructions above:

    1. In order for the project to work, you will need the JSF jar in your project's lib directory. (javax.faces-2.x.x.jar) This can be downloaded here: http://javaserverfaces.java.net/download.html

    2. home.xhtml needs to be put in a subdirectory called "pages" under the root helloworld folder, or you will get an error that the page cannot be found. "\JSFhelloworld\pages\home.xhtml"

    ОтветитьУдалить