从班级到JSF页面显示ImageIcon? [英] Show ImageIcon from class to JSF page?

查看:73
本文介绍了从班级到JSF页面显示ImageIcon?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究JSF 2.0,并且在一个类中有一个ImageIcon.我可以显示此ImageIcon到JSF页面吗?

I'm studying JSF 2.0 and I have an ImageIcon in a class. Can I show this ImageIcon to JSF page?

推荐答案

正在发生一些重大的概念误解.

There's some major conceptual misunderstanding going on.

Java/JSF在Web服务器上运行并生成HTML. HTML在网络浏览器上运行,并且可以通过<link><script><img>元素中的(相对)URL引用CSS/JS/图像文件. ImageIcon是Swing的一部分,与这一切无关.

Java/JSF runs on webserver and produces HTML. HTML runs on webbrowser and can reference CSS/JS/image files by (relative) URLs in <link>, <script> and <img> elements. ImageIcon is part of Swing and has nothing to do with this all.

Swing根本不产生任何HTML.当您仍尝试在由HTTP请求调用的Java类(例如JSF支持bean类或什至是普通香草" servlet类)中以摇摆方式"运行它时,它将仅显示在屏幕上它是附加到运行Web服务器的物理计算机上的,而不是附加到运行Web浏览器的计算机上的(正是HTML运行的位置).

Swing does not produce any HTML at all. When you still attempt to run it "the Swing way" in a Java class which is invoked by a HTTP request, such as a JSF backing bean class or even a "plain vanilla" servlet class, then it would only be shown to the screen which is attached to the physical machine where the webserver runs, not the one where the webbrowser runs (which is exactly where HTML runs).

最终,您需要使用普通的HTML方式显示图标.也就是说,使用<img>元素,或者使用具有CSS background-image属性的块元素.无论哪种方式,他们都必须指向一个有效的URL,该URL返回一个图像(即,当最终用户在浏览器的地址栏中输入该URL的专有名称时,它应该完整地看到该图像).通常,这可以通过将图像文件放置在公共Web内容中(也可以在其中存储JSF(XHTML/JSP)页面的位置)来实现.

Ultimately, you need to show the icon by normal HTML means. That is, using the <img> element, or perhaps on a block element with a CSS background-image property. Either way, they have to point a valid URL which returns an image (that is, when the enduser enters exaclty that URL in the browser's addressbar, it should see the image in its entirety). Normally, this is to be achieved by placing the image file in the public webcontent (there where you also store your JSF (XHTML/JSP) pages.

在JSF中,您可以使用<h:graphicImage>组件处理HTML <img>元素.提供了以下webcontent文件夹结构

In JSF means, you can procude a HTML <img> element using <h:graphicImage> component. Provided the following webcontent folder structure

WebContent
 |-- META-INF
 |-- WEB-INF
 |-- resources
 |    `-- images
 |         `-- icon.png
 `-- index.xhtml

index.xhtml

<h:graphicImage name="images/icon.png" />

然后,最终用户只需打开index.xhtml就能看到所需的图像.

then the enduser should be able to see the desired image by just opening index.xhtml.

另一种方法是创建一个基于Swing的 applet webstart(JNLP)应用程序,该应用程序将图像显示为,最后通过HTML <applet><object>元素将其嵌入网页中.但这很笨拙.

An alternative is to create a Swing based applet or webstart (JNLP) application which displays the image as an ImageIcon and finally embed it via HTML <applet> or <object> element in the webpage. But that's plain clumsy.

这篇关于从班级到JSF页面显示ImageIcon?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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