清單2.User類
- 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}引用發生在清單1中,其中,我將user托管bean的country屬性指定為<util:autoComplete>組件的值。
清單3顯示了AutoComplete類,該類定義了countrIEs屬性,即自動完成組件的完成項目列表:
清單3.完成項目
- 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",
- };
- }
- }
自動完成組件的使用方法已經介紹完畢。現在,您將了解它的工作原理。