如何使用OmniFaces在JSF应用程序中正确嵌入SVG图形

时间:2015-02-18 11:24:17

标签: jsf-2.2 omnifaces

这是How can I embed an SVG image using JSF/OmniFaces/PrimeFaces?

的后续行动

在那个问题中,我了解到OmniFaces-2.1快照支持通过它的o:graphicImage标签来使用InputStream / byte []。

Getting started with SVG graphics objects in JSF 2.0 pages中,我了解到标准的graphicImage标记只是呈现 img 标记,而SVG应该呈现为和 object 标记一样:<object type="image/svg+xml" data="#{bean.svgUrl}">

我的问题实际上是直接针对来自OmniFaces项目的BalusC:

不应该o:graphicImage以这种方式呈现SVG吗?我得到的只是img-tags而且他们不能在IE10 +中工作。

更新#2:这是一个扩展问题!我将高度设置为125并开始看到我的图形边缘。

更新#1:示例呈现的img标签:

这个基于静态SVG文件:<img height="25" alt="" src="/javax.faces.resource/images/Logo.svg.xhtml;jsessionid=cc0acf6def65ac5c127d09384977?ln=default">

这基于一个字节数组: <img src="data:image/svg+xml;base64,PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0iVVRGLTgiIHN0YW5kYWxvbmU9Im5vIj8+CjwhLS0gQ3JlYXRlZCB3aXRoIElua3NjYXBlIChodHRwOi8vd3d3Lmlua3NjYXBlLm9yZy8pIC0tPgoKPHN2ZwogICB4bWxuczpkYz0iaHR0cDovL3B1cmwub3JnL2RjL2VsZW1lbnRzLzEuMS8iCiAgIHhtbG5zOmNjPSJodHRwOi8vY3JlYXRpdmVjb21tb25zLm9yZy9ucyMiCiAgIHhtbG5zOnJkZj0iaHR0cDovL3d3dy53My5vcmcvMTk5OS8wMi8yMi1yZGYtc3ludGF4LW5zIyIKICAgeG1sbnM6c3ZnPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIKICAgeG1sbnM9Imh0dHA6Ly93d3cudzMub3JnLzIwMDAvc3ZnIgogICB4bWxuczppbmtzY2FwZT0iaHR0cDovL3d3dy5pbmtzY2FwZS5vcmcvbmFtZXNwYWNlcy9pbmtzY2FwZSIKICAgdmVy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Q7c3Ryb2tlLWxpbmVqb2luOm1pdGVyO3N0cm9rZS1taXRlcmxpbWl0OjQ7c3Ryb2tlLW9wYWNpdHk6MTtzdHJva2UtZGFzaGFycmF5Om5vbmUiIC8+PC9nPjwvZz48L3N2Zz4=" alt="100.1 - Farlig sving" title="100.1 - Farlig sving" width="40" style="margin-left: 3px; pointer-events: none;" height="40">

1 个答案:

答案 0 :(得分:0)

由于我发现这是一个SVG扩展问题,而且我似乎不是唯一一个遇到过这个问题的人,我会在这里发布我的解决方案。

简而言之,我

  • 检索SVG。
  • 使用JDOM解析它。
  • 根据指定的尺寸
  • 注入视图框(如果缺少)

这里是没有错误处理的Java代码:

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    InputStream is = execute(builder.buildGet(), InputStream.class);
    Document doc = new SAXBuilder().build(is);
    Element root = doc.getRootElement();
    if (root.getAttribute("viewBox") == null)
    {
            String width = root.getAttributeValue("width"), height =  root.getAttributeValue("height");
            root.setAttribute("viewBox", String.format("0 0 %s %s", width, height));
    }

    new XMLOutputter().output(doc, baos);
    return baos.toByteArray();