问题
I have found a Converter online and changed it to my needs as far as I could. The problem is though that I need to add a flag (i.e. a string) that must be checked and than the converter must apply a certain pattern to a string.
Custom Converter:
@FacesConverter("convtest.UrlConverter")
public class UrlConverter implements Converter {
@Override
public Object getAsObject(FacesContext facesContext, UIComponent component, String value) {
StringBuilder url = new StringBuilder();
if(value!=null){
if(value.length()==13){
String tempstring;
tempstring=value.toString();
String finalstring= tempstring.substring(0, 4) + "-" + tempstring.substring(4, 8) + "-" + tempstring.substring(8, 13);
url.append(finalstring);
}else{
url.append(value);
}
}else
url.append("");
try {
new URI(url.toString());
} catch (URISyntaxException e) {
return null;
}
UrlData urlData = new UrlData(url.toString());
return urlData;
}
@Override
public String getAsString(FacesContext facesContext,
UIComponent component, Object value) {
try {
return value.toString();
} catch (Exception e) {
return null;
}
}
}
XHTML:
<h:inputText value="#{userData.data}">
<f:converter converterId="convtest.UrlConverter" />
</h:inputText>
Now the problem is that for example I have 2 conversion types:
hju
zurt
Let's say that hju
have the output format XXXX-XXXX-XXXXX
and zurt
has the output format XX-XX-XX-XX-XX-XX-X
.
Now I would like to call the converter like for example:
<f:converter converterId="convtest.UrlConverter" type="hju" />
Or something like that and get it to use the correct pattern.
Any ideas on how to do this?
回答1:
You need to register the custom converter as a new tag in *.taglib.xml
wherein you can specify as many attributes as you want which will then be mapped as bean properties of the converter instance.
So, given a new property type
:
@FacesConverter("convtest.UrlConverter")
public class UrlConverter implements Converter {
private String type; // +getter+setter
}
And this /WEB-INF/my.taglib.xml
(assuming JSF 2.x on Facelets):
<?xml version="1.0" encoding="UTF-8"?>
<facelet-taglib
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-facelettaglibrary_2_0.xsd"
version="2.0"
>
<namespace>http://example.com/ui</namespace>
<tag>
<tag-name>urlConverter</tag-name>
<converter>
<converter-id>convtest.UrlConverter</converter-id>
</converter>
<attribute>
<name>type</name>
<type>java.lang.String</type>
</attribute>
</tag>
</facelet-taglib>
Which is registered as below in /WEB-INF/web.xml
:
<context-param>
<param-name>javax.faces.FACELETS_LIBRARIES</param-name>
<param-value>/WEB-INF/my.taglib.xml</param-value>
</context-param>
Then this usage should do:
<html ... xmlns:my="http://example.com/ui">
...
<h:inputText ...>
<my:urlConverter type="hju" />
</h:inputText>
<html ... xmlns:my="http://example.com/ui">
...
<h:inputText ...>
<my:urlConverter type="zurt" />
</h:inputText>
Alternatively, if you happen to use JSF utility library OmniFaces, then you can also save the above XML boilerplate by using <o:converter> as below:
<html ... xmlns:o="http://omnifaces.org/ui">
...
<h:inputText ...>
<o:converter converterId="convtest.UrlConverter" type="hju" />
</h:inputText>
<html ... xmlns:o="http://omnifaces.org/ui">
...
<h:inputText ...>
<o:converter converterId="convtest.UrlConverter" type="zurt" />
</h:inputText>
It will transparently set those attributes as converter properties.
回答2:
You should use <f:attribute/>
<h:outputText value="#{userData.data}" >
<f:converter converterId="convtest.UrlConverter" />
<f:attribute name="myCoolFlag" value="hju"/>
</h:outputText>
In the converter you can call component.getAttributes().get("myCoolFlag");
来源:https://stackoverflow.com/questions/31406474/creating-custom-tag-for-converter-with-attributes