该hello1应用程序是一个Web模块,它使用JavaServer Faces技术来显示问候语和响应。您可以使用文本编辑器查看应用程序文件,也可以使用NetBeans IDE。

此应用程序的源代码位于tut-install/examples/web/jsf/hello1/目录中。

查看hello1

  1. 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 lang="en"
xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://xmlns.jcp.org/jsf/html">
<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"/>
<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" />
<p></p>
<h:commandButton id="submit" value="Submit" action="response">
</h:commandButton>
<h:commandButton id="reset" value="Reset" type="reset">
</h:commandButton>
</h:form>
...
</h:body>
</html>

页面上最复杂的元素是inputText字段。该maxlength属性指定字段的最大长度。该required属性指定必须填写该字段; requiredMessage如果字段为空,则该属性提供要显示的错误消息。该title属性提供屏幕阅读器用于视觉禁用的文本。最后,该value属性包含将由Hello托管bean 提供的表达式。

Web页面Hello通过Expression Language(EL)值表达式连接到托管bean,该表达式从托管bean中#{hello.name}检索name属性的值。请注意使用hello引用托管bean Hello。如果@Named在托管bean 的注释中未指定名称,则始终使用小写的类名的第一个字母访问托管bean。

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

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>
  1. 查看hello1.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;
}
}

如果使用bean类的默认名称,则可以指定@Model为注释,而不必同时指定@Named和@RequestScoped。

3. 查看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>

最新文章

  1. fullpage.js全屏滚动插件使用小结
  2. Python 3.5安装JPype
  3. ArrayList和LinkedList的区别
  4. php导出csv数据在浏览器中输出提供下载或保存到文件的示例
  5. (转)使用myeclipse生成实体类和hibernate映射文件
  6. jmeter断言
  7. Java基础之读文件——从文件中读取文本(ReadAString)
  8. [Erlang]怎样加入自己的BIF
  9. 严重: Exception starting filter struts2 --Unable to load configuration
  10. AndroidAutoLayout 屏幕适配
  11. html5 图片上传版本1.0
  12. Java版经典兔子繁殖迭代问题——斐波那契(Fibonacci)数列
  13. CentOS6.9安装WordPress搭建自己的博客网站
  14. ELK原理与简介
  15. 谓词逻辑 p-&gt;q 的真假
  16. laravel表单操作
  17. Linux学习之分区自动挂载与fstab文件修复(九)
  18. ftp删除目录和文件,目录下有文件删除提示【550 Remove directory operation failed.】
  19. SpringData_CrudRepository接口
  20. Configuring Transitive IPMP on Solaris 11

热门文章

  1. druid socket timeout超时15分钟(转载)
  2. day62-html-标签
  3. Linux系统如何记录时间
  4. Django2.0——django-filter: TypeError at *** __init__() got an unexpected keyword argument &#39;name&#39;
  5. stopping service [tomcat],服务未启动
  6. Java——Collection集合、迭代器、泛型
  7. 线程池-进程池-io模型
  8. Mutation|DNM|
  9. [APIO2016]Gap(交互)
  10. MySQL数据库中索引的数据结构是什么?(B树和B+树的区别)