RichFaces组件listShuttle使用注意事项
1、 县列出官方例子源代码:
package a2; public class ToolBarItem { private String icon; private String label; private String iconURI; public ToolBarItem() { // TODO Auto-generated constructor stub } public ToolBarItem(String label, String icon) { setLabel(label); setIcon(icon); } //getter and setter…
} |
package a2;
import java.util.ArrayList; import java.util.List;
import org.jboss.seam.annotations.Name;
@Name("toolBar") public class ToolBar { private List items = new ArrayList(); private List freeItems = new ArrayList();
public ToolBar() { ToolBarItem item = new ToolBarItem(); item.setIcon("create_folder"); item.setLabel("Create Folder"); items.add(item); item = new ToolBarItem(); item.setIcon("create_doc"); item.setLabel("Create Doc"); items.add(item); item = new ToolBarItem(); item.setIcon("find"); item.setLabel("Find"); items.add(item); item = new ToolBarItem(); item.setIcon("open"); item.setLabel("Open"); freeItems.add(item); item = new ToolBarItem(); item.setIcon("save"); item.setLabel("Save"); freeItems.add(item); item = new ToolBarItem(); item.setIcon("save_all"); item.setLabel("Save All"); freeItems.add(item); item = new ToolBarItem(); item.setIcon("delete"); item.setLabel("Delete"); freeItems.add(item); }
public String do1() { System.out.println(items); System.out.println(freeItems); return null; } // getter and setter } |
package a2;
import javax.faces.component.UIComponent; import javax.faces.context.FacesContext;
import org.jboss.seam.annotations.Name; import org.jboss.seam.annotations.intercept.BypassInterceptors;
@BypassInterceptors @org.jboss.seam.annotations.faces.Converter(id = "listShuttleconverter") @Name("listShuttleconverter") public class Converter implements javax.faces.convert.Converter { public Object getAsObject(FacesContext context, UIComponent component, String value) { int index = value.indexOf(':'); return new ToolBarItem(value.substring(0, index), value.substring(index + 1)); }
public String getAsString(FacesContext context, UIComponent component, Object value) { ToolBarItem optionItem = (ToolBarItem) value; return optionItem.getLabel() + ":" + optionItem.getIcon(); } } |
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <f:view contentType="text/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:a="http://richfaces.org/a4j" xmlns:s="http://jboss.com/products/seam/taglib" xmlns:c="http://java.sun.com/jstl/core" xmlns:r="http://richfaces.org/rich">
<html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <title>seam</title> <link rel="shortcut icon" href="#{request.contextPath}/favicon.ico" />
</head>
<body> <h:form id="form1">
<r:toolBar id="toolBar" itemSeparator="line" height="28px"> <c:forEach items="#{toolBar.items}" var="item"> <h:panelGroup> <h:graphicImage value="#{item.iconURI}" styleClass="pic" /> <h:outputLink value="#"> <h:outputText value="#{item.label}"></h:outputText> </h:outputLink> </h:panelGroup> </c:forEach> </r:toolBar> <r:spacer height="20"></r:spacer>
<r:listShuttle sourceValue="#{toolBar.freeItems}" targetValue="#{toolBar.items}" var="items" listHeight="300" listWidth="300" sourceCaptionLabel="Available Items" targetCaptionLabel="Currently Active Items" converter="listShuttleconverter"> <r:column width="18"> <h:graphicImage value="#{items.iconURI}"></h:graphicImage> </r:column> <r:column> <h:outputText value="#{items.label}"></h:outputText> </r:column> <a:support event="onlistchanged" reRender="toolBar" /> </r:listShuttle>
<r:messages></r:messages> <h:commandButton action="#{toolBar.do1}" value=" 测试 " /> <a:log popup="false" level="ALL" style="width: 800px; height: 300px;"> </a:log> </h:form> </body> </html> </f:view> |
2、 注意事项:
a) 页面中r:listShuttle组件一定要指定converter属性值,
否则会报javax.faces.el.PropertyNotFoundException异常。
b) 实体组件,例如例子中的ToolBarItem组件一定要实现hashCode()和equals() 方法,否则不能通过验证,提示Component “XXXX” has invalid value expression “XXXX”验证信息。
c) r:listShuttle组件中sourceValue和 targetValue为 List or Array of items,而sourceSelection和targetSelection为collection of items。