来自:blog.youkuaiyun.com/bq_cui/article/details/3860416
开始:
用'seam create-entity'命令,或用 JBoss Tools ,在项目上单击右键,然后"New -> Seam Entity" ,Entity 名为"Attachment"。我们仅用部分生成代码,只是为了在正确的位置建立相应文件,这样可以不必担心文件位置。
下一步,修改 Attachment.java :
- import javax.persistence.Basic;
- import javax.persistence.Column;
- import javax.persistence.Entity;
- import javax.persistence.FetchType;
- import javax.persistence.GeneratedValue;
- import javax.persistence.Id;
- import javax.persistence.Lob;
- @Entity
- public class Attachment implements java.io.Serializable {
- @Id
- @GeneratedValue
- private Long id;
- public Long getId() { return this.id; }
- public void setId(Long id) { this.id = id; }
- private String name;
- public String getName() { return this.name; }
- public void setName(String name) { this.name = name; }
- private long size;
- public long getSize() { return this.size; }
- public void setSize(long size) { this.size = size; }
- private String contentType;
- public String getContentType() { return this.contentType; }
- public void setContentType(String contentType) { this.contentType = contentType; }
- @Lob
- @Column(length = 2147483647)
- @Basic(fetch = FetchType.LAZY)
- private byte[] data;
- public byte[] getData() { return this.data; }
- public void setData(byte[] data) { this.data = data; }
- }
现在它已经包含了与s:fileUpload对应的全部字段。
打开 attachment.xhtml ,编辑内容为:
- <!DOCTYPE composition PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
- "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <ui:composition xmlns="http://www.w3.org/1999/xhtml"
- xmlns:s="http://jboss.com/products/seam/taglib"
- xmlns:ui="http://java.sun.com/jsf/facelets"
- xmlns:f="http://java.sun.com/jsf/core"
- xmlns:h="http://java.sun.com/jsf/html"
- xmlns:rich="http://richfaces.org/rich" template="layout/template.xhtml">
- <ui:define name="body">
- <h:messages globalOnly="true" styleClass="message" />
- <h:form enctype="multipart/form-data">
- <rich:panel>
- <f:facet name="header">Upload Attachment</f:facet>
- <s:decorate id="fileUploadDecoration" template="layout/edit.xhtml">
- <ui:define name="label">Attachment</ui:define>
- <s:fileUpload id="file"
- data="#{attachmentHome.instance.data}"
- contentType="#{attachmentHome.instance.contentType}"
- fileName="#{attachmentHome.instance.name}"
- fileSize="#{attachmentHome.instance.size}" />
- </s:decorate>
- <s:decorate id="nameDecoration" template="layout/display.xhtml">
- <ui:define name="label">Name</ui:define>
- <h:outputText value="#{attachmentHome.instance.name}"/>
- </s:decorate>
- <s:decorate id="contentTypeDecoration" template="layout/display.xhtml">
- <ui:define name="label">Content Type</ui:define>
- <h:outputText value="#{attachmentHome.instance.contentType}"/>
- </s:decorate>
- <s:decorate id="sizeDecoration" template="layout/display.xhtml">
- <ui:define name="label">Size</ui:define>
- <h:outputText value="#{attachmentHome.instance.size}"/>
- </s:decorate>
- <div style="clear: both" mce_style="clear: both" />
- </rich:panel>
- <div class="actionButtons">
- <h:commandButton value="Upload"
- action="#{attachmentHome.persist}"
- rendered="#{!attachmentHome.managed}"/>
- <h:commandButton value="Delete"
- action="#{attachmentHome.remove}"
- immediate="true"
- rendered="#{attachmentHome.managed}">
- <s:conversationPropagation type="end" />
- </h:commandButton>
- <s:button propagation="end"
- id="done"
- value="Done"
- view="/attachmentList.xhtml"/>
- </div>
- </h:form>
- </ui:define>
- </ui:composition>
以上代码与 CRUD 页面的主要区别是,字段是输出文本而非输入,因为字段值由 s:fileUpload component 自动确定。这里的代码你可以随便修改。
继续修改 attachmentList.xhtml ,使其可以显示上传文件的属性,并且允许下载:
- <!DOCTYPE composition PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
- "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <ui:composition xmlns="http://www.w3.org/1999/xhtml"
- xmlns:s="http://jboss.com/products/seam/taglib"
- xmlns:ui="http://java.sun.com/jsf/facelets"
- xmlns:f="http://java.sun.com/jsf/core"
- xmlns:h="http://java.sun.com/jsf/html"
- xmlns:rich="http://richfaces.org/rich"
- template="layout/template.xhtml">
- <ui:define name="body">
- <h:messages globalOnly="true" styleClass="message"/>
- <rich:panel>
- <f:facet name="header">Files Uploaded</f:facet>
- <div class="results">
- <h:outputText value="No files exists" rendered="#{empty attachmentList.resultList}"/>
- <rich:dataTable id="attachmentList"
- var="attachment"
- value="#{attachmentList.resultList}"
- rendered="#{not empty attachmentList.resultList}">
- <rich:column>
- <f:facet name="header">File Name</f:facet>
- <s:link value="#{attachment.name}" view="/attachment.xhtml">
- <f:param name="attachmentId" value="#{attachment.id}" />
- </s:link>
- </rich:column>
- <rich:column>
- <f:facet name="header">Content Type</f:facet>
- #{attachment.contentType}
- </rich:column>
- <rich:column>
- <f:facet name="header">Size(bytes)</f:facet>
- #{attachment.size}
- </rich:column>
- <rich:column>
- <f:facet name="header">Action</f:facet>
- <s:link value="Download" action="#{downloadAttachment.download}">
- <f:param name="attachmentId" value="${attachment.id}" />
- </s:link>
- </rich:column>
- </rich:dataTable>
- </div>
- </rich:panel>
- <div class="actionButtons">
- <s:button id="done"
- value="Create attachment"
- view="/attachment.xhtml"/>
- </div>
- </ui:define>
- </ui:composition>
这里有一个名为 downloadAttachment 的Component,引用了我们还没创建的内容,所以,我们先来建立它。你可以用EJB3 或者 Seam pojo,下面的工作好简单,我们继续。DownloadAttachment 组件是本文的核心,所以要注意了:
- @Name("downloadAttachment")
- public class DownloadAttachment {
- @Logger
- private Log log;
- @In
- private EntityManager entityManager;
- @In(value="#{facesContext.externalContext}")
- private ExternalContext extCtx;
- @In(value="#{facesContext}")
- FacesContext facesContext;
- @RequestParameter
- private Long attachmentId;
- public String download() {
- Attachment attachment = entityManager.find(Attachment.class, attachmentId);
- HttpServletResponse response = (HttpServletResponse)extCtx.getResponse();
- response.setContentType(attachment.getContentType());
- response.addHeader("Content-disposition", "attachment; filename=/"" + attachment.getName() +"/"");
- try {
- ServletOutputStream os = response.getOutputStream();
- os.write(attachment.getData());
- os.flush();
- os.close();
- facesContext.responseComplete();
- } catch(Exception e) {
- log.error("/nFailure : " + e.toString() + "/n");
- }
- return null;
- }
- }
为圆满完成任务,这里展示AttachmentHome.java 和 AttachmentList.java代码:
- import org.jboss.seam.annotations.Name;
- import org.jboss.seam.framework.EntityQuery;
- @Name("attachmentList")
- public class AttachmentList extends EntityQuery
- {
- @Override
- public String getEjbql()
- {
- return "select attachment from Attachment attachment";
- }
- }
- import org.jboss.seam.annotations.Name;
- import org.jboss.seam.annotations.Begin;
- import org.jboss.seam.annotations.web.RequestParameter;
- import org.jboss.seam.framework.EntityHome;
- import org.domain.knowledgebase.entity.Attachment;
- @Name("attachmentHome")
- public class AttachmentHome extends EntityHome<Attachment> {
- @RequestParameter
- Long attachmentId;
- @Override
- public Object getId() {
- if (attachmentId==null) {
- return super.getId();
- } else {
- return attachmentId;
- }
- }
- @Override @Begin
- public void create() {
- super.create();
- }
- }
剩下的事情就是定义出错处理以及在components.xml文件中multipart filter。
在pages.xml中增加:
- <exception class="org.jboss.seam.web.FileUploadException">
- <redirect view-id="/error.xhtml">
- <message>#{org.jboss.seam.handledException.message}</message>
- </redirect>
- </exception>
components.xml中增加:
- xmlns:web="http://jboss.com/products/seam/web"
- ...
- http://jboss.com/products/seam/web http://jboss.com/products/seam/web-2.0.xsd
- ...
- <web:multipart-filter create-temp-files="true"
- max-request-size="1000000"
- url-pattern="*.seam" />
可以将以上内容结合到你自己的web程序。