客户端 有任何的功能更整洁的或更多的可读国际化用户界面文本的标签有什么你能否则不使用JSF?

例如,与普通的JSF,使用h:outputFormat是一个非常详细的方式插入变量的消息。

澄清: 我知道,我可以添加一条消息的文件项目,看起来像:

label.widget.count = You have a total of {0} widgets.

和显示这种(如果我使用缝线):

<h:outputFormat value="#{messages['label.widget.count']}">
   <f:param value="#{widgetCount}"/>
</h:outputFormat>

但是,这是一个很大的混乱出一句话-只是事情,给JSF一个坏的名字。

有帮助吗?

解决方案

你可以创建你自己的脸标签的图书馆,以使它不那么详细,类似的东西:

<ph:i18n key="label.widget.count" p0="#{widgetCount}"/>

然后创建的标签库在你看dir:/components/ph.taglib.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE facelet-taglib PUBLIC "-//Sun Microsystems, Inc.//DTD Facelet Taglib 1.0//EN" "https://facelets.dev.java.net/source/browse/*checkout*/facelets/src/etc/facelet-taglib_1_0.dtd">

<facelet-taglib xmlns="http://java.sun.com/JSF/Facelet">
    <namespace>http://peterhilton.com/core</namespace>

    <tag>
        <tag-name>i18n</tag-name>
        <source>i18n.xhtml</source>
    </tag>

</facelet-taglib>

建立/组件/i18n.xhtml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<ui:composition xmlns="http://www.w3.org/1999/xhtml"
    xmlns:ui="http://java.sun.com/jsf/facelets"
        xmlns:h="http://java.sun.com/jsf/html"        
    xmlns:f="http://java.sun.com/jsf/core">

    <h:outputFormat value="#{messages[key]}">
            <!-- crude but it works -->
        <f:param value="#{p0}" />
        <f:param value="#{p1}" />
        <f:param value="#{p2}" />
        <f:param value="#{p3}" />
    </h:outputFormat>

</ui:composition>

你也许可以找到一个优雅的方式,通过参与一个小小的研究。

现在注册的新标签库中web.xml

<context-param>
<param-name>facelets.LIBRARIES</param-name>
<param-value>
        /components/ph.taglib.xml
    </param-value>
</context-param>

只是添加 xmlns:ph="http://peterhilton.com/core" 您的意见和你的所有设置!

其他提示

因为你使用缝, 你可以使用EL 在信息的文件。

财产:

label.widget.count = You have a total of #{widgetCount} widgets.

XHTML:

<h:outputFormat value="#{messages['label.widget.count']}" />

这仍然使用outputFormat,但较不详细。

我从来没有碰到另一种方式这样做的其他比outputFormat.这是不幸的是相当详细。

唯一其它的事情我可以建议是创造消息,在背豆然后输出,而不是messageFormat:.

在我的情况下,我们弹簧是因为综合与JSF(使用 MessageSourcePropertyResolver).然后,它很容易在你背豆获得的参数化的消息-你只需要知道哪个区域设置你的用户是中(同样,我已经得到了区域开一背豆属性,因此它是可以通过JSF或Java)。

我想参数--特别是在信-是有一件事JSF真的可以做的更好!

我一直在思考这个更大,它的发生,我认为我大概可以写我自己的JSTL功能,需要一个关键信息和数量可变的参数:

<h:outputText value="#{my:message('label.widget.count', widgetCount)}"/>

如果我消息的功能HTML编码输出结果之前,我甚至不需要使用h:outputText

#{my:message('label.widget.count', widgetCount)}

你可以使用缝插器:

<h:outputText value="#{interpolator.interpolate(messages['label.widget.count'], widgetCount)}"/>

它有@BypassInterceptors在这样的性能应该是确定。

你可以使用豆直接的,如果你插入的信息。

label.widget.count = You have a total of #{widgetCount} widgets.
label.welcome.message = Welcome to #{request.contextPath}!
label.welcome.url = Your path is ${pageContext.servletContext}.

${messages['label.widget.count']} 是enougth.

这一个伟大工程,使用弹簧:

package foo;

import javax.el.ELContext;
import javax.el.ELException;
import javax.el.ExpressionFactory;
import javax.el.ResourceBundleELResolver;
import javax.faces.context.FacesContext;

import org.springframework.web.jsf.el.SpringBeanFacesELResolver;

public class ELResolver extends SpringBeanFacesELResolver {
    private static final ExpressionFactory FACTORY = FacesContext
            .getCurrentInstance().getApplication().getExpressionFactory();
    private static final ResourceBundleELResolver RESOLVER = new ResourceBundleELResolver();

    @Override
    public Object getValue(ELContext elContext, Object base, Object property)
            throws ELException {
        Object result = super.getValue(elContext, base, property);
        if (result == null) {
            result = RESOLVER.getValue(elContext, base, property);
            if (result instanceof String) {
                String el = (String) result;
                if (el.contains("${") | el.contains("#{")) {
                    result = FACTORY.createValueExpression(elContext, el,
                            String.class).getValue(elContext);
                }
            }
        }
        return result;
    }
}

和...

你需要改变EL-解决程序在 faces-config.xmlorg.springframework.web.jsf.el.SpringBeanFacesELResolver

关于

<el-resolver>foo.ELResolver</el-resolver>

使用ResourceBundle和财产的文件。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top