hello1源码解析

1:选择hello1文件夹并单击“打开项目”。展开“Web页”节点,然后双击该index.xhtml文件以在编辑器中查看它。

该index.xhtml文件是Facelets应用程序的默认登录页面。在典型的Facelets应用程序中,网页是在XHTML中创建的。对于此应用程序,页面使用简单的标记来显示带有图形图像,标题,字段和两个命令按钮的表单

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> //此标签可告知浏览器文档使用哪种 HTML 或 XHTML规范;DTD一整套文本类型的定义;标准原文的链接

<html lang="en"

      xmlns="http://www.w3.org/1999/xhtml"

      xmlns:h="http://xmlns.jcp.org/jsf/html"> //语言使用英文;xml采用名字空间声明,允许你通过一个网址来识别你的标记;jcp是java社区的执行委员会

    <h:head>

        <title>Facelets Hello Greeting</title>

    </h:head>

    <h:body>

        <h:form>

            <h:graphicImage url="#{resource['images:duke.waving.gif']}"

                            alt="Duke waving his hand"/>          //做为资源形式出现的;alt加载不出来就进行替换

            <h2>Hello, my name is Duke. What's yours?</h2>

            <h:inputText id="username"

                         title="My name is: "

                         value="#{hello.name}"

                         required="true"

                         requiredMessage="Error: A name is required."

                         maxlength="25" />       //Web页面Hello通过Expression Language(EL)值表达式连接到托管bean,

                                                //该表达式从托管bean中#{hello.name}检索name属性的值。请注意使用 hello引用托管bean Hello。如果@Named在托管bean 的注释中未指定名称,

                                               //则始终使用小写的类名的第一个字母访问托管bean

                                               //required属性指定必须填写该字段; requiredMessage如果字段为空,则该 属性提供要显示的错误消息

            <p></p>

            <h:commandButton id="submit" value="Submit" action="response">

            </h:commandButton>

            <h:commandButton id="reset" value="Reset" type="reset">

            </h:commandButton>                   //Submit commandButton元素将操作指定为response,表示单击按钮时,将response.xhtml显示该页面。

        </h:form>

        ...

    </h:body>

</html>

2:双击该response.xhtml文件以查看它。

出现响应页面。甚至比问候页面简单,响应页面包含一个图形图像,一个显示托管bean提供的表达式的标题,以及一个按钮,其 action元素将您传回index.xhtml页面:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"

    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html lang="en"

      xmlns="http://www.w3.org/1999/xhtml"

      xmlns:h="http://xmlns.jcp.org/jsf/html">

    <h:head>

        <title>Facelets Hello Response</title>

    </h:head>

    <h:body>

        <h:form>

            <h:graphicImage url="#{resource['images:duke.waving.gif']}"

                            alt="Duke waving his hand"/>

            <h2>Hello, #{hello.name}!</h2>

            <p></p>

            <h:commandButton id="back" value="Back" action="index" />

        </h:form>

    </h:body>

</html>

3.展开“源包”节点,然后展开javaeetutorial.hello1 节点。双击该Hello.java文件以查看它。在Hello类,称为管理bean类,提供了getter和setter方法name中的Facelets页面表达式中使用属性。默认情况下,表达式语言引用类名,第一个字母为小写(hello.name)。

package javaeetutorial.hello1;

import javax.enterprise.context.RequestScoped;

import javax.inject.Named;   //使用请求范围将类标识为托管bean,范围定义应用程序数据如何保持和共享

@Named

@RequestScoped             //@表示注解

public class Hello {

    private String name;

    public Hello() {

    }

    public String getName() {

        return name;

    }

    public void setName(String user_name) {

        this.name = user_name;

    }

}

//JavaServer Faces应用程序中最常用的范围如下:

//Request(@RequestScoped):请求范围在Web应用程序中的单个HTTP请求期间保持不变。在类似的应用程序hello1中,应用程序由单个请求和响应组成,bean使用请求范围

//Session(@SessionScoped):会话范围在Web应用程序中的多个HTTP请求中保持不变。当应用程序由需要维护数据的多个请求和响应组成时,bean使用会话范围

//Application(@ApplicationScoped):应用程序范围在所有用户与Web应用程序的交互中持续存在

4:在“Web页”节点下,展开WEB-INF节点,然后双击该web.xml文件以进行查看。该web.xml文件包含Facelets应用程序所需的几个元素。使用NetBeans IDE创建应用程序时,将自动创建以下所有内容。

指定项目阶段的上下文参数:

    <context-param>

        <param-name>javax.faces.PROJECT_STAGE</param-name>

        <param-value>Development</param-value>

    </context-param>

上下文参数提供Web应用程序所需的配置信息。应用程序可以定义自己的上下文参数。此外,JavaServer Faces技术和Java Servlet技术定义了应用程序可以使用的上下文参数。

一个servlet元素及其servlet-mapping元素指定 FacesServlet。所有带.xhtml后缀的文件都将匹配:

    <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>

一个welcome-file-list元素指定着陆页的位置:

    <welcome-file-list>

        <welcome-file>index.xhtml</welcome-file>

    </welcome-file-list>

原文地址:https://www.cnblogs.com/April315/p/10521219.html