从inputText到URL有任何标准的JSF转换器吗?
我正在尝试将inputText
转换为JSF页面中的java.net.URL
:
... ...
我支持的bean是:
import java.net.URL; @ManagedBean public class MyBean { public URL url; }
我应该从头开始实现转换器还是有其他方法?
是的,您需要实现Converter
。 这个特殊情况并不难:
@FacesConverter(forClass=URL.class) public class URLConverter implements Converter { @Override public Object getAsObject(FacesContext context, UIComponent component, String value) { if (value == null) { return null; } try { return new URL(value); } catch (MalformedURLException e) { throw new ConverterException(new FacesMessage(String.format("Cannot convert %s to URL", value)), e); } } @Override public String getAsString(FacesContext context, UIComponent component, Object value) { if (value == null) { return ""; } return value.toString(); } }
把它放在项目的某个地方。 感谢@FacesConverter
它将自动注册。