Board logo

标题: Ajax 组件(2)使用自动完成组件 [打印本页]

作者: look_w    时间: 2018-9-14 13:16     标题: Ajax 组件(2)使用自动完成组件

使用自动完成组件复合组件:基础如果您不熟悉如何实现 JSF 2 复合组件,那么您可以从 “” 这篇文章中了解基本知识。

Locations 自动完成字段是一个 JSF 复合组件,并应用于 facelet,如  所示:
清单 1. facelet
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:util="http://java.sun.com/jsf/composite/util">
   <h:head>
      <title>#{msgs.autoCompleteWindowTitle}</title>
   </h:head>

   <h:body>
      <div style="padding: 20px;">
         <h:form>
            <h:panelGrid columns="2">
               #{msgs.locationsPrompt}
               <util:autoComplete value="#{user.country}"
                     completionItems="#{autoComplete.countries}" />
            </h:panelGrid>
         </h:form>
      </div>
   </h:body>
</html>




中的 facelet 通过声明适当的名称空间 —util— 以及借助组件的相关标记(<util:autoComplete>)来使用 autoComplete 复合组件。
注意  中 <util:autoComplete> 标记的两个属性:
User 类是一个简单的托管 bean,专为本例而设计。其代码如  所示:
清单 2. User 类
1
2
3
4
5
6
7
8
9
10
11
12
13
14
package com.corejsf;

import java.io.Serializable;

import javax.inject.Named;
import javax.enterprise.context.SessionScoped;

@Named()
@SessionScoped
public class User implements Serializable {
  private String country;
  public String getCountry() { return country; }
  public void setCountry(String country) { this.country = country; }
}




请注意 @Named 注释,它与 @SessionScoped 一起实例化了一个名称为 user 的托管 bean,并在 JSF 第一次在 facelet 中遇到 #{user.country} 时将它置于 session 作用域中。此应用程序中唯一的 #{user.country} 引用发生在  中,其中,我将 user 托管 bean 的 country 属性指定为 <util:autoComplete> 组件的值。
显示了 AutoComplete 类,该类定义了 countries 属性,即自动完成组件的完成项目列表:
清单 3. 完成项目
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
package com.corejsf;

import java.io.Serializable;

import javax.enterprise.context.ApplicationScoped;
import javax.inject.Named;

@Named
@ApplicationScoped
public class AutoComplete implements Serializable {
   public String[] getLocations() {
      return new String[] {
              "Abari", "Absurdsvanj", "Adjikistan", "Afromacoland",
              "Agrabah", "Agaria", "Aijina", "Ajir", "Al-Alemand",
              "Al Amarja", "Alaine", "Albenistan", "Aldestan",
              "Al Hari", "Alpine Emirates", "Altruria",
              "Allied States of America", "BabaKiueria", "Babalstan",
              "Babar's Kingdom","Backhairistan", "Bacteria",
              "Bahar", "Bahavia", "Bahkan", "Bakaslavia",
              "Balamkadar", "Baki", "Balinderry", "Balochistan",
              "Baltish", "Baltonia", "Bataniland, Republic of",
              "Bayview", "Banania, Republica de", "Bandrika",
              "Bangalia", "Bangstoff", "Bapetikosweti", "Baracq",
              "Baraza", "Barataria", "Barclay Islands",
              "Barringtonia", "Bay View", "Basenji",
      };
   }
}




自动完成组件的使用方法已经介绍完毕。现在,您将了解它的工作原理。




欢迎光临 电子技术论坛_中国专业的电子工程师学习交流社区-中电网技术论坛 (http://bbs.eccn.com/) Powered by Discuz! 7.0.0