如何用seam实现文件上传

来源:互联网 发布:潮汕牛肉丸淘宝哪家好 编辑:程序博客网 时间:2024/04/30 03:15

 1.  需要两个seam组件。一个Tomahawk 和SLSB用来处理JSF action 的 "backing bean"。

1a. Backing Bean:

 

@Name("uploadBean")public class UploadBackingBean {    private UploadedFile file;        public void setFile(UploadedFile file) {        this.file = file;    }    @NotNull    public UploadedFile getFile() {        return this.file;    }    }

 

1b. Action 和它的接口文件 

@Stateless@Name("upload")@Interceptors(SeamInterceptor.class)public class UploadAction implements Upload {    private Logger logger;    /**     * O arquivo que foi enviado.     */    @In    private UploadBackingBean uploadBean;    @In    private FacesContext facesContext;    @PostConstruct    public void init() {        logger = Logger.getLogger(this.getClass());        logger.debug("init()");    }    public String upload() {        logger.debug("upload()");        UploadedFile file = uploadBean.getFile();                logger.debug("Abrindo o arquivo como DOM4J");        logger.debug(file.getName());        facesContext.addMessage(null, new FacesMessage("Nome do arquivo: "                + file.getName()));        return "success";    }}

 

 

@Localpublic interface Upload {    public String upload();    }

 

2. 如果你想通过facelets存取tomahawk upload 组件,需要定义一个tomahawk.taglib.xml 文件。它应被包含在.war 包的WEB-INF 目录。

 

<?xml version="1.0"?><!DOCTYPE facelet-taglib PUBLIC "-//Sun Microsystems, Inc.//DTD Facelet Taglib 1.0//EN" "facelet-taglib_1_0.dtd"><facelet-taglib>    <!-- author: thomas.jachmann@mindmatters.de -->    <namespace>http://myfaces.apache.org/tomahawk</namespace>    <tag>        <tag-name>inputFileUpload</tag-name>        <component>            <component-type>org.apache.myfaces.HtmlInputFileUpload</component-type>        </component>    </tag></facelet-taglib>

 

完整的taglib 定义: http://wiki.apache.org/myfaces-data/attachments/Use_Facelets_with_Tomahawk/attachments/tomahawk.taglib.xml

3. web.xml中,Tomahawk 需要的部分:

<!-- My Faces Extensions Filter -->    <filter>        <filter-name>extensionsFilter</filter-name>        <filter-class>org.apache.myfaces.component.html.util.ExtensionsFilter</filter-class>        <init-param>            <param-name>uploadMaxFileSize</param-name>            <param-value>100m</param-value><!--             <description>Set the size limit for uploaded files.                Format: 10 - 10 bytes                        10k - 10 KB                        10m - 10 MB                        1g - 1 GB            </description>             -->        </init-param>        <init-param>            <param-name>uploadThresholdSize</param-name>            <param-value>100k</param-value><!--             <description>Set the threshold size - files                    below this limit are stored in memory, files above                    this limit are stored on disk.                Format: 10 - 10 bytes                        10k - 10 KB                        10m - 10 MB                        1g - 1 GB            </description>             -->        </init-param>        <!--        <init-param>            <param-name>uploadRepositoryPath</param-name>            <param-value>/temp</param-value>            <description>Set the path where the intermediary files will be stored.            </description>        </init-param>        -->    </filter>    <filter-mapping>        <filter-name>extensionsFilter</filter-name>        <url-pattern>*.seam</url-pattern>    </filter-mapping><!-- MyFaces Tomahawk Library -->    <context-param>        <param-name>facelets.LIBRARIES</param-name>        <param-value>/WEB-INF/tomahawk.taglib.xml</param-value>    </context-param>

 

4. HTML可以像这样: 

<h:form enctype="multipart/form-data"><t:inputFileUpload storage="file" value="#{uploadBean.file}"/>        <h:commandButton value="Submit" action="#{upload.upload}"/>        <h:messages/></h:form>

 

5.  最后,把tomahawk.jar 、the commons-file-upload.jar、common-io.jar 打包到ear。当然需要在MANIFEST.MF 文件中修改class-path。

 Class-Path: jboss-seam.jar commons-fileupload-1.1.jar commons-io-1.2.jar tomahawk.jar

 希望有所帮助. Regards. Marcio Endo

 下面有附件。文件是fileupload.zip。它不包含完善的build.xml。不过你可以通过ant -f packaging-build.xml 生成必需的.ear。

例子运行环境:

  • JBoss-4.0.4.CR2
  • JBoss-Seam-1.0.0.CR2
  • MyFaces 1.1.1
  • Tomahawk 1.1.1

 Marcio Endo


不必修改manifest 文件。我已经把全部tomahawk 组件集成了。一会儿我把设置贴出来。

Fady Matar


Alternative FileUpload
我已经集成了例子文件,不需要backing bean ,用seam gen工具建立项目。

Arthur Marinis


附件:
fileupload.zip Info on fileupload.zip 1849616 bytes

 

bq_cui:

这个例子我还没有测试,应该还不能将文件上传信息保存到数据库中。2007-4-27

原创粉丝点击