我试图转换一个Outputtext -简单地说:
<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-class:
@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);
}
}
我试图用<h:inputtext/>
输入一个字符串到Bean属性name
,并用<h:outputtext/>
打印出来,但用TextToStringConverter
转换。但是转换器似乎从来没有被调用过——我错过了什么吗?
您定义了输出文本的转换器。因此,应该调用getAsString()
方法。在此方法中没有System.out.println
。也许这就是为什么您认为没有调用转换器的原因。
代码对我来说似乎是正确的