博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
JSF 2.0 hello world example
阅读量:5797 次
发布时间:2019-06-18

本文共 7795 字,大约阅读时间需要 25 分钟。

In this tutorial, we will show you how to develop a JavaServer Faces (JSF) 2.0 hello world example, shows list of JSF 2.0 dependencies, basic annotations and configurations.

Project Environment

This JSF 2.0 example is built with following tools and technologies

  • JSF 2.1.7
  • Maven 3
  • Eclipse 3.6
  • JDK 1.6
  • Tomcat 6.0.26

First, review the final project structure, in case you are confused about where should create the corresponding files or folder later.

270059381721654.png

1. JSF 2.0 Dependencies

Maven central repository has the JSF version up to 1.2 only, to get the JSF 2.0, you may need to download from Java.net repository.

The maven central repository is updated JSF library to 2.1.7. The previous Java.net repository is no longer required.

For Java EE Application Server like Glassfish

In most Java EE application servers, it has build-in support for JSF 2.0, so you need to download the single JSF API for development purpose.

...
javax.faces
jsf-api
2.0
provided
java.net.m2
java.net m2 repo
http://download.java.net/maven/2
...

For simple servlet container like Tomcat

This is a bit troublesome, you need to download following dependencies.

File : pom.xml

4.0.0
com.mkyong.common
JavaServerFaces
war
1.0-SNAPSHOT
JavaServerFaces Maven Webapp
http://maven.apache.org
com.sun.faces
jsf-api
2.1.7
com.sun.faces
jsf-impl
2.1.7
javax.servlet
jstl
1.2
javax.servlet
servlet-api
2.5
javax.servlet.jsp
jsp-api
2.1
com.sun.el
el-ri
1.0
JavaServerFaces
org.apache.maven.plugins
maven-compiler-plugin
2.3.1
1.6
1.6

Warning

The el-ri.jar is an arguable dependency in the Tomcat servlet container, even it’s not stated in the release note, but you need this library to solve the “JSP version of the container is older than 2.1…” error message.

Updated – 21-10-2010

This “el-ri.jar” is too old, it’s recommended to use the latest “el-impl-2.2.jar”, from Java.net

org.glassfish.web
el-impl
2.2

Updated – 25-07-2012

This el-ri.jar dependency is no longer required in Tomcat 7.

2. JSF 2.0 Managed Bean

A Java bean or JSF managed bean, with a name property to store user data. In JSF, managed bean means this Java class or bean can be accessed from a JSF page.

In JSF 2.0, use @ManagedBean annotation to indicate this is a managed bean.

HelloBean.java

package com.mkyong.common;import javax.faces.bean.ManagedBean;import javax.faces.bean.SessionScoped;import java.io.Serializable;@ManagedBean@SessionScopedpublic class HelloBean implements Serializable {    private static final long serialVersionUID = 1L;        private String name;    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }}

Note

In JSF 1.x, you had to declare beans in the faces-config.xml, but this is no longer required in JSF 2.0.

3. JSF 2.0 Pages

In JSF 2.0, it’s recommended to create a JSF page in XHTML file format, a file with a .xhtml extension.

See following two JSF 2.0 pages :

Note

To use the JSF 2.0 components or features, just declared the JSF namespace at the top of the page.

 

JSF 2.0 Hello World Example - hello.xhtml

Note

In JSF 1.x, you had to declare the “navigation rule” in “faces-config.xml“, to tell which page to display when the button is clicked. In JSF 2.0, you can put the page name directly in the button’s “action” attribute. For simple navigation, it’s more than enough, but, for complex navigation, you are still advised to use the “navigation rule” in “faces-config.xml“.

File : welcome.xhtml – Display the submitted text box value.

JSF 2.0 Hello World Example - welcome.xhtml

Welcome #{helloBean.name}

The #{…} indicate this is a JSF expression language, in this case, #{helloBean.name}, when the page is submitted, JSF will find the “helloBean” and set the submitted textbox value via the setName() method. When welcome.xhtml page is display, JSF will find the same session “helloBean” again and display the name property value via the getName() method.

4. JSF 2.0 Serlvet Configuration

Just like any other standard web frameworks, you are required to configure JSF stuffs in web.xml file.

File : web.xml

JavaServerFaces
javax.faces.PROJECT_STAGE
Development
faces/hello.xhtml
Faces Servlet
javax.faces.webapp.FacesServlet
1
Faces Servlet
/faces/*
Faces Servlet
*.jsf
Faces Servlet
*.faces
Faces Servlet
*.xhtml

Define a “javax.faces.webapp.FacesServlet” mapping, and map to those well-known JSF file extensions (/faces/*, *.jsf, *.xhtml,*.faces).

In this case, the below 4 URLs are pointing to the same hello.xhtml.

http://localhost:8080/JavaServerFaces/hello.jsf    http://localhost:8080/JavaServerFaces/hello.faces    http://localhost:8080/JavaServerFaces/hello.xhtml    http://localhost:8080/JavaServerFaces/faces/hello.jsf

In JSF 2.0 development, it’s recommended to set the “javax.faces.PROJECT_STAGE” to “Development“, it will provide many useful debugging information to let you track the bugs easily. For deployment, just change it to “Production“, you just do not want your customer to look at this annoying debugging information :).

5. Demo

A long article end with a project demo :)

URL : http://localhost:8080/JavaServerFaces/hello.jsf

jsf2-hello-world-example-1

A simple JSF page, with a text box and a button.

jsf2-hello-world-example-2

When the button is clicked, displays the submitted text box value.

转载于:https://www.cnblogs.com/ghgyj/p/4762126.html

你可能感兴趣的文章
argparse - 命令行选项与参数解析(转)
查看>>
一维数组
查看>>
Linux学习笔记之三
查看>>
2463: [中山市选2009]谁能赢呢?
查看>>
3631: [JLOI2014]松鼠的新家
查看>>
微信公众号
查看>>
Android_内部文件读取
查看>>
QTP的那些事---webtable的一些重要使用集合精解
查看>>
POJ1061 青蛙的约会(扩展欧几里得)题解
查看>>
[JavaWeb]关于DBUtils中QueryRunner的一些解读(转)
查看>>
C/C++之循环结构
查看>>
Django 2.1.3 文档
查看>>
hdu2147
查看>>
Linux使用bitnami安装redmine
查看>>
Maven 项目生成或者update jdk变为1.5的问题
查看>>
【SICP练习】86 练习2.58
查看>>
python学习笔记一
查看>>
格式化输入函数scanf()及输入输出函数的*修饰符
查看>>
Chapter 8. 面向对象(封装)
查看>>
练习:WinForm--DataGridView增删改查完整版
查看>>