Java 怎么获取要在JSF中呈现的字符串值

2019年8月15日21:18:22 评论 26

1.简介

Java 怎么获取要在JSF中呈现的字符串值?——请看下文。

2.代码

/**
 * 版权所有 编程十万个怎么办(www.tah1986.com)
 */

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import javax.el.ValueExpression;
import javax.faces.FacesException;
import javax.faces.component.EditableValueHolder;
import javax.faces.component.UIComponent;
import javax.faces.component.UIForm;
import javax.faces.component.UISelectItem;
import javax.faces.component.UISelectItems;
import javax.faces.component.ValueHolder;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.model.SelectItem;

public class Main{
    
    public static String getStringValueToRender(FacesContext facesContext,
            UIComponent component) {
        if (component instanceof ValueHolder) {

            if (component instanceof EditableValueHolder) {
                Object submittedValue = ((EditableValueHolder) component)
                        .getSubmittedValue();
                if (submittedValue != null) {
                    return submittedValue.toString();
                }
            }

            ValueHolder valueHolder = (ValueHolder) component;
            Object value = valueHolder.getValue();
            if (value == null)
                return "";

            
            if (valueHolder.getConverter() != null) {
                return valueHolder.getConverter().getAsString(facesContext,
                        component, value);
            }
            
            else {
                ValueExpression expr = component
                        .getValueExpression("value");
                if (expr != null) {
                    Class<?> valueType = expr.getType(facesContext
                            .getELContext());
                    if (valueType != null) {
                        Converter converterForType = facesContext
                                .getApplication()
                                .createConverter(valueType);

                        if (converterForType != null)
                            return converterForType.getAsString(
                                    facesContext, component, value);
                    }
                }
            }

            
            return value.toString();
        } else {
            
            
            String value = component.toString();

            if (value != null)
                return value.trim();
            else
                return "";
        }
    }
    
    public static String getStringValueToRender(FacesContext facesContext,
            UIComponent component, Object value) {
        if (value == null)
            return null;

        ValueHolder valueHolder = (ValueHolder) component;

        Converter converter = valueHolder.getConverter();
        if (converter != null) {
            return converter.getAsString(facesContext, component, value);
        } else {
            ValueExpression expr = component.getValueExpression("value");
            if (expr != null) {
                Class<?> valueType = expr.getType(facesContext
                        .getELContext());
                Converter converterForType = facesContext.getApplication()
                        .createConverter(valueType);

                if (converterForType != null)
                    return converterForType.getAsString(facesContext,
                            component, value);
            }
        }

        return value.toString();
    }
}

 

3.总结

示例代码演示了 Java 怎么获取要在JSF中呈现的字符串值,具体步骤参见示例代码。

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: