richlist:Shuttle的应用

最近在JSF的开发中使用了richfaces插件,使用了其便捷的标签插件:

其中richlist:Shuttle就是一个比较好用的标签,以下讲其标签的具体应用的测试实例:

首先在backbean中需要做以下配置:

 <managed-bean>
      <managed-bean-name>testShuffle</managed-bean-name>
     <managed-bean-class>demo.controller.TestShuffle</managed-bean-class>
     <managed-bean-scope>session</managed-bean-scope>
  </managed-bean>
<converter>
       <converter-id>Shuttleconverter</converter-id>
       <converter-class>demo.bean.Converter</converter-class>
</converter>

TestShuffle是用来实现生成List的action:

public class TestShuffle {
 
 private List<ShuffleItem> sourceList = new ArrayList<ShuffleItem>();
 private List<ShuffleItem> targetList = new ArrayList<ShuffleItem>();
 ApplicationContext context = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
 IUserDAO userdao = (UserDAO)context.getBean("userDao");
 public List<ShuffleItem> getSourceList() {
  return sourceList;
 }
 public void setSourceList(List<ShuffleItem> sourceList) {
  this.sourceList = sourceList;
 }
 public List<ShuffleItem> getTargetList() {
  return targetList;
 }
 public void setTargetList(List<ShuffleItem> targetList) {
  this.targetList = targetList;
 }
 
public String getRichList(){
  
   List list = userdao.getNames();
   for(int i = 0;i < list.size();i++){
     HashMap paramMap = (HashMap)list.get(i);
     ShuffleItem item = new ShuffleItem();
    item.setName(paramMap.get("NAME").toString());
    sourceList.add(item);
  }
  return "richList";//转向testRichfacesShuffle.xhtml
 }
/*

*此方法用于测试TargetList取到的值

*/    
  public String getTargetValue(){
     
      System.out.println("targetList.size()="+this.getTargetList().size());
      System.out.println("the value in targetList is:");
      for(int i=0;i < this.getTargetList().size();i++){
        System.out.println(targetList.get(i).getName());
     }
        return null;
 }

展示List的界面testRichfacesShuffle.xhtml:

 <?xml version="1.0" encoding="gb2312"?>
<!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:ui="http://java.sun.com/jsf/facelets"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:f="http://java.sun.com/jsf/core"
      xmlns:rich="http://richfaces.org/rich"
      xmlns:a4j="http://richfaces.org/a4j"
      xmlns:t="http://myfaces.apache.org/tomahawk"
      xmlns:c="http://java.sun.com/jstl/core">
      <!--引用统一的模板-->
   <ui:composition template="pages/templates/Demotemplate.xhtml">
      <!--header区域-->
   <ui:define name="pageHeader">测试rich:listShuttle的取值</ui:define>
   <!--body区域-->
   <ui:define name="body">
  

       <h:form> 
     <h:messages>
      <f:facet name="errorMessage"></f:facet>
     </h:messages>
     <rich:listShuttle var="items"
       sourceValue="#{testShuffle.sourceList}"
       targetValue="#{testShuffle.targetList}"
       targetRequired="true"
       converter="Shuttleconverter"
       sourceCaptionLabel="备选字段" targetCaptionLabel="已选字段"
       listsHeight="200"
                             sourceListWidth="130"
                             targetListWidth="130" >
       <rich:column width="50">
                    <h:outputText value="#{items.name}"></h:outputText>
                </rich:column>
     </rich:listShuttle>
     <h:commandButton action="#{testShuffle.getTargetValue}" value="下一步"  />
      </h:form>
   </ui:define>
   </ui:composition>
</html> 

注意:此处的 converter需要自己实现;以下是实现的代码:

首先:定义Converter.java类:

public class Converter implements javax.faces.convert.Converter{

 public Object getAsObject(FacesContext context, UIComponent component,
   String value) {
  return new ShuffleItem(value);
 }

 public String getAsString(FacesContext context, UIComponent component,
   Object value) {

  ShuffleItem optionItem = (ShuffleItem) value;
  return optionItem.getName();
 }

}

其次是ShuffleItem .java:

public class ShuffleItem {
 
private String name;


public ShuffleItem(){

}
public ShuffleItem(String name) {
 super();
 this.name = name;
}


public String getName() {
 return name;
}

public void setName(String name) {
 this.name = name;
}
@Override
public int hashCode() {
 final int prime = 31;
 int result = 1;
 result = prime * result + ((name == null) ? 0 : name.hashCode());
 return result;
}
@Override
public boolean equals(Object obj) {
 if (this == obj)
  return true;
 if (obj == null)
  return false;
 if (getClass() != obj.getClass())
  return false;
 ShuffleItem other = (ShuffleItem) obj;
 if (name == null) {
  if (other.name != null)
   return false;
 } else if (!name.equals(other.name))
  return false;
 return true;
}


}
必须要实现转换器(converter)才能在列表间移动数据的时候分别取得源列表和目标列表的值!
 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值