Build your own app
This part specifically tells how to create your own JavaServer Faces app step by step. The example I use is very simple, it requires the user to enter his (her) name, then click the Submit button, then the application displays a welcome information to the user.
Create the following directory structure:
C: /Tomcat4.1/webapps
Hello
SRC
Web
WEB-INF
Web.xml
Lib
Classes
The basic meaning of this directory structure is that I want to create a new application called Hello. In the Hello subdirectory, there is a SRC subdirectory, put all the Java source files inside; there is a web subdirectory, there is a web-inflicity in this directory, including the web.xml file and the other two subdirectories, respectively It is lib and classes.
Copy all JAR files in the C: / JSF-EA3 / LIB directory to the lib subdirectory we created above.
Create a web.xml file to configure our web app. When using JavaServer Faces, several configurations must be specified, such as: (1) Servlet Context Listener, (2) Servlet Mapping for the JavaServer Faces requests and (3) The above servlet mapping. The following code is a configuration file for this application.
Code 1: Web.xml
XML Version = "1.0"?>
"- // Sun Microsystems, Inc.//dtd Web Application 2.3 // En"
"http://java.sun.com/dtd/web-app_2_3.dtd">
listener>
servlet>
The servlet is received ->
servlet-maping>
web-app>
Create an HTML page using the JavaServer Faces tag. First we write an index.html page, this is the first page that the user enters this application. There is a super connection in this page, click it to start the application. code show as below:
Code 2: Index.html
hEAD>
click here to start the application. P>
body>
html>
When the user clicks "Here", the system will load "index.jsp", the code is as follows:
Code 3: Index.jsp
<% @ Taglib Uri = "http://java.sun.com/jsf/html" prefix = "h"%>
<% @ Taglib URI = "http://java.sun.com/jsf/core" prefix = "f"%>
ModelReference = "UserNameBean.usename" /> h: form> f: USE_FACES> Html> This JSP page has several places worth noting: Customized tag library. The component tag library does not require hardcoding HTML to constitute a UI component, allowing the component to be multiplexed, and Core Tag Library can make the component registration event and other behavior easier. The Form tag is used to represent an input form. INPUT_TEXT and Command_Button are used to represent components in the form, nested in the Form tag. The Input_Text tag indicates a text box that allows the user to enter a string. This marker has two properties, which are ID and modelReference, respectively. The ID attribute corresponds to this component, is optional. ModelReference represents the properties of the model object, this property saves the value of our input text box. Command_button represents a submission button. Write the model object (JavaBean component) if necessary. The model object bean is just like other JavaBean components: it has a set of access methods. The following code segments show the JavaBean components you want to use in our app. Code 4: UserNameBean.java Public class usrnamebean { String username = null; Public userNameBean () { } Public void setusername (String user_name) { UserName = user_name; } Public string getUsername () { Return UserName; } } Http://www.9cbs.net/develop/read_article.asp?id=18707 Develop web applications with JavaServer Faces (2) Http://www.9cbs.net/develop/read_article.asp?id=18710 Develop web applications with JavaServer Faces (4) Http://www.9cbs.net/develop/read_article.asp?id=18712 Develop web applications with JavaServer Faces (5)