JSF 2 中带有参数的自定义转换器 [英] Custom converter in JSF 2 with arguments

查看:25
本文介绍了JSF 2 中带有参数的自定义转换器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试实现一个自定义的截断转换器,它在给定的索引处截断一个字符串并添加一个延续符号.转换器工作正常,只有当我对参数进行硬编码时,因为它们没有被传递到后端.我做错了什么?

I'm trying to implement a custom truncate converter, which truncates a string at a given index and adds a continuation symbol. The converter works fine, only when i hard code the parameters, as they are not being passed to the backend. What am I doing wrong?

参数是转换器类的属性:

The parameters are properties of the converter class:

@FacesConverter(value = TruncateConverter.CONVERTER_ID)
public class TruncateConverter implements Converter, StateHolder
{
    public static final String CONVERTER_ID = "bla.blablabla.Truncate";

    private int truncateIndex;
    private String contSymbol;

以下是我使用转换器(或尝试使用)的方式:

Here is how i'm using the converter (or trying to):

<h:outputText id="news-text-left" value="#{newsListBean.newsList_teaser.text}">
    <f:converter converterId="bla.blablabla.Truncate" truncateIndex="150" contSymbol="..." />
</h:outputText>

我在谷歌上搜索了很多,但找不到一个带参数的 JSF2 转换器示例...谢谢大家的帮助,非常感谢!

I googled around for quite a bit and wasn't able to find a single example of a JSF2 converter with parameters... Thank you guys for your help, really appreciate it!

推荐答案

您可以查看 JSF2.0 源代码.例如 DateTimeConverter ... svn 存储库中可用的 JSF 源:https://svn.java.net/svn/mojarra~svn/trunk

You may take a look at JSF2.0 sources. For example DateTimeConverter... JSF sources available here in svn reposotory: https://svn.java.net/svn/mojarra~svn/trunk

IMO 创建这样的转换器并不容易.还需要创建转换器标签来注册转换器.

IMO creating such converter is not easy. It also required to create converter tag to register converter.

将一些数据传递给转换器的其他方法是属性.所以你可以写

Other way to pass some data to converter is attibutes. So You can write

<h:outputText ...>
  <f:converter converterId="bla.blablabla.Truncate" />
  <f:attribute name="truncateIndex" value="150"/>
</h:outputText>

比调用component.getAttributes().get("truncateIndex");在转换器代码中.

Than call to component.getAttributes().get("truncateIndex"); in converter code.

这篇关于JSF 2 中带有参数的自定义转换器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆