1. Trang chủ
  2. » Công Nghệ Thông Tin

leJSF Mkyong Tutorial

321 619 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

leJSF Mkyong Tutorial 1.JSF 2.0 hello world example . Example : HelloWorld.java package com.mkyong.common; import javax.faces.bean.ManagedBean; import javax.faces.bean.SessionScoped; @ManagedBean //allow call this bean in xhtml page @SessionScoped public class HelloWorld implements java.io.Serializable{ private static final long serialVersionUID = 1L; private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } public static long getSerialversionuid() { return serialVersionUID; } } web.xml <?xml version="1.0" encoding="UTF-8"?> <web-app id="WebApp_ID" version="2.5" 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"> <display-name>JSF_Project</display-name> <context-param> <description>State saving method: 'client' or 'server' (=default). See JSF Specification 2.5.2</description> <param-name>javax.faces.STATE_SAVING_METHOD</param-name> <param-value>client</param-value> </context-param> <context-param> <param- name>javax.servlet.jsp.jstl.fmt.localizationContext</param-name> <param-value>resources.application</param-value> </context-param> <listener> <listener-class>com.sun.faces.config.ConfigureListener</listener- class> </listener> <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>/faces/*</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>Faces Servlet</servlet-name> <url-pattern>*.jsf</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>Faces Servlet</servlet-name> <url-pattern>*.faces</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>Faces Servlet</servlet-name> <url-pattern>*.xhtml</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>hello.xhtml</welcome-file> </welcome-file-list> </web-app> hello.xhtml <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xmlns:f="http://java.sun.com/jsf/core" xmlns:h="http://java.sun.com/jsf/html"> <h:head> <title>JSF 2.0 hello world</title> </h:head> <h:body> <h3>JSF 2.0 Hello World Example - hello.xhtml</h3> <h:form> <h:inputText value="#{helloWorld.name}" /> <h:commandButton action="welcome" value="Welcome Me" /> </h:form> </h:body> </html> welcome.xhtml <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xmlns:f="http://java.sun.com/jsf/core" xmlns:h="http://java.sun.com/jsf/html"> <h:head> <title>JSF 2.0 welcome</title> </h:head> <h:form> <h3>Welcome to JSF 2.0 -welcome.xhtml</h3> <h4>Welcome #{helloWorld.name}</h4> </h:form> </html> Result : Reference : JSF 2.0 hello world example Posted on September 7, 2010 , Last modified : November 6, 2012 By mkyong 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 1. JSF 2.1.7 2. Maven 3 3. Eclipse 3.6 4. JDK 1.6 5. 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. 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. <dependencies> <dependency> <groupId>javax.faces</groupId> <artifactId>jsf-api</artifactId> <version>2.0</version> <scope>provided</scope> </dependency> </dependencies> <repositories> <repository> <id>java.net.m2</id> <name>java.net m2 repo</name> <url>http://download.java.net/maven/2</url> </repository> </repositories> For simple servlet container like Tomcat This is a bit troublesome, you need to download following dependencies. File : pom.xml <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.mkyong.common</groupId> <artifactId>JavaServerFaces</artifactId> <packaging>war</packaging> <version>1.0-SNAPSHOT</version> <name>JavaServerFaces Maven Webapp</name> <url>http://maven.apache.org</url> <dependencies> <dependency> <groupId>com.sun.faces</groupId> <artifactId>jsf-api</artifactId> <version>2.1.7</version> </dependency> <dependency> <groupId>com.sun.faces</groupId> <artifactId>jsf-impl</artifactId> <version>2.1.7</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> <version>2.5</version> </dependency> <dependency> <groupId>javax.servlet.jsp</groupId> <artifactId>jsp-api</artifactId> <version>2.1</version> </dependency> <! Tomcat 6 need this > <dependency> <groupId>com.sun.el</groupId> <artifactId>el-ri</artifactId> <version>1.0</version> </dependency> </dependencies> <build> <finalName>JavaServerFaces</finalName> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>2.3.1</version> <configuration> <source>1.6</source> <target>1.6</target> </configuration> </plugin> </plugins> </build> </project> Note For more detail about the JSF 2.0 dependencies, please refer to this official JSF 2.0 release note. 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 <dependency> <groupId>org.glassfish.web</groupId> <artifactId>el-impl</artifactId> <version>2.2</version> </dependency> 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 @SessionScoped public 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. <html xmlns="http://www.w3.org/1999/xhtml" xmlns:f="http://java.sun.com/jsf/core" xmlns:h="http://java.sun.com/jsf/html"> File : hello.xhtml – Renders a JSF text box and link it with the “helloBean” (JSF managed bean), “name” property, and also a button to display the “welcome.xhtml” page when it’s clicked. <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xmlns:f="http://java.sun.com/jsf/core" xmlns:h="http://java.sun.com/jsf/html"> <h:head> <title>JSF 2.0 Hello World</title> </h:head> <h:body> <h3>JSF 2.0 Hello World Example - hello.xhtml</h3> <h:form> <h:inputText value="#{helloBean.name}"></h:inputText> <h:commandButton value="Welcome Me" action="welcome"></h:commandButton> </h:form> </h:body> </html> 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. <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xmlns:h="http://java.sun.com/jsf/html"> <h:head> <title>JSF 2.0 Hello World</title> </h:head> <h:body bgcolor="white"> <h3>JSF 2.0 Hello World Example - welcome.xhtml</h3> <h4>Welcome #{helloBean.name}</h4> </h:body> </html> 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 <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> <display-name>JavaServerFaces</display-name> <! Change to "Production" when you are ready to deploy > <context-param> <param-name>javax.faces.PROJECT_STAGE</param-name> <param-value>Development</param-value> </context-param> [...]... value="#{helloWorld.SayWelcome()}" /> Reference : JSF 2.0 + Ajax hello world example Posted on September 8, 2010 , By mkyong Last modified : August 29, 2012 In JSF 2.0, coding Ajax is just like coding a normal HTML tag, it’s extremely easy In this tutorial, you will restructure the last JSF 2.0 hello world example, so that, when the button is clicked, it will make an Ajax request instead... helloWorld com .mkyong. common.HelloWorld session Reference: Configure Managed Beans in JSF 2.0 Posted on September 9, 2010 , Last modified : August 29, 2012 By mkyong In JSF 2.0, Java bean that can be accessed from JSF page is called Managed... 1"> Reference : JSF 2.0 and Resource Bundles example Posted on September 8, 2010 , By mkyong Last modified : August 29, 2012 In this tutorial, we demonstrate the use of resource bundle to display messages in JSF 2.0 For maintainability concern, it’s always recommended to put all the messages in properties file,... com .mkyong. messages msg 2 Local Resource Bundle To load the properties file locally, or for specified page only Declare the tag in the page that need to access the message in the messages.properties 3 JSF 2.0 Pages... older than 2.1… 7 Eclipse IDE : Unsupported content type in editor 8 Eclipse IDE : xhtml code assist is not working for JSF tag 2.JSF 2.0 +Ajax hello world example Example : HelloWorld.java package com .mkyong. common; import javax.faces.bean.ManagedBean; import javax.faces.bean.SessionScoped; @ManagedBean @SessionScoped public class HelloWorld implements java.io.Serializable{ private static final long... the HTML form value) There are two ways to configure the managed bean : 1 Configure Managed Bean with Annotation In JSF 2.0, you can annotated a Managed Bean with new @ManagedBean annotation package com .mkyong. common; import javax.faces.bean.ManagedBean; import javax.faces.bean.SessionScoped; import java.io.Serializable; @ManagedBean @SessionScoped public class HelloBean implements Serializable { private... xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-facesconfig_2_0.xsd" version="2.0"> helloBean com .mkyong. common.HelloBean session Best Practice It’s recommended to put the managed beans in a separate XML file... JSF-2-Managed-Beans-Example.zip (10KB) 5.Injecting managed beans in JSF 2.0 Example : ??????????????????????????? Reference : Injecting Managed beans in JSF 2.0 Posted on September 10, 2010 , Last modified : August 29, 2012 By mkyong In JSF 2.0, a new @ManagedProperty annotation is used to dependency injection (DI) a managed bean into the property of another managed bean Let see a @ManagedProperty example : MessageBean.java... Reference 1 ManagedProperty Javadoc 2 JSF 2.0 : managed bean x does not exist, Check that appropriate getter and/or setter methods exist 6.Implicit navigation in JSF 2.0 Example : MessageBean.java package com .mkyong. common; public class MessageBean implements java.io.Serializable { private String message; public String getMessage() { return message; } public void setMessage(String message) { this.message =... value="#{messageBean.message}" /> Reference : Implicit Navigation in JSF 2.0 Posted on September 10, 2010 , By mkyong Last modified : August 29, 2012 In JSF 1.2, all the page navigation are required to declare in the “faces-config.xml” file like this : page1.xhtml . 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. 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. xmlns:f="http://java.sun.com /jsf/ core" xmlns:h="http://java.sun.com /jsf/ html"> <h:head> <title> ;JSF 2.0 hello world</title> </h:head> <h:body> <h3> ;JSF 2.0

Ngày đăng: 06/05/2014, 13:54

Xem thêm: leJSF Mkyong Tutorial

TỪ KHÓA LIÊN QUAN

Mục lục

    1. Configure Managed Bean with Annotation

    2. Configure Managed Bean with XML

    JSF 2.0 and Resource Bundles example

    Complete JSF internationalization example

    JSF 2 hidden value example

    JSF hidden field example

    How to checked checkbox’s value by default?

    JSF 2 radio buttons example

    How to select radio button value by default?

    How to pre-select a listbox value?

TÀI LIỆU CÙNG NGƯỜI DÙNG

TÀI LIỆU LIÊN QUAN

w