RichFaces - 选择组件

在本章中,我们将了解RichFaces Technology提供的不同选择组件.

< rich:pickList>

使用此标签,我们可以从填充的列表中选择一个值.它还允许我们向另一个List添加和删除列表组件.以下示例演示了它的工作原理.继续创建一个xhtml文件并将其命名为"pickListExample.xhtml"并将以下代码放入其中.

<?xml version = "1.0" encoding = "UTF-8"?>  
<!DOCTYPE html> 
<html xmlns  =  "http://www.w3.org/1999/xhtml"   
   xmlns:h = "http://java.sun.com/jsf/html"   
   xmlns:f = "http://java.sun.com/jsf/core"   
   xmlns:ui = "http://java.sun.com/jsf/facelets"   
   xmlns:a4j = "http://richfaces.org/a4j"   
   xmlns:rich = "http://richfaces.org/rich"> 
   
   <h:head> 
      <title>PickList Example</title> 
   </h:head> 
    
   <h:body> 
      <h:form>   
         <h:outputText value  =  "Pick List Example"/>
         <br/>
         <br/>      
         
         <rich:pickList value = "#{managedBean.subjectList}"   
            sourceCaption = "SubjectList"   
            targetCaption = "Selected Subject"   
            listWidth = "170px"   
            listHeight = "120px"       
            orderable = "true">   
            
            <f:selectItems value = "#{managedBean.subjectList}" 
               itemValue = "#{subject}" itemLabel = "#{subject.subjectName}"/>   
         </rich:pickList>  
      </h:form>
   </h:body> 
   
</html>

我们需要修改managedBean.java文件以填充xhtml文件中的列表组件.以下是我们修改过的Java文件的快照.

import java.util.Arrays; 
import java.util.List; 
import javax.faces.bean.ManagedBean;   
import javax.faces.bean.RequestScoped;   

@ManagedBean   
@RequestScoped   

public class managedBean {   
   String message;  
   String job; 
   private List<String> SubjectList = Arrays.asList(
      "Richface","AJAX","JAVA","JSF","DOTNET","python"); 
   
   public String getMessage() {   
      return message;   
   }   
   public void setMessage(String message) {   
      System.out.println("setMessage method is getting called with--"+message); 
      this.message = message;   
   } 
   public String getJob() { 
      return job; 
   } 
   public void setJob(String job) { 
      System.out.println("setJob method is getting called with--"+job); 
      this.job = job; 
   } 
   public List<String> getSubjectList() { 
      return SubjectList;
   }  
   public void setSubjectList(List<String> SubjectList) { 
      this.SubjectList = SubjectList; 
   } 
}

上面的代码将在浏览器中产生以下输出. pickList标记的"value"属性只是bean类的"getSubjectList()". "itemValue"是对象类的缩写,对应的"itemLabel"是实例值名称.在此示例中,我们的pickList标记自动创建两个名为"sourceCaption"和"targetCaption"的单独列表.属性可订购用于维护目标列表中的选择顺序.

Rich Picklist

< rich:orderingList>

此标记用于整体呈现列表. &LT; orderingList&GT;将自动提供一些按钮,如通过列表传播的功能,它有助于订购所选项目.在下面的示例中,我们将使用以下代码为"OrderingListExample.xhtml"创建一个orderingList.

<?xml version = "1.0" encoding = "UTF-8"?>
<!DOCTYPE html> 
<html xmlns = "http://www.w3.org/1999/xhtml"   
   xmlns:h = "http://java.sun.com/jsf/html"   
   xmlns:f = "http://java.sun.com/jsf/core"   
   xmlns:ui = "http://java.sun.com/jsf/facelets"   
   xmlns:a4j = "http://richfaces.org/a4j"   
   xmlns:rich = "http://richfaces.org/rich"> 
   
   <h:head> 
      <title>OrderingList Example</title> 
   </h:head> 
    
   <h:body> 
      <h:form>   
         <h:outputText value = "ordering List Example"/><br/><br/>
         <rich:orderingList value = "#{managedBean.subjectList}"  
            itemValue = "#{subject}" 
            itemLabel = "#{subject.subjectName}" >   
         </rich:orderingList>  
      </h:form>    
   </h:body> 
   
</html>

我们不需要更改我们的bean类,因为我们使用不同的标记为不同的表示再次填充相同的列表.与前面的示例一样,即使在这里,值属性也会保留整个列表来自"getSubjectList()". "itemValue"和"itemLabel"分别保存对象类和相应实例变量的值.

上面的代码将在浏览器中生成以下输出.

Rich OrderingList

< rich:ListShuttle>

ListShuttle标签在RichFaces 3中可用.它有助于传播一个列表并将相同的值放入另一个列表中.在RichFaces 4中,此标记已被抑制,因为另一个名为< rich:pickList>的新标记可以实现相同的功能.如上所述.如果您使用的是RichFaces 3.0,则可以按以下方式使用此标记.

<rich:listShuttle sourceValue = "#{toolBar.freeItems}" 
   targetValue = "#{toolBar.items}" var = "items" listsHeight = "150" 
   sourceListWidth = "130" targetListWidth = "130" 
   sourceCaptionLabel = "Available Items" 
   targetCaptionLabel = "Currently Active Items" 
   converter = "listShuttleconverter">  
   
   <rich:column width = "18">  
      <h:graphicImage value = "#{items.iconURI}"></h:graphicImage> 
   </rich:column> 
   
   <rich:column> 
      <h:outputText value = "#{items.label}"></h:outputText> 
   </rich:column> 
   
   <a4j:support event = "onlistchanged" reRender = "toolBar" /> 
   <a4j:support event = "onorderchanged" reRender = "toolBar" /> 
</rich:listShuttle>

使用pickList而不是使用此标签非常方便,因为只需编写两行代码就可以使用pickList实现相同的功能.