假设我像这样指定一个outputText组件:
<h:outputText value="#{ManagedBean.someProperty}"/>
Run Code Online (Sandbox Code Playgroud)
如果我在someProperty调用getter for时打印一条日志消息并加载页面,那么注意每个请求多次调用getter是很容易的(在我的情况下发生了两次或三次):
DEBUG 2010-01-18 23:31:40,104 (ManagedBean.java:13) - Getting some property
DEBUG 2010-01-18 23:31:40,104 (ManagedBean.java:13) - Getting some property
Run Code Online (Sandbox Code Playgroud)
如果someProperty计算的值很昂贵,这可能是一个问题.
我用Google搜索了一下,认为这是一个已知问题.一个解决方法是包括一个检查,看看它是否已经计算过:
private String someProperty;
public String getSomeProperty() {
if (this.someProperty == null) {
this.someProperty = this.calculatePropertyValue();
}
return this.someProperty;
}
Run Code Online (Sandbox Code Playgroud)
这个问题的主要问题是你得到大量的样板代码,更不用说你可能不需要的私有变量了.
这种方法有哪些替代方案?没有那么多不必要的代码,有没有办法实现这一目标?有没有办法阻止JSF以这种方式行事?
感谢您的输入!
我需要使用JSF <h:graphicimage>标记或HTML <img>标记在Web应用程序中显示驻留在deploy文件夹之外的图像.我怎样才能做到这一点?
我在JBoss 7.1.1上使用PrimeFaces 3.2.
我试图在MySQL数据库中显示存储在BLOB中的图像<ui:repeat>.图像存储在a中byte[],然后转换StreamedContent为如下:
InputStream stream = new ByteArrayInputStream(ingredient.getImage());
ingredient.setJsfImage(new DefaultStreamedContent(stream, "image/jpg"));
Run Code Online (Sandbox Code Playgroud)
然后我试图在Facelet中显示它如下:
<ui:repeat var="ingredient" value="#{formBean.ingredientResultSet}">
<p:panel id="resultsPanel" header="#{ingredient.location.shopName}">
<p:graphicImage value="#{ingredient.jsfImage}" alt="No picture set" />
...
Run Code Online (Sandbox Code Playgroud)
但是,在加载页面时,我在JBoss中收到以下错误:
SEVERE [org.primefaces.application.PrimeResourceHandler](http - 127.0.0.1-8080-12)流式动态资源出错.
这是怎么造成的,我该如何解决?
我想使用PrimeFaces数据表从数据库动态加载图像.代码如下所示,基于此PF论坛主题:
<p:dataTable id="tablaInventario" var="inv" value="#{registrarPedidoController.inventarioList}" paginator="true" rows="10"
selection="#{registrarPedidoController.inventarioSelected}" selectionMode="single"
update="tablaInventario tablaDetalle total totalDesc" dblClickSelect="false" paginatorPosition="bottom">
<p:column sortBy="producto.codigo" filterBy="producto.codigo">
<f:facet name="header">#{msg.codigo}</f:facet>
#{inv.producto.codProducto}
</p:column>
<p:column>
<f:facet name="header">Foto</f:facet>
<p:graphicImage id="photo" value="#{registrarPedidoController.streamedImageById}" cache="FALSE">
<f:param name="inv" value="#{inv.id}" />
</p:graphicImage>
</p:column>
</p:dataTable>
Run Code Online (Sandbox Code Playgroud)
同
public StreamedContent getStreamedImageById() {
DefaultStreamedContent image = null;
String get = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("inv");
System.out.println("[Param]: " + get); // This prints null.
Long id = new Long(get);
List<Inventario> listInventarios = controladorRegistrarPedido.listInventarios();
for (Inventario i : listInventarios) {
if (i.getId().compareTo(id) == 0) {
byte[] …Run Code Online (Sandbox Code Playgroud) 我正在使用streamedcontent渲染从JSF发送的字节,然后将其作为流内容发送回如下:
<p:graphicImage value="#{manage.bytesToStreamedContent(car.image)}"/>
Run Code Online (Sandbox Code Playgroud)
其中image是存储在数据库中的图像的字节数组
支持豆:
public StreamedContent bytesToStreamedContent(byte[] bytes)
{
InputStream is = new ByteArrayInputStream(bytes);
StreamedContent image = new DefaultStreamedContent (is, "image/jpeg");
return image;
}
Run Code Online (Sandbox Code Playgroud)
但我没有在JSF页面中获取图像.我在服务器日志中显示此消息:
警告:JSF1091:找不到文件dynamiccontent的mime类型.要解决此问题,请将mime类型映射添加到应用程序web.xml.
和:
SEVERE: Error in streaming dynamic resource.
Run Code Online (Sandbox Code Playgroud)
你可以帮帮我吗,我找不到有关这个问题的任何有用的信息
PS:
我使用以下库:
Mojarra 2.1.3
PrimeFaces 3.1.1
Glassfish 3.1
我正在使用Primefaces
电话号码:fileDownload
下载不在类路径中的文件.
所以我将FileInputStream作为参数传递给DefaultStreamedContent.
当我的bean保存在@SessionScoped时,每件事都可以正常工作......,
但是
java.io.NotSerializableException:java.io.FileInputStream
当我将bean保存在@Viewscoped中时抛出.
我的代码:
DownloadBean.java
@ManagedBean
@ViewScoped
public class DownloadBean implements Serializable {
private StreamedContent dFile;
public StreamedContent getdFile() {
return dFile;
}
public void setdFile(StreamedContent dFile) {
this.dFile = dFile;
}
/**
* This Method will be called when download link is clicked
*/
public void downloadAction()
{
File tempFile = new File("C:/temp.txt");
try {
dFile = new DefaultStreamedContent(new FileInputStream(tempFile), new MimetypesFileTypeMap().getContentType(tempFile));
} catch …Run Code Online (Sandbox Code Playgroud) 我正在尝试显示在新浏览器窗口中打开的内联PDF.我有以下场景:
window.open打开新页面以显示PDF)在打开的页面上我只有p:media标签里面h:body的值指向StreamedContent:
现在,在该页面上我的PDF不会生成.在日志中我可以看到这两行:
org.primefaces.application.PrimeResourceHandler handleResourceRequest
SEVERE: Error in streaming dynamic resource. Expression cannot be null
Run Code Online (Sandbox Code Playgroud)
我开始调试并找出一些东西.
首先,我将断点添加到@PostConstruct我的RequestScopedbean的方法中.有趣的是,断点达到了两次,并且在PDF显示完美之后让我大吃一惊?!
经过一些调试后,PrimeResourceHandler我发现在某些情况下ValueExpression没有计算,实际上它会抛出NullPointerException,并且在调试时我再次看到发送了两个请求,第二个请求失败,因为dynamicContentId在第一个请求中删除了,第二个调用handleResourceRequest没有有意义.
通过Firebug我可以看到两个请求,第一个是PDF数据,第二个也是内容类型application/pdf但是空,大小为0.
xhtml页面:
<html>
<h:head></h:head>
<h:body>
<p:media value="#{reportBean.streamedContent}" player="pdf" width="500" height="500"/>
</h:body>
</html>
Run Code Online (Sandbox Code Playgroud)
支持豆:
@RequestScoped
public class StampaListeBackingBean implements Serializable {
private static final long serialVersionUID = 1L;
private StreamedContent streamedContent;
@PostConstruct
public void init() {
Map<String, Object> session = …Run Code Online (Sandbox Code Playgroud) 我用它<p:media>来显示静态PDF内容.
<p:media value="/resource/test.pdf"
width="100%" height="300px" player="pdf">
</p:media>
Run Code Online (Sandbox Code Playgroud)
如何更改它以显示动态内容?
我正在使用PrimeFaces 5.3 <p:fileUpload>上传PNG图像,我想<p:graphicImage>在保存到数据库之前显示它的预览.
这是一个MCVE:
<h:form enctype="multipart/form-data">
<p:fileUpload value="#{bean.uploadedFile}" mode="simple" />
<p:graphicImage value="#{bean.image}" />
<p:commandButton action="#{bean.preview}" ajax="false" value="Preview" />
</h:form>
Run Code Online (Sandbox Code Playgroud)
private UploadedFile uploadedFile;
public UploadedFile getUploadedFile() {
return uploadedFile;
}
public void setUploadedFile(UploadedFile uploadedFile) {
this.uploadedFile = uploadedFile;
}
public void preview() {
// NOOP for now.
}
public StreamedContent getImage() {
if (uploadedFile == null) {
return new DefaultStreamedContent();
} else {
return new DefaultStreamedContent(new ByteArrayInputStream(uploadedFile.getContents()), "image/png");
}
}
Run Code Online (Sandbox Code Playgroud)
在辅助bean上没有发生错误,图像将不会加载并显示在前端.客户端提到图像返回404未找到错误.
重要提示:自2015年4月8日发布的PrimeFaces 5.2 final(社区发布)以来,此问题已得到修复.因此,如果您碰巧使用该版本或更新版本,则无需使用临时解决方法.
现在可以安全地修改先前给出的示例,如下所示.
public StreamedContent getImage() throws IOException {
FacesContext context = FacesContext.getCurrentInstance();
if (context.getCurrentPhaseId() == PhaseId.RENDER_RESPONSE) {
return new DefaultStreamedContent();
} else {
String id = context.getExternalContext().getRequestParameterMap().get("id");
byte[] bytes = Utils.isNumber(id) ? service.findImageById(Long.parseLong(id)) : null;
return bytes == null ? null : new DefaultStreamedContent(new ByteArrayInputStream(bytes));
}
}
Run Code Online (Sandbox Code Playgroud)
LONGBLOB在我厌倦了操作/管理存储在磁盘文件系统中的图像之后,我已经以BLOB()的形式将图像移动到数据库(MySQL).
因此,我正在<p:dataTable>如下显示图像(从这里公然复制:)).
<p:column headerText="Header">
<p:graphicImage value="#{bannerBean.image}" height="200" width="200">
<f:param name="id" value="#{row.bannerId}"/>
</p:graphicImage>
<p:column>
Run Code Online (Sandbox Code Playgroud)
检索图像的bean如下所示.
@ManagedBean
@ApplicationScoped
public …Run Code Online (Sandbox Code Playgroud) jsf ×8
primefaces ×8
jsf-2 ×5
graphicimage ×3
image ×2
media ×2
blob ×1
datatable ×1
el ×1
file-upload ×1
getter ×1
jsf-2.2 ×1
pdf ×1
performance ×1
preview ×1
stream ×1
uirepeat ×1