为了账号安全,请及时绑定邮箱和手机立即绑定

使用<h:GraphicImage>或<img>标记从webapp/webtext/Deploy

使用<h:GraphicImage>或<img>标记从webapp/webtext/Deploy

交互式爱情 2019-06-14 16:46:50
使用<h:GraphicImage>或<img>标记从webapp/webtext/Deploy我需要使用JSF显示Web应用程序中部署文件夹之外的图像<h:graphicimage>标记或HTML<img>标签。我怎样才能做到这一点?
查看完整描述

3 回答

?
慕尼黑的夜晚无繁华

TA贡献1864条经验 获得超6个赞

为了达到你所需要的<h:graphicImage><img>标签,您需要创建一个Tomcatv 7别名,以便将外部路径映射到Web应用程序的上下文中。

要做到这一点,您需要指定web应用程序的上下文..最简单的方法是定义一个meta-INF/context.xml文件,其内容如下:

<Context path="/myapp" aliases="/images=/path/to/external/images"></Context>

然后,在重新启动Tomcat服务器之后,可以使用<h:graphicImage>或<img>标签如下:

<h:graphicImage value="/images/my-image.png">

<img src="/myapp/images/my-image.png">

*注意上下文路径对于标记是必需的,但对于


如果不需要通过HTTPGET方法获得映像,另一种可能的方法是使用PrimeFaces<p:fileDownload>标签(使用命令链接命令按钮标签-http POST法).

在你的法布雷特:

<h:form>
  <h:commandLink id="downloadLink" value="Download">  
    <p:fileDownload value="#{fileDownloader.getStream(file.path)}" />  </h:commandLink></h:form

在你的豆子里:

@ManagedBean@ApplicationScopepublic class FileDownloader {

    public StreamedContent getStream(String absPath) throws Exception {
        FileInputStream fis = new FileInputStream(absPath);
        BufferedInputStream bis = new BufferedInputStream(fis);
        StreamedContent content = new DefaultStreamedContent(bis);
        return content;
       }
    }}


查看完整回答
反对 回复 2019-06-14
?
阿晨1998

TA贡献2037条经验 获得超6个赞

在PrimeFaces中,您可以这样实现bean:

private StreamedContent image;public void setImage(StreamedContent image) {
    this.image = image;}public StreamedContent getImage() throws Exception {
    return image;}public void prepImage() throws Exception {File file = new File("/path/to/your/image.png");
    InputStream input = new FileInputStream(file);ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext();
    setImage(new DefaultStreamedContent(input,externalContext.getMimeType(file.getName()), file.getName()));}

在HTML Faclet中:

<body onload="#{yourBean.prepImage()}"></body> <p:graphicImage value="#{youyBean.image}" style="width:100%;height:100%" cache="false" >
</p:graphicImage>

我建议在GraphicImage组件中设置属性cache=“false”。


查看完整回答
反对 回复 2019-06-14
  • 3 回答
  • 0 关注
  • 959 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信