A simple example of JSF is provided to introduce the JSF application and some advantages of this MVC framework.

Source: Internet
Author: User

This article briefly introduces JSF, and uses a small example to introduce the development process of the JSF project, from which the benefits of the MVC Framework for Development are realized. It also briefly illustrates a little difference with struts.

 

JSF is a Web-side framework that complies with j2ee5 standards. JSF provides standard programming interfaces, event-driven models, and a wide range of scalable component libraries, including a core JSP tag library for processing events, performing verification, and other non-ui-related operations, the core tag library and a standard HTML Tag library are used to represent the UI component, that is, the HTML Tag library.

 

Shows the JSF architecture:

It can be clearly understood that JSF is a Web Framework compliant with the MVC design specifications, and all requests are handled by the facesserlet (javax. Faces. webapp. facesservlet) class. We only need to configure the servlet to Web. XML to inform the server of the requests sent to the servlet for processing. The following is a simple application.

 

Shows the project structure:

 

JSF project portal:

 

Configure web. XML to tell the server that all requests ending with. faces are handled by JSF. The web. xml configuration is as follows:

<?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>JSFTEST</display-name>    <servlet>  <servlet-name>FacesServlet</servlet-name>  <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>  <load-on-startup>1</load-on-startup>  </servlet>  <servlet-mapping>  <servlet-name>FacesServlet</servlet-name>  <url-pattern>*.faces</url-pattern>  </servlet-mapping>    <welcome-file-list>  <welcome-file>index.faces</welcome-file>  </welcome-file-list></web-app>

 

Next, write the application: the user enters the name and logs in to the background to return to the welcome page. Before writing, let's look at the web. the homepage of the application configured in XML <welcome-file-List> is index. faces. This is a convenient demonstration. Another important point is that if we directly use the JSF tag on the application homepage, if you request this page in the form of a simple Java Web, the javax. servlet. JSP. jspexception: cannot find facescontext. At this time, we can write an index for two pages. JSP (this Homepage contains the JSF tag), an index. faces (do not perform any operation), there are other methods, here only a brief description of the demo, details will be further discussed later.

 

Index. Faces Page code:

 

<%@ page language="java" contentType="text/html; charset=utf-8"  pageEncoding="utf-8"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

Index. jsp page code:

 

<% @ Taglib uri = "http://java.sun.com/jsf/core" prefix = "C" %> <% @ taglib uri = "http://java.sun.com/jsf/html" prefix = "H" %> <% @ page Language = "Java "contenttype =" text/html; charset = UTF-8 "pageencoding =" UTF-8 "%> <! Doctype HTML public "-// W3C // dtd html 4.01 transitional // en" "http://www.w3.org/TR/html4/loose.dtd"> <HTML> 

Seeing the code above, we found that the input box and the login button are bound to a user, which is also different from struts2 in pushing data. What is the user in the preceding section? This involves a key knowledge point of JSF, managed beans. Its role is equivalent to the action of struts2, but it is different. The user actually corresponds to the Java userbean, as shown in the code below.

Userbean code:

Package COM. test; public class userbean {// This attribute is used to bind private string username to the JSF tag; // This method is bound to the Public String login () attribute of the Action attribute of the UI component (button () {system. out. println ("==== login ====="); this. username + = "the background login method is processed"; Return "login";} Public String GetUserName () {return username;} public void setusername (string username) {This. username = username ;}}

 

This bean can be used as a user in index. jsp above because it is configured to be used as a managed beans in the JSF Core Profile faces-config.xml.

 

Faces-config.xml Configuration:

<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE faces-config PUBLIC "-//Sun Microsystems, Inc.//DTD JavaServer Faces Config 1.1//EN" "http://java.sun.com/dtd/web-facesconfig_1_1.dtd"><faces-config><managed-bean><managed-bean-name>user</managed-bean-name><managed-bean-class>com.test.UserBean</managed-bean-class><managed-bean-scope>session</managed-bean-scope></managed-bean><navigation-rule><from-view-id>/index.jsp</from-view-id><navigation-case><from-outcome>login</from-outcome><to-view-id>/welcome.jsp</to-view-id></navigation-case></navigation-rule></faces-config>

 

Enter the username in the previous section to log on and call the login method of userbean. The returned Login ID will be captured by JSF to determine the jump interface in sequence, in the faces-config.xml above it was found to jump to/welcome. JSP page. The output content does not need to be de-linked because we have found that managed beans can be bound to the corresponding interface tag component.

 

Welcome. jsp page code:

 

<% @ Taglib uri = "http://java.sun.com/jsf/core" prefix = "C" %> <% @ taglib uri = "http://java.sun.com/jsf/html" prefix = "H" %> <% @ page Language = "Java "contenttype =" text/html; charset = UTF-8 "pageencoding =" UTF-8 "%> <! Doctype HTML public "-// W3C // dtd html 4.01 transitional // en" "http://www.w3.org/TR/html4/loose.dtd"> <HTML> 

 

Program running effect:

 

First, you can see the following input interface:

 

Then click log on. The login Method Processing result is received in welcome. jsp:

What do we really care about during the entire operation process? It is the background processing (business logic) of page (business flow) redirection and login processes ), as for how to achieve frontend and backend interaction, we do not care too much, so developers can focus more on the business.

 

In terms of role, managed bean is similar to struts2 action, but the role mode is different. For struts2 actions, the application submits requests to struts2 through form submission; however, the managed bean of JSF, the system directly binds the behavior of the UI component in JSF to the property or method of the managed bean.

 

In short, managedbeans are used to bind with JSF labels that are configured in the faces-config.xml, and this file also determines the page Jump process. This MVC framework allows us to link business processes and business logic After configuring the controller, without having to have too many complicated data interactions.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.