taking multiple values from inputText field separated by commas in JSF

依然范特西╮ 提交于 2019-12-20 07:12:53

问题


I am designing an email client system using JSF Framework. The UI should be capable of taking multiple recipient address in the same inputText field each of which is separated by commas(,). How can i achieve this?


回答1:


As per the comments:

can i assign the value attribute of inputText field to an array?

You could implement a Converter for this.

@FacesConverter("commaSeparatedFieldConverter")
public class CommaSeparatedFieldConverter implements Converter {

    @Override
    public String getAsString(FacesContext context, UIComponent component, Object value) {
        if (value == null) {
            return null;
        }

        String[] strings = (String[]) value;
        StringBuilder builder = new StringBuilder();

        for (String string : strings) {
            if (builder.length() > 0) {
                builder.append(",");
            }

            builder.append(string);
        }

        return builder.toString();
    }

    @Override
    public Object getAsObject(FacesContext context, UIComponent component, String value) {
        if (value == null) {
            return null;
        }

        return value.split(",");
    }

}

Use it as follows:

<h:inputText value="#{bean.addresses}" converter="commaSeparatedFieldConverter" />

with

private String[] addresses;


来源:https://stackoverflow.com/questions/8478698/taking-multiple-values-from-inputtext-field-separated-by-commas-in-jsf

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!