seam级联出现值无效

1,如果用到级联的back bean是长对话范围:在相应的配置文件里加入
<begin-conversation join="true" flush-mode="MANUAL"/>
2,如果不是长对话:将back bean的范围标记为 page,在页面上加入一个转换器(identityEntityConverter)见:util包下的IdentityEntityConverter.java
页面如下:
<h:selectOneMenu value="#{testSelectItems.bmqy}" converter="#{identityEntityConverter}">
                <s:selectItems var="bmqy" value="#{baseBmqyList.allBaseBmqyList()}" label="#{bmqy.qymc}" noSelectionLabel="#{messages.pleaseselect}">
                </s:selectItems>
                <a:support action="#{testSelectItems.cascade}" reRender="bmxx,ygxx" event="onchange"/>
         </h:selectOneMenu>
          <h:selectOneMenu id="bmxx" value="#{testSelectItems.bmxx}" style=" width : 119px; height : 23px;" converter="#{identityEntityConverter}">
                <s:selectItems var="bmxx" value="#{testSelectItems.bmxxList}" label="#{bmxx.bmmc}" noSelectionLabel="#{messages.pleaseselect}">
                </s:selectItems>
                <a:support action="#{testSelectItems.cascade2}" reRender="ygxx" event="onchange"/>
         </h:selectOneMenu>
         <h:selectOneMenu id="ygxx" value="#{testSelectItems.ygxx}" style=" width : 119px; height : 23px;" converter="#{identityEntityConverter}">
                <s:selectItems var="ygxx" value="#{testSelectItems.ygxxList}" label="#{ygxx.ygxm}" noSelectionLabel="#{messages.pleaseselect}">
                </s:selectItems>
         </h:selectOneMenu>




package com.golden.info.util;
import java.io.Serializable;
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;

import javax.faces.component.UIComponent;
import javax.faces.component.UISelectItems;
import javax.faces.context.FacesContext;
import javax.faces.convert.ConverterException;
import javax.faces.model.SelectItem;
import javax.persistence.Id;

import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.Install;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Scope;
import org.jboss.seam.annotations.Transactional;
import org.jboss.seam.annotations.faces.Converter;
import org.jboss.seam.annotations.intercept.BypassInterceptors;
import org.jboss.seam.log.LogProvider;
import org.jboss.seam.log.Logging;
import org.jboss.seam.ui.AbstractEntityLoader;
import org.jboss.seam.util.AnnotatedBeanProperty;

@Name("identityEntityConverter")
@Scope(ScopeType.CONVERSATION)
@Install(precedence = Install.APPLICATION)
@Converter
@BypassInterceptors
public class IdentityEntityConverter implements javax.faces.convert.Converter, Serializable {
	private static final long serialVersionUID = 5291805316838074880L;
	
	private static final LogProvider log = Logging.getLogProvider(IdentityEntityConverter.class);
	
    private AbstractEntityLoader<?> entityLoader;

    public AbstractEntityLoader<?> getEntityLoader() {
        if (entityLoader == null)
        	entityLoader = AbstractEntityLoader.instance();

        return entityLoader;
    }
    public void setEntityLoader(AbstractEntityLoader<?> entityLoader) {
        this.entityLoader = entityLoader;
    }

    @Transactional
    public String getAsString(FacesContext facesContext, UIComponent component, Object value) throws ConverterException {
        if (value == null)
            return null;
        if (value instanceof String)
            return (String) value;
        return getEntityLoader().put(value);
    }

    @Transactional
    public Object getAsObject(FacesContext facesContext, UIComponent component, String value) throws ConverterException {
        if (value == null)
            return null;
        Object loaded = getEntityLoader().get(value);
        if (loaded == null) {
        	log.error("The Entity loader returned a null object");
        	throw new ConverterException("The Entity loader returned a null object.");
        }
        
        Class<?> clazz = loaded.getClass();
        AnnotatedBeanProperty<Id> idProperty;

        /* 
         * Look backwards in the entity inheritance hierarchy for a property annotated with @Id. 
         * Note: The class of a managed entity is a java assist descendant of the entity class.
        */
        do {
        	idProperty = new AnnotatedBeanProperty<Id>(clazz, Id.class);
        	clazz = clazz.getSuperclass();
        } while (!idProperty.isSet() && clazz != null);

        /* If the @Id property cannot be found then there is nothing to do but raise an error */
    	if (!idProperty.isSet()) {
    		log.error("Cannot find a property annotated with @Id on entity");
    		throw new ConverterException("Cannot find a property annotated with @Id on entity");
    	}
    	
        Object loadedId = idProperty.getValue(loaded);
        if (loadedId == null) {
        	log.error("The loaded entity must have a not-null value for its identifier (@Id)");
        	throw new ConverterException("The loaded entity must have a not-null value for its identifier (@Id)");
        }
        Set<Object> originalList = getOriginalSelectItems(component);
        for (Object original : originalList) {
            Object originalId = idProperty.getValue(original);
            if (originalId != null && loadedId.equals(originalId))
                return original;
        }

        /* If the item was not found in the list fall back to the default provided by the EntityLoader */
        return loaded;
    }

    /**
     * Get a list of objects from the component's select items
     * 
     * @param component
     * @return
     */
    protected Set<Object> getOriginalSelectItems(UIComponent component) {
        Set<Object> items = new HashSet<Object>();
        
        for (UIComponent child : component.getChildren()) {
            if (child instanceof UISelectItems) {
                UISelectItems selectItems = (UISelectItems) child;
                for (Object selectItemsValue : (Collection<Object>) selectItems.getValue()) {
                    SelectItem selectItem = (SelectItem) selectItemsValue;
                    if (selectItem.getValue() == null) 
                    	continue;
                    
                    items.add(selectItem.getValue());
                }
                break;
            }
        }
        return items;
    }
}

或者可以重写需要级联的类的equals方法

你可能感兴趣的:(java,bean,UI,jboss,seam)