我正在尝试迁移自定义组件以在MyFaces 2.0上呈现SelectOneMenu,我需要使用此库,因为WAS 8.5已从Mojarra 1.2移至MyFaces 2.0实现。
在mojarra 1.2中,我从com.sun.faces.renderkit.html_basic.MenuRenderer
延伸了一个方法renderOptions
,该方法以FacesContext context, UIComponent component, List<SelectItem> listSelectItems
作为参数接收,但现在使用MyFaces我没有类似的方法。
这是我在渲染之前用于订购项目的方法(Mojarra 1.2):
@Override
protected void renderOptions(FacesContext context, UIComponent component,
List<SelectItem> listSelectItems) throws IOException {
COBISSelectOneMenuUIComponent uicomponent = (COBISSelectOneMenuUIComponent) component;
String order = uicomponent.getOrder();
if (order == null || order.isEmpty()) {
order = DEFAULT_ORDER;
}
if (order.equals("asc")) {
Collections.sort(listSelectItems, comparatorSelectItemLabel);
} else {
if (order.equals("desc")) {
Collections.sort(listSelectItems,
comparatorSelectItemLabelInvers);
}
}
super.renderOptions(context, uicomponent, listSelectItems);
}
现在我的MyFaces我找不到在呈现它们之前订购SelectItems的方法。
我尝试使用此代码(相关部分)使用MyFaces构建SelectOneMenu:
package cobiscorp.cobis.ce.components.jsf;
import javax.faces.component.FacesComponent;
import javax.faces.component.UISelectItems;
import javax.faces.component.html.HtmlSelectOneMenu;
@FacesComponent(value=COBISSelectOneMenuComponent.COMPONENT_TYPE)
public class COBISSelectOneMenuComponent extends HtmlSelectOneMenu {
/**
* Component family of {@link COBISSelectOneMenuComponent}.
* */
public static final String COMPONENT_FAMILY = "COBISSelectOneMenuComponent";
/**
* Component type of {@link COBISSelectOneMenuComponent}.
* */
public static final String COMPONENT_TYPE = "COBISSelectOneMenuComponent";
@Override
public String getFamily() {
return COBISSelectOneMenuComponent.COMPONENT_FAMILY;
}
public static final String ATTR_ORDER = "order";
public static final String ATTR_ORDER_DEFAULT = "asc";
public String getOrder() {
return (String) getStateHelper().eval(ATTR_ORDER, ATTR_ORDER_DEFAULT);
}
public void setOrder(String order) {
getStateHelper().put(ATTR_ORDER, order);
}
}
渲染就是这个(相关部分):
package cobiscorp.cobis.ce.components.render;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import javax.faces.component.UIComponent;
import javax.faces.component.UISelectItems;
import javax.faces.component.UISelectOne;
import javax.faces.context.FacesContext;
import javax.faces.context.ResponseWriter;
import javax.faces.convert.Converter;
import javax.faces.model.SelectItem;
import javax.faces.render.FacesRenderer;
import org.apache.myfaces.renderkit.html.HtmlMenuRenderer;
import org.apache.myfaces.shared_impl.renderkit.RendererUtils;
import cobiscorp.cobis.ce.components.jsf.COBISSelectOneMenuComponent;
import cobiscorp.cobis.ce.domains.ICOBISConstants;
import cobiscorp.cobis.ce.util.ComparatorUIComponentItemASC;
import cobiscorp.cobis.ce.util.ComparatorUIComponentItemDESC;
@FacesRenderer(componentFamily = COBISSelectOneMenuComponent.COMPONENT_FAMILY, rendererType = COBISSelectOneMenuRenderer.RENDERER_TYPE)
public class COBISSelectOneMenuRenderer extends HtmlMenuRenderer {
/**
* Renderer type of {@link COBISSelectOneMenuRenderer}.
* */
public static final String RENDERER_TYPE = "cobiscorp.cobis.ce.components.COBISSelectOneMenuRenderer";
/**
* El atributo comparatorSelectItemLabelASC tiene como finalidad comparar los
* select item por los label en orden ascendente
*/
private ComparatorUIComponentItemASC comparatorSelectItemLabelASC;
/**
* El atributo comparatorSelectItemLabelDESC tiene como finalidad comparar
* los select item por los label en orden descendente
*/
private ComparatorUIComponentItemDESC comparatorSelectItemLabelDESC;
/**
* Este constructor inicializa los comparadores
*/
private Boolean loadDefaultOption = null;
public COBISSelectOneMenuRenderer() {
comparatorSelectItemLabelASC = new ComparatorUIComponentItemASC();
comparatorSelectItemLabelDESC = new ComparatorUIComponentItemDESC();
}
/*
* (non-Javadoc)
*
* @see
* com.sun.faces.renderkit.html_basic.MenuRenderer#encodeBegin(javax.faces
* .context.FacesContext, javax.faces.component.UIComponent)
*/
public void encodeBegin(FacesContext context, UIComponent component)
throws IOException {
COBISSelectOneMenuComponent uicomponent = (COBISSelectOneMenuComponent) component;
loadDefaultOption = uicomponent.getLoadDefaultOption();
if(loadDefaultOption){
List<SelectItem> listItems = new ArrayList<SelectItem>(1);
listItems.add(new SelectItem("", ""));
UISelectItems uiSelectItems = new UISelectItems();
uiSelectItems.setValue(listItems);
uicomponent.addSelectItemsDefault(uiSelectItems);
}
super.encodeBegin(context, uicomponent);
}
protected void renderMenu(FacesContext context,
UISelectOne selectOne, boolean disabled, Converter converter)
throws IOException {
System.out.println("renderMenu");
COBISSelectOneMenuComponent uicomponent = (COBISSelectOneMenuComponent) selectOne;
String order = uicomponent.getOrder();
uicomponent.getChildren();
List selectItemList = RendererUtils.getSelectItemList(
(UISelectOne) uicomponent, context);
System.out.println("renderMenu order -> " + order);
if (order.equals("asc")) {
Collections.sort(selectItemList, comparatorSelectItemLabelASC);
} else {
if (order.equals("desc")) {
Collections.sort(selectItemList,
comparatorSelectItemLabelDESC);
}
}
}
/*
* (non-Javadoc)
*
* @see
* com.sun.faces.renderkit.html_basic.MenuRenderer#encodeEnd(javax.faces
* .context.FacesContext, javax.faces.component.UIComponent)
*/
public void encodeEnd(FacesContext context, UIComponent component)
throws IOException {
Boolean autocomplete = null;
String id = null;
COBISSelectOneMenuComponent uicomponent = (COBISSelectOneMenuComponent) component;
this.renderMenu(context, uicomponent, isDisabled(context, uicomponent), getConverter(context, uicomponent));
super.encodeEnd(context, component);
if (context != null) {
ResponseWriter writer = context.getResponseWriter();
if (component.isRendered() && uicomponent.isDisabled() == false) {
autocomplete = uicomponent.getAutocomplete();
id = uicomponent.getId().trim();
if (autocomplete != null && autocomplete.equals("true")) {
writer.startElement("script", component);
writer.writeAttribute("type", "text/javascript", "type");
writer.writeText(String.format(
ICOBISConstants.SCRIPT_DHTML_LIST, id, uicomponent
.getClientId(context)), null);
writer.endElement("script");
}
}
}
}
}
使用前面的代码将组件呈现为无顺序,这是xhtml页面中组件代码的示例:
<cobis:selectOneMenu idForm="form_cita_uno" order="asc">
<f:selectItem itemLabel="C" itemValue="C"></f:selectItem>
<f:selectItem itemLabel="A" itemValue="A"></f:selectItem>
<f:selectItem itemLabel="F" itemValue="F"></f:selectItem>
<f:selectItem itemLabel="D" itemValue="D"></f:selectItem>
<f:selectItem itemLabel="N" itemValue="N"></f:selectItem>
<f:selectItem itemLabel="L" itemValue="L"></f:selectItem>
<f:selectItem itemLabel="Z" itemValue="Z"></f:selectItem>
<f:selectItem itemLabel="X" itemValue="Z"></f:selectItem>
</cobis:selectOneMenu>
可以向我提供有关如何订购MyFaces版本2的商品的想法吗?