我试图转换输出文本 - 就像那样:
<h:form>
<h:inputText value="#{someBean.name}"/>
<h:outputText value="#{someBean.name}" converter="com.java.ee.jsf.TextToStringConverter"/>
<h:commandButton value="submit" action="#{someBean.doSecond()}"/>
</h:form>
ConverterClass:
@FacesConverter("com.java.ee.jsf.TextToStringConverter")
public class TextToStringConverter implements Converter {
@Override
public Object getAsObject(FacesContext context, UIComponent component, String value) {
System.out.println("String: " + component);
return value;
}
@Override
public String getAsString(FacesContext context, UIComponent component, Object value) {
return (String) value;
}
}
SomeBean级:
@Model
public class SomeBean {
private String name;
private int id;
private Double someNumber;
private Date someDate;
blic int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Double getSomeNumber() {
return someNumber;
}
public void setSomeNumber(Double someNumber) {
this.someNumber = someNumber;
}
public Date getSomeDate() {
System.out.println(someDate);
return someDate;
}
public void setSomeDate(Date someDate) {
this.someDate = someDate;
}
public void doSecond() {
System.out.println("doSecond");
System.out.println("name: " + name);
System.out.println("id: " + id);
System.out.println("someNumber: " + someNumber);
}
}
我尝试使用name
将字符串输入到Bean属性<h:inputtext/>
中,并使用<h:outputtext/>
打印出来,但使用TextToStringConverter
进行转换。但转换器似乎永远不会被引用 - 我错过了什么吗?
答案 0 :(得分:1)
您为输出文本定义了转换器。因此,应调用getAsString()
方法。您在此方法中没有System.out.println
。也许这就是为什么你认为没有调用转换器。
代码似乎对我来说是正确的。