使用属性为转换器创建自定义标签 [英] 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
有输出格式 XXXX-XXXX-XXXXXX
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.
所以,给定一个新属性type
:
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,那么您也可以使用 <o:converter>
如下:
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屋!