seam中另一种上传下载的实现思路

本文详细介绍了如何使用Seam框架构建Seam Entity类来管理文件上传,包括实现文件上传表单、展示上传文件详情及提供下载功能。通过创建Attachment类并配置相关注解,实现了文件的基本CRUD操作。同时,展示了如何创建相应的XHTML页面来展示文件信息,并提供了下载链接。此外,还介绍了下载附件组件的实现以及如何在页面中进行错误处理。最终,整合了前端页面与后端逻辑,实现了文件上传、显示和下载的一站式解决方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

来自:blog.youkuaiyun.com/bq_cui/article/details/3860416

开始:

用'seam create-entity'命令,或用 JBoss Tools ,在项目上单击右键,然后"New -> Seam Entity" ,Entity 名为"Attachment"。我们仅用部分生成代码,只是为了在正确的位置建立相应文件,这样可以不必担心文件位置。

 

下一步,修改 Attachment.java :

 

[java]  view plain copy
  1. import javax.persistence.Basic;  
  2. import javax.persistence.Column;  
  3. import javax.persistence.Entity;  
  4. import javax.persistence.FetchType;  
  5. import javax.persistence.GeneratedValue;  
  6. import javax.persistence.Id;  
  7. import javax.persistence.Lob;  
  8.   
  9. @Entity  
  10. public class Attachment implements java.io.Serializable {  
  11.   
  12.     @Id  
  13.     @GeneratedValue  
  14.     private Long id;  
  15.     public Long getId() { return this.id; }  
  16.     public void setId(Long id) { this.id = id; }  
  17.       
  18.     private String name;  
  19.     public String getName() { return this.name; }  
  20.     public void setName(String name) { this.name = name; }  
  21.       
  22.     private long size;  
  23.     public long getSize() { return this.size; }  
  24.     public void setSize(long size) { this.size = size; }  
  25.       
  26.     private String contentType;  
  27.     public String getContentType() { return this.contentType; }  
  28.     public void setContentType(String contentType) { this.contentType = contentType; }  
  29.       
  30.     @Lob  
  31.     @Column(length = 2147483647)  
  32.     @Basic(fetch = FetchType.LAZY)  
  33.     private byte[] data;  
  34.     public byte[] getData() { return this.data; }  
  35.     public void setData(byte[] data) { this.data = data; }  
  36.       
  37. }  

 

 

现在它已经包含了与s:fileUpload对应的全部字段。

打开 attachment.xhtml ,编辑内容为:

 

[xhtml]  view plain copy
  1. <!DOCTYPE composition PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"   
  2.                              "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  3. <ui:composition xmlns="http://www.w3.org/1999/xhtml"  
  4.     xmlns:s="http://jboss.com/products/seam/taglib"  
  5.     xmlns:ui="http://java.sun.com/jsf/facelets"  
  6.     xmlns:f="http://java.sun.com/jsf/core"  
  7.     xmlns:h="http://java.sun.com/jsf/html"  
  8.     xmlns:rich="http://richfaces.org/rich" template="layout/template.xhtml">  
  9.   
  10.     <ui:define name="body">  
  11.   
  12.         <h:messages globalOnly="true" styleClass="message" />  
  13.   
  14.         <h:form enctype="multipart/form-data">  
  15.   
  16.             <rich:panel>  
  17.                 <f:facet name="header">Upload Attachment</f:facet>  
  18.   
  19.                 <s:decorate id="fileUploadDecoration" template="layout/edit.xhtml">  
  20.                     <ui:define name="label">Attachment</ui:define>  
  21.                     <s:fileUpload id="file"   
  22.                               data="#{attachmentHome.instance.data}"  
  23.                               contentType="#{attachmentHome.instance.contentType}"  
  24.                               fileName="#{attachmentHome.instance.name}"  
  25.                               fileSize="#{attachmentHome.instance.size}" />  
  26.                 </s:decorate>  
  27.                   
  28.                 <s:decorate id="nameDecoration" template="layout/display.xhtml">  
  29.                     <ui:define name="label">Name</ui:define>  
  30.                     <h:outputText value="#{attachmentHome.instance.name}"/>  
  31.                 </s:decorate>  
  32.                   
  33.                 <s:decorate id="contentTypeDecoration" template="layout/display.xhtml">  
  34.                     <ui:define name="label">Content Type</ui:define>  
  35.                     <h:outputText value="#{attachmentHome.instance.contentType}"/>  
  36.                 </s:decorate>  
  37.                   
  38.                 <s:decorate id="sizeDecoration" template="layout/display.xhtml">  
  39.                     <ui:define name="label">Size</ui:define>  
  40.                     <h:outputText value="#{attachmentHome.instance.size}"/>  
  41.                 </s:decorate>  
  42.                   
  43.                 <div style="clear: both" mce_style="clear: both" />  
  44.             </rich:panel>  
  45.               
  46.             <div class="actionButtons">  
  47.                 <h:commandButton value="Upload"   
  48.                          action="#{attachmentHome.persist}"    
  49.                          rendered="#{!attachmentHome.managed}"/>  
  50.                 <h:commandButton value="Delete"   
  51.                          action="#{attachmentHome.remove}"  
  52.                          immediate="true"    
  53.                          rendered="#{attachmentHome.managed}">  
  54.                     <s:conversationPropagation type="end" />  
  55.                 </h:commandButton>  
  56.                 <s:button propagation="end"   
  57.                           id="done"   
  58.                           value="Done"  
  59.                           view="/attachmentList.xhtml"/>  
  60.             </div>  
  61.   
  62.         </h:form>  
  63.   
  64.     </ui:define>  
  65.   
  66. </ui:composition>  

 

 

以上代码与 CRUD 页面的主要区别是,字段是输出文本而非输入,因为字段值由 s:fileUpload component 自动确定。这里的代码你可以随便修改。

 

继续修改 attachmentList.xhtml ,使其可以显示上传文件的属性,并且允许下载:

[xhtml]  view plain copy
  1. <!DOCTYPE composition PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"   
  2.                              "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  3. <ui:composition xmlns="http://www.w3.org/1999/xhtml"  
  4.                 xmlns:s="http://jboss.com/products/seam/taglib"  
  5.                 xmlns:ui="http://java.sun.com/jsf/facelets"  
  6.                 xmlns:f="http://java.sun.com/jsf/core"  
  7.                 xmlns:h="http://java.sun.com/jsf/html"  
  8.                 xmlns:rich="http://richfaces.org/rich"  
  9.                 template="layout/template.xhtml">  
  10.                          
  11.     <ui:define name="body">  
  12.           
  13.         <h:messages globalOnly="true" styleClass="message"/>  
  14.           
  15.         <rich:panel>  
  16.             <f:facet name="header">Files Uploaded</f:facet>  
  17.               
  18.             <div class="results">  
  19.               
  20.                 <h:outputText value="No files exists" rendered="#{empty attachmentList.resultList}"/>  
  21.                          
  22.                 <rich:dataTable id="attachmentList"   
  23.                              var="attachment"  
  24.                              value="#{attachmentList.resultList}"   
  25.                              rendered="#{not empty attachmentList.resultList}">  
  26.                     <rich:column>  
  27.                         <f:facet name="header">File Name</f:facet>  
  28.                         <s:link value="#{attachment.name}" view="/attachment.xhtml">  
  29.                             <f:param name="attachmentId" value="#{attachment.id}" />  
  30.                         </s:link>  
  31.                     </rich:column>  
  32.                     <rich:column>  
  33.                         <f:facet name="header">Content Type</f:facet>  
  34.                         #{attachment.contentType}  
  35.                     </rich:column>  
  36.                     <rich:column>  
  37.                         <f:facet name="header">Size(bytes)</f:facet>  
  38.                         #{attachment.size}  
  39.                     </rich:column>  
  40.                     <rich:column>  
  41.                         <f:facet name="header">Action</f:facet>  
  42.                         <s:link value="Download" action="#{downloadAttachment.download}">  
  43.                             <f:param name="attachmentId" value="${attachment.id}" />  
  44.                         </s:link>  
  45.                     </rich:column>  
  46.                 </rich:dataTable>  
  47.                   
  48.             </div>  
  49.               
  50.         </rich:panel>  
  51.           
  52.         <div class="actionButtons">  
  53.             <s:button id="done"   
  54.                    value="Create attachment"  
  55.                     view="/attachment.xhtml"/>               
  56.         </div>  
  57.           
  58.     </ui:define>  
  59.   
  60. </ui:composition>  

 

 

这里有一个名为 downloadAttachment 的Component,引用了我们还没创建的内容,所以,我们先来建立它。你可以用EJB3 或者 Seam pojo,下面的工作好简单,我们继续。DownloadAttachment 组件是本文的核心,所以要注意了:

 

 

[java]  view plain copy
  1. @Name("downloadAttachment")  
  2. public class DownloadAttachment {  
  3.       
  4.     @Logger  
  5.     private Log log;  
  6.       
  7.     @In  
  8.     private EntityManager entityManager;  
  9.       
  10.     @In(value="#{facesContext.externalContext}")  
  11.     private ExternalContext extCtx;  
  12.       
  13.     @In(value="#{facesContext}")  
  14.     FacesContext facesContext;  
  15.       
  16.     @RequestParameter  
  17.     private Long attachmentId;  
  18.       
  19.     public String download() {  
  20.         Attachment attachment = entityManager.find(Attachment.class, attachmentId);  
  21.         HttpServletResponse response = (HttpServletResponse)extCtx.getResponse();  
  22.         response.setContentType(attachment.getContentType());  
  23.                 response.addHeader("Content-disposition""attachment; filename=/"" + attachment.getName() +"/"");  
  24.         try {  
  25.             ServletOutputStream os = response.getOutputStream();  
  26.             os.write(attachment.getData());  
  27.             os.flush();  
  28.             os.close();  
  29.             facesContext.responseComplete();  
  30.         } catch(Exception e) {  
  31.             log.error("/nFailure : " + e.toString() + "/n");  
  32.         }  
  33.   
  34.         return null;  
  35.     }  
  36. }  

为圆满完成任务,这里展示AttachmentHome.java 和 AttachmentList.java代码:

 

 

[java]  view plain copy
  1. import org.jboss.seam.annotations.Name;  
  2. import org.jboss.seam.framework.EntityQuery;  
  3.   
  4. @Name("attachmentList")  
  5. public class AttachmentList extends EntityQuery  
  6. {  
  7.     @Override  
  8.     public String getEjbql()   
  9.     {   
  10.         return "select attachment from Attachment attachment";  
  11.     }  
  12. }  

 

[java]  view plain copy
  1. import org.jboss.seam.annotations.Name;  
  2. import org.jboss.seam.annotations.Begin;  
  3. import org.jboss.seam.annotations.web.RequestParameter;  
  4. import org.jboss.seam.framework.EntityHome;  
  5.   
  6. import org.domain.knowledgebase.entity.Attachment;  
  7.   
  8. @Name("attachmentHome")  
  9. public class AttachmentHome extends EntityHome<Attachment> {  
  10.   
  11.     @RequestParameter   
  12.     Long attachmentId;  
  13.       
  14.     @Override  
  15.     public Object getId() {   
  16.         if (attachmentId==null) {  
  17.             return super.getId();  
  18.         } else {  
  19.             return attachmentId;  
  20.         }  
  21.     }  
  22.       
  23.     @Override @Begin  
  24.     public void create() {  
  25.         super.create();  
  26.     }  
  27.       
  28. }  

 

 

剩下的事情就是定义出错处理以及在components.xml文件中multipart filter。

在pages.xml中增加:

[xhtml]  view plain copy
  1. <exception class="org.jboss.seam.web.FileUploadException">  
  2.   <redirect view-id="/error.xhtml">  
  3.     <message>#{org.jboss.seam.handledException.message}</message>  
  4.   </redirect>  
  5. </exception>  

 

components.xml中增加:

 

[xhtml]  view plain copy
  1. xmlns:web="http://jboss.com/products/seam/web"  
  2. ...  
  3. http://jboss.com/products/seam/web http://jboss.com/products/seam/web-2.0.xsd  
  4. ...  
  5. <web:multipart-filter create-temp-files="true"  
  6.                       max-request-size="1000000"   
  7.                       url-pattern="*.seam" />  

 

可以将以上内容结合到你自己的web程序。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值