EDITED
我使用<p:uploadfile>
将图片上传到我的文章的项目目录中,上传成功,但我无法立即查看图片,但刷新工作区后在我的文章中出现。
UPLOAD方法:
public void upload(FileUploadEvent event) {
try {
new FileUpload().copyFile(event.getFile().getFileName(), event
.getFile().getInputstream());
} catch (IOException e) {
e.printStackTrace();
}
}
COPYFILE方法:
public void copyFile(String fileName, InputStream in) {
try {
OutputStream out = new FileOutputStream(new File(destination
+ fileName));
int read = 0;
byte[] bytes = new byte[1024];
while ((read = in.read(bytes)) != -1) {
out.write(bytes, 0, read);
}
in.close();
out.flush();
out.close();
} catch (IOException e) {
System.out.println(e.getMessage());
}
}
.XHTML:[已编辑部分]
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html 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"
xmlns:p="http://primefaces.org/ui">
<h:head>
<style>
.topAligned {
vertical-align: top;
}
</style>
</h:head>
<h:body>
<p:tooltip />
<h:form id="form">
<p:growl id="notification" showDetail="true" />
<h:panelGrid columns="2" style="width:1220px;">
<h:outputText value="Nos Actualités :"
style="font-size: 30px;line-height: 40px;" />
<p:commandLink title="Ajouter" id="add" onclick="PF('dlg1').show();">
<h:graphicImage value="../img/add.png"
style="width:55px;height:55px;float:right;" />
</p:commandLink>
</h:panelGrid>
<div style="margin-left: 25px;">
<h:panelGrid columns="1">
<ui:repeat value="#{actualiteMBean.actualites}" var="actualite">
<h:outputText value="#{actualite.titreActualite}"
style="font-weight:bold;" />
<h:panelGrid columns="2" styleClass="topAligned"
style="display:block;" cellpadding="10">
<h:graphicImage value="#{actualite.imageActualite}"
style="width:150px; height:150px;" />
<h:panelGrid columns="1">
<div style="text-align: justify;">
<h:outputText value="#{actualite.descriptionActualite}" />
</div>
<h:panelGrid columns="2" style="float:right;">
<p:commandLink title="Modifier" id="modif"
actionListener="#{actualiteMBean.redirect(actualite)}">
<h:graphicImage value="../img/edit.png"
style="width:30px; height:30px;" />
</p:commandLink>
<p:commandLink title="Supprimer" update=":form" id="delete"
actionListener="#{actualiteMBean.delete(actualite)}">
<h:graphicImage value="../img/delete.jpg"
style="width:40px;height:33px;" />
<p:confirm header="Confirmation" message="etes vous sur?"
icon="ui-icon-alert" />
</p:commandLink>
</h:panelGrid>
</h:panelGrid>
</h:panelGrid>
<p:separator />
</ui:repeat>
</h:panelGrid>
</div>
</h:form>
<p:confirmDialog global="true" showEffect="fade" hideEffect="explode">
<p:commandButton value="Oui" type="button"
styleClass="ui-confirmdialog-yes" icon="ui-icon-check" />
<p:commandButton value="Non" type="button"
styleClass="ui-confirmdialog-no" icon="ui-icon-close" />
</p:confirmDialog>
<p:dialog widgetVar="dlg1" modal="true" resizable="true"
appendTo="@(body)" showEffect="slide" hideEffect="slide"
header="Ajouter une Nouvelle Actualité" showHeader="true">
<h:form id="form1">
<p:wizard widgetVar="wiz" style="width:900px;height:500px;">
<p:tab id="titre" title="titre d'actualite">
<p:panel header="Titre d'actualite">
<h:panelGrid columns="3" cellspacing="10">
<p:outputLabel value=" Titre :" />
<p:inputText id="tit"
value="#{actualiteMBean.nouvelleActualite.titreActualite}"
required="true" requiredMessage="Champ Obligatoir" />
<p:message for="tit" />
</h:panelGrid>
</p:panel>
</p:tab>
<p:tab id="image" title="image d'actualite">
<p:panel header="Image d'actualite">
<h:panelGrid columns="4" cellspacing="10">
<p:outputLabel value="Choisir Image :" />
<p:fileUpload id="up" multiple="true"
fileUploadListener="#{actualiteMBean.upload}"
allowTypes="/(\.|\/)(gif|jpe?g|png)$/" sizeLimit="100000"
description="Select Images" required="true"
requiredMessage="Image Obligatoire" update=":form1:message" />
<p:message for="up" />
<p:message for="up" display="Chargement reussi" id="message" />
</h:panelGrid>
</p:panel>
</p:tab>
<p:tab id="descrition" title="decsription d'actualite">
<p:panel header="Description d'actualite">
<h:panelGrid columnClasses="topAligned" columns="3"
cellspacing="10" style="margin:0 none;">
<p:outputLabel value=" Description :" />
<p:inputTextarea id="desc" cols="60" rows="10"
value="#{actualiteMBean.nouvelleActualite.descriptionActualite}"
required="true" requiredMessage="Champ Obligatoir" />
<p:message for="desc" />
</h:panelGrid>
</p:panel>
</p:tab>
<p:tab id="validation" title="validation">
<p:panel header="Validation">
<h:outputText
value="#{actualiteMBean.nouvelleActualite.titreActualite}" />
<h:panelGrid columns="2">
<h:graphicImage style="width:150px;height:150px;"
value="#{actualiteMBean.nouvelleActualite.imageActualite}" />
<h:outputText
value="#{actualiteMBean.nouvelleActualite.descriptionActualite}" />
</h:panelGrid>
<h:panelGrid columns="2">
<p:commandButton value="Valider" update=":form :form1"
icon="ui-icon-check" action="#{actualiteMBean.save}"
oncomplete="PF('dlg1').hide();wiz.loadStep (wiz.cfg.steps [0], true)" />
<p:commandButton value="Annuler" icon="ui-icon-cancel"
action="#{actualiteMBean.cancel}"
oncomplete="PF('dlg1').hide();wiz.loadStep (wiz.cfg.steps [0], true)"
update=":form1" />
</h:panelGrid>
</p:panel>
</p:tab>
</p:wizard>
</h:form>
</p:dialog>
</h:body>
</html>
问题图片:
在eclipse中刷新项目并重新启动tomcat之后:
我想要的是上面的结果,而不是每次手动重新调整项目,有没有办法以编程方式 ??