创建自定义标签的转换器属性 [英] Creating custom tag for Converter with attributes
问题描述
我找到了一个转换在线,它改变了我的需求,据我所能。现在的问题是,虽然我需要添加一个标志(即一个字符串)必须进行检查和比转换器必须采用特定的模式的字符串。
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.
自定义转换器:
@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>
现在的问题是,比如我有2个转换类型:
Now the problem is that for example I have 2 conversion types:
-
hju
-
zurt
hju
zurt
让我们说, hju
有输出格式 XXXX-XXXX-XXXXX
和 zurt
的输出格式 XX-XX-XX-XX-XX-XX-X
。
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?
推荐答案
您需要注册自定义转换器作为一个新的标签在 *。taglib.xml
,其中你可以只要你想随后将被映射为转换器实例的bean属性指定许多属性。
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
}
和本 /WEB-INF/my.taglib.xml
(假设Facelets的JSF 2.x中):
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>
这是 /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>
另外,如果你碰巧使用JSF工具库 OmniFaces ,那么你也可以通过保存上述XML样板< A HREF =http://showcase.omnifaces.org/taghandlers/converter相对=nofollow> &LT;○:转换器&GT;
如下:
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.
这篇关于创建自定义标签的转换器属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!