首页 文章

JSF:commandLink作为outputFormat的参数

提问于
浏览
4

我正在将一些JSF文件国际化,因此我正在外化字符串(以及使用占位符连接字符串) . 我对JSF(今天和昨天)的经验很少,所以如果对我的问题有一个明显的回答,请原谅!

我已经成功地为简单的占位符使用了h:outputFormat标记(和f:param标记),但现在我正在寻找用commandLink组件替换占位符 .

<h:outputFormat value="#{adminMsgs.scheduleUpdateLink} ">
    <h:commandLink value="#{adminMsgs.update}" action="refresh" />
</h:outputFormat>

属性文件:

scheduleUpdateLink = After waiting a few seconds, please {0} the page.
update = update

并按以下方式输出:

After waiting a few seconds, please <a href="#" class="..." onclick="...;return false;">update</a> the page.

这不起作用(更新链接出现在'scheduleUpdateLink'文本之前),有谁知道我怎么做?

提前致谢 .

EDIT / UPDATE

感谢您的回复McDowell - 非常有用,但仍然没有完全解决我的问题 . 我还需要对输入框(h:inputText)执行相同的操作,并且可能还存在一个资源字符串中有多个占位符的情况 . 因此,我不能保证阿拉伯语中的顺序是一样的 .

如果我使用Java函数;你知道我是否有办法以字符串形式传递JSF标签,例如: <h:outputFormat value=.. . 并使用faces上下文来获取呈现的HTML,然后我可以将其插入到各自的占位符中并以纯HTML格式返回?或者沿着这些方向的任何其他想法?

干杯 .

3 回答

  • 6

    h:outputFormat标签(据我所知)仅使用f:param children作为参数 .

    如果消息是链接(而不是h:commandLink),则可以使用HTML文字:

    <h:outputFormat escape="false" 
        value="...seconds, please &lt;a href='etc..." />.
    

    我不建议尝试通过这样做来模拟h:commandLink . 有可能在_2595768中找到更丰富的h:commandLink版本,它可以完全满足您的需求 .

    将字符串分成两部分是可能的,但是翻译人员的生活很困难,翻译质量可能会受到影响(特别是如果你这么做的话) .

    我会看一下使用这样的自定义函数:

    <f:loadBundle basename="i18n.I18n" var="bundle" />
    <h:outputText value="#{i18n:fragment(bundle.scheduleUpdateLink, 0)}" />
    <h:commandLink>
      <h:outputText value="#{bundle.update}" />
    </h:commandLink>
    <h:outputText value="#{i18n:fragment(bundle.scheduleUpdateLink, 1)}" />
    

    渲染的输出格式如下:

    After waiting a few seconds, please <script type="text/javascript"><!--
        /*JavaScript elided*/
    //-->
    </script><a href="#"
          onclick="return oamSubmitForm('someClientId');">update</a> the page.
    

    该函数由静态方法支持,该方法拆分字符串并返回请求的片段:

    public static String fragment(String string, int index) {
      // TODO: regex probably not complete; ask stackoverflow!
      // see http://java.sun.com/javase/6/docs/api/java/text/MessageFormat.html
      final String regex = "\\{\\d.*?\\}";
      String[] fragments = string.split(regex, index + 2);
      return fragments[index];
      // TODO: error handling
    }
    

    根据您的视图技术(Facelets或JSP),函数声明略有不同:


    编辑:基于更多信息

    JSF采用声明式方法来构建接口 . 您想要一个动态UI,其子项按照它们在翻译字符串中出现的顺序排序 . 这两件事情不一致,但一切都不会丢失 .

    有几种方法可以解决这个问题 .

    • 重新设计UI以使用上面的文本,下面的输入/控制表(流行的表单方法) . 这是最简单的解决方案,但可能无法让您的UI设计师满意 .

    • 使用binding attribute on a panel创建并以编程方式排序所有内容 . 这可能意味着您的支持bean中存在大量重复代码 .

    • 编写一个可重用的自定义控件,可能如下所示:

    .

    <ed:formatPane string="click {0} after filling in forename {1} and surname {2}">
       <h:commandLink value="#{bundle.linkMsg}" />
       <h:inputText value="#{bean.prop1}" />
       <h:inputText value="#{bean.prop2}" />
    </ed:formatPane>
    

    只需替换h:outputFormat的渲染器以支持任意子节点而不是创建完整的自定义控件,可能(而且工作量较少),但我反对它,因为它可能会导致长期维护期间的混乱 .

    有人可能已经编写了一个自定义控件来完成我的建议(它几乎不是一个原创的想法) .

  • 0

    为了使JSF能够正确呈现纯文本,它必须由<html:outputText ...>生成(有一些模糊的技术原因) . 我会用outputText,commandLink和outputText来做这件事 .

    通常,在处理JSF时,一切都必须是标记,而不是纯文本 .

    一个方便的参考:http://exadel.com/tutorial/jsf/jsftags-guide.html

  • 0

    要解决您的问题,您可以使用<h:outputText>,就像Thorbjørn所说的那样 .

    基本上你的代码和属性文件变成这样:

    <h:outputText value="#{adminMsgs.scheduleUpdateLink1}" />
    <h:commandLink value="#{adminMsgs.update}" action="refresh" />
    <h:outputText value0"#{adminMsgs.scheduleUpdateLink2}" />
    

    和这样的属性文件:

    scheduleUpdateLink1 = After waiting a few seconds, please
    update = update
    scheduleUpdateLink2 = the page.
    

    Tbh我不认为outputFormat可以与UiComponents一起使用,但只能使用文本 .

相关问题