Upload file and insert description in DB
jmahle Feb 16, 2008 4:11 PMHi everyone,
I want to upload a file to a server and afterward write a description about the file and save the description and path to the file in the DB.
The uploading part is already working. Below is my GUI. First the user should browse and upload the file and then fill out the description and press save.
I have an action bean wich job is to upload the file. This part is working.
I have a bean to save the information (description and image path) in the database.
My code in the view look like this:
<ice:form enctype="multipart/form-data"> <ice:panelGrid styleClass="componentLayoutRootTable" columns="1"> <ice:panelGroup styleClass="formBorderHighlight" style="text-align:left"> <ice:inputFile progressListener="#{inputFileSave.progressMonitor.progress}" actionListener="#{inputFileSave.progressMonitor.action}"/> <ice:outputProgress id="pro" value="#{inputFileSave.percent}"/> </ice:panelGroup> </ice:panelGrid> </ice:form> <!-- Print uploaded filename and path to the screen --> <h:outputText id="imgNameId" value="#{inputFileSave.fileName}"/> <h:outputText id="imgPathId" value="#{inputFileSave.filePath}"/> <ice:form id="imageEditForm"> <ice:panelGroup id="editimageId" styleClass="formBorderHighlight"> <s:decorate id="descriptionDecoration" template="layout/edit.xhtml"> <ui:define name="label">Description</ui:define> <h:inputText id="description" required="false" value="#{imageHome.instance.description}"/> </s:decorate> <div style="clear:both"/> </ice:panelGroup> <div class="actionButtons"> <h:commandButton id="editimageTypeHomesave" value="Save" action="#{imageHome.persist}" rendered="#{!imageHome.managed}"/> <h:commandButton id="editimageTypeHomeupdate" value="Save" action="#{imageHome.update}" rendered="#{imageHome.managed}"/> <h:commandButton id="editimageHomedelete" value="Delete" action="#{imageHome.remove}" immediate="true" rendered="#{imageHome.managed}"/> <s:button propagation="end" id="editimageTypeHomedone" value="Done" view="/imageList.xhtml"/> </div> </ice:form>
My problem is how to I pass the path information from InputFileSaveAction to the ImageHome bean in a clever way? I have tried to set the @Out and @In annotation on the beans, but without luck. I have tried to find any examples on the net. Im just started using JSF and Seam, so maybe this is really simple?
Thanks, regards
Jacob
Here is the code from the two beans:
@Name("imageHome") @Scope(ScopeType.SESSION) public class ImageHome extends EntityHome<Image> { @RequestParameter Integer imageId; @Factory("imageType") public Image initImage() { return getInstance(); } @In(required=true) private String filePath = ""; @Override public Object getId() { if (imageId==null) { return super.getId(); } else { return imageId; } } @Override public void create() { super.create(); } @Override public String persist() { instance.setPath(filePath); return super.persist(); } }
@Stateful @Name("inputFileSave") @Local(InputFileSave.class) @Remote(InputFileSave.class) @Scope(ScopeType.SESSION) public class InputFileSaveAction implements Renderable, InputFileSave { private @Logger Log log; private int percent = -1; private File file = null; private transient PersistentFacesState state = PersistentFacesState.getInstance(); private String fileName = ""; @Out(required=true) private String filePath = ""; private String contentType = ""; private InnerProgressMonitor pmImpl = new InnerProgressMonitor(); public InputFileSaveAction() { pmImpl = new InnerProgressMonitor(); state = PersistentFacesState.getInstance(); } public PersistentFacesState getState() { return state; } public void renderingException(RenderingException re) { log.error("Exception : " + re); } public void setPercent(int percent) { this.percent = percent; } public int getPercent() { return percent; } public void setFile(File file) { this.file = file; } public File getFile() { return file; } public InnerProgressMonitor getProgressMonitor() { return pmImpl; } public InnerProgressMonitor getActionMonitor() { return pmImpl; } public void setFileName(String fileName) { this.fileName = fileName; } public String getFileName() { return fileName; } public String getFilePath() { return filePath; } public void setFilePath(String filePath) { this.filePath = filePath; } public void setContentType(String contentType) { this.contentType = contentType; } public String getContentType() { return contentType; } @Remove @Destroy public void destroy() { log.info("Destroying "); } public class InnerProgressMonitor implements Serializable { public void progress(EventObject event) { com.icesoft.faces.component.inputfile.InputFile file = (com.icesoft.faces.component.inputfile.InputFile) event.getSource(); int percent = file.getFileInfo().getPercent(); InputFileSaveAction.this.setPercent(percent); InputFileSaveAction.this.setFile(file.getFile()); log.info("Progress - Percent: " + percent); try { state.execute(); state.render(); } catch (RenderingException re ) { System.out.println("Rendering exception : " + re); re.printStackTrace(); } } public String action(ActionEvent event) { com.icesoft.faces.component.inputfile.InputFile inputFile = (com.icesoft.faces.component.inputfile.InputFile) event.getSource(); if (inputFile.getStatus() == com.icesoft.faces.component.inputfile.InputFile.SAVED) { InputFileSaveAction.this.setFileName(inputFile.getFileInfo().getFileName()); InputFileSaveAction.this.setContentType(inputFile.getFileInfo().getContentType()); InputFileSaveAction.this.setFile(inputFile.getFile()); InputFileSaveAction.this.setFilePath(inputFile.getFileInfo().getPhysicalPath()); log.info("File uploaded: " + inputFile.getFileInfo().getFileName()); } if (inputFile.getStatus() == com.icesoft.faces.component.inputfile.InputFile.INVALID) { inputFile.getFileInfo().getException().printStackTrace(); } if (inputFile.getStatus() == com.icesoft.faces.component.inputfile.InputFile.SIZE_LIMIT_EXCEEDED) { inputFile.getFileInfo().getException().printStackTrace(); } if (inputFile.getStatus() == com.icesoft.faces.component.inputfile.InputFile.UNKNOWN_SIZE) { inputFile.getFileInfo().getException().printStackTrace(); } return null; } } }