使用JavaServer Faces技术的Web模块:hello1 example

该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>
原文地址:https://www.cnblogs.com/yuanchao-blog/p/10825348.html