在JSF中将inputText转换为byte [] [英] Convert inputText to byte[] in JSF
问题描述
我正在编写一个Web应用程序,它有一个JSF页面,并在其后面有一个bean。我遇到了麻烦,我认为这是因为这个bean需要一个字节数组用于一个特定的字段,并且它提供了一个字符串。
据我所知, JSF提供了一些功能,可以自动将您在inputText字段中输入的内容转换为所需的数据类型,但是当您需要一个字节[]时,我认为它不会执行此操作...
这仅仅是为JSF编写客户转换器的问题吗?比如像这样:
public class StringToByteArray implements Converter {
...
public byte [] getAsObject(FacesContext context,UIComponent component,String value){
if(StringUtils.isEmpty(value)){return null;}
byte [] valueAsBytes = new byte [];
valueAsBytes = value.getBytes();
返回valueAsBytes; }}
答案是肯定的。这只是写一个自定义转换器的问题。不要因为认为代码太简单了而感到气馁。以下是将自定义对象转换为字符串并返回的转换器的示例。这种转换器通常用于选择菜单:
@FacesConverter(value =merkmalConverter)
public class MerkmalMenuConverter实现了Converter {
public String getAsString(FacesContext context,UIComponent component,Object value){
if(value!= null){
Merkmal m =(Merkmal)value;
return m.getBezeichnung();
}
返回null; //值为空。
$ b @SuppressWarnings(unchecked)
public Object getAsObject(FacesContext context,UIComponent component,String value){
if(value!= null){
MerkmalJpaController mJpaC = new MerkmalJpaController();
列表< Merkmal> mList = mJpaC.findMerkmalEntities(); (Merkmal m:mList){
if(m.getBezeichnung()。equals(value)){
return m;
}
}
}
返回null; //值为空或没有任何匹配。
$ b $使用你的转换器在jsf文件中显示如下
< f:converter converterId =merkmalConverter/>
I'm writing a web application which has a JSF page with a bean behind it. I am having trouble with it and I think it's because the bean is expecting a byte array for one particular field and it's being provided with a String.
From what I understand, JSF provides some functionality to automatically convert whatever you enter in your inputText fields to the required data type, but I don't think it does this when you want a byte[] ...
Is it simply a matter of writing a customer converter for JSF? Such as something like this:
public class StringToByteArray implements Converter {
...
public byte[] getAsObject(FacesContext context, UIComponent component, String value) {
if (StringUtils.isEmpty(value)){ return null;}
byte[] valueAsBytes = new byte[];
valueAsBytes = value.getBytes();
return valueAsBytes; } }
解决方案 The answer is yes. It is simply the matter of writing a custom converter. Don´t be discouraged by thinking "too much code for a simple conversion". Here is an example for a converter that converts a custom object to string and back. This kind of converter is often used for select menus:
@FacesConverter( value="merkmalConverter" )
public class MerkmalMenuConverter implements Converter {
public String getAsString(FacesContext context, UIComponent component, Object value) {
if (value != null) {
Merkmal m = (Merkmal) value;
return m.getBezeichnung();
}
return null; // Value is null.
}
@SuppressWarnings("unchecked")
public Object getAsObject(FacesContext context, UIComponent component, String value) {
if (value != null) {
MerkmalJpaController mJpaC = new MerkmalJpaController();
List<Merkmal> mList = mJpaC.findMerkmalEntities();
for (Merkmal m : mList) {
if (m.getBezeichnung().equals(value)) {
return m;
}
}
}
return null; // Value is null or doesn't have any match.
}
}
Use your converter in the jsf file as shown here as child element of your input field to be converted:
<f:converter converterId="merkmalConverter" />
这篇关于在JSF中将inputText转换为byte []的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!