hello1源码解析

时间:2023-03-09 08:44:50
hello1源码解析

1.选择hello1文件夹并单击“打开项目”

2.展开网页节点,双击index.xhtml文件在编辑器中查看它

index.xhtml文件是facelets应用程序的默认登录页,在典型的facelets应用程序中,web页面是在XHTML中创建的,对于此应用程序,该页面简单的标记来显示窗体,其中包含图形图像,标题,字段和两个命令按钮<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"

    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">//此标签可告知浏览器文档使用哪种HTML规范;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." <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>

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

package javaeetutorial.hello1;

import javax.enterprise.context.RequestScoped;
import javax.inject.Named; @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>