Samuel Learning

  • 联系
  •  
  • 管理

文章分类(203)

  • 01 ASP/HTML(6)
  • 02 Script/CSS(16)
  • 03 C/C++
  • 04 XML(4)
  • 05 微软编程(26)
  • 06 J2EE(60)
  • 09 Linux(14)
  • 10 Database(27)
  • 11 报表打印
  • 12 打包安装(1)
  • 13 模式重构(2)
  • 14 系统架构
  • 15 敏捷开发(11)
  • 16 案例分析(30)
  • 17 Workflow(1)
  • 18 配置管理(1)
  • 19 项目管理
  • 20 英语(4)

新闻分类(52)

  • CXF学习
  • Hibernate学习(1)
  • iBatis学习(16)
  • Struts1学习(1)
  • Struts2学习(34)

.NET资源

  • adxmenu
  • C# Open Source
  • DNN Directory
  • M2Land
  • Windows Form FAQ
  • 中国DNN
  • 中国DNN联盟

Ajax

  • DoJo
  • GWT
  • JQuery
  • jquery autocomplete
  • jquery flexgrid
  • JQuery Form
  • jquery masked input
  • JQuery UI
  • jquery validation
  • Jquery 图表
  • jquery报表
  • jquery插件集合
  • Qooxdoo
  • Tibco GI
  • YUI
  • YUI-EXT
  • ZeroKode

Java开源

  • ABLE
  • Agile Tao
  • Ajax4JSF
  • Alfresco
  • AppFuse
  • Compiere
  • Equinox
  • Findbugs
  • Geronimo
  • Grails
  • Harmony
  • Hibernate论坛
  • JAG
  • Java开源大全
  • Java视线论坛
  • jBPM工作流
  • JSFTutorials
  • Nereide ERP
  • Ofbiz ERP
  • Opentaps ERP
  • operamasks
  • Petstore
  • Prototype.js
  • RIFE
  • Runa
  • SpringSide
  • Struts Menu
  • Sun Glassfish
  • Trails
  • YUI4JSF
  • 满江红

Mobile

  • Sencha

WEB资源

  • DHTML中心
  • DHTML参考手册
  • DHTML文档
  • EclipsePlugin
  • Firebug
  • GRO Clinux
  • jMaki
  • JSTL文档
  • LoadIcon
  • Openlaszlo
  • Struts Menu 展示
  • Web Test Tools
  • WebCtrs
  • Webdeveloper
  • 中国RIA开发者论坛

Workflow

  • E-Workflow
  • JBPM
  • OpenWFE
  • OSWorkflow
  • WFMC
  • Workflow Research

其他连接

  • confach
  • CPP
  • ejay
  • Giovanni
  • 丹佛
  • 交大e-learning
  • 交大研究生院
  • 可恶的猫
  • 天天@blog
  • 我的相册
  • 阿飞

大牛人

  • 32篇JBPM
  • David.Turing
  • HongSoft@业务集成
  • Joel
  • Koen Aers
  • Martinfowler
  • Raible Matt
  • Raible Wiki
  • Scott W.Ambler
  • Tom Baeyens
  • Uncle Bob
  • 一个世界在等待
  • 子在川上曰
  • 小布老师
  • 小明
  • 差沙
  • 徐昊
  • 江南白衣
  • 汪博士
  • 汪小金
  • 银狐999

开源软件

  • 2Bizbox ERP
  • CompiereCRM&ERP
  • EGW
  • Vtiger CRM
  • webERP

敏捷

  • Canoo
  • Cruisecontrol
  • DBUnit
  • EL4Ant
  • Extreme Programming
  • Fit
  • Fitnesse
  • JFrog
  • Liquibase
  • Maven
  • MockObjects
  • Selenium
  • Squish
  • xpairtise
  • XPlanner
  • XProgramming
  • 敏捷联盟

数据库

  • Oracle 中国
  • Oracle-ERP
  • Oracle在线社区

未归类

  • Aquarius Orm Studio
  • mambo建站系统
  • Oracle产品下载
  • 远程同步管理工具Capivara

经典框架

  • Apache Shale
  • formdef-struts
  • FreeMarker 主页
  • JBoss Seam
  • JSF 中心
  • JSF 入门应用
  • JSF中国
  • MyFaces官方
  • Spring 社区
  • Spring专业网站
  • Spring中文论坛
  • Spring参考手册
  • Spring官方网站
  • strecks-struts
  • Struts1
  • Struts2
  • Struts-layout
  • StrutsWiKi
  • Tapestry WIKI
  • Tapestry 官方
  • Tapestry4开发指南
  • Tapestry中文文档
  • Webwork2文档
  • Wicket

网络教程

  • Laliluna
  • RoseIndia
  • Sang Shin
  • Visualbuilder

著名站点

  • Buildix
  • Dev2Dev
  • IBM dev中国
  • InfoQ
  • ITPub
  • Java Eye
  • Java Research
  • JavaRead
  • JavaWorldTW
  • Matrix
  • PHP100
  • PHPX
  • SpringSideWiKi
  • TheServerSide
  • TWPHP
  • 中国工作流论坛

项目管理

  • 管理人网

最新评论

View Post

Upload - Struts-Examples1.3.8

 

Upload -Struts-Examples1.3.8

l 上传文件的form定义

<html:form action="upload-submit.do?queryParam=Successful" enctype="multipart/form-data">

    <p>Please enter some text, just to demonstrate the handling of text elements as opposed to file elements: <br />

    <html:text property="theText" errorStyle="background-color: yellow"/></p>

    <p>Please select the file that you would like to upload: <br />

    <html:file property="theFile" errorStyle="background-color: yellow" /></p>

    <p>If you would rather write this file to another file, please check here: <br />

    <html:checkbox property="writeFile" /></p>

    <p>If you checked the box to write to a file, please specify the file path here: <br />

    <html:text property="filePath" errorStyle="background-color: yellow"/></p>

    <p>

    <html:submit />

</p>

</html:form>

l FormBean的定义

<form-beans>

    <form-bean name="uploadForm"

                   type="org.apache.struts.webapp.upload.UploadForm"/>

</form-beans>

publicclass UploadForm extends ValidatorForm {

/**

     *Thevalueofthetexttheuserhassentasformdata

     */

    protected String theText;

    /**

     *Thevalueoftheembeddedquerystringparameter

     */

    protected String queryParam;

    /**

     *Whetherornottowritetoafile

     */

    protectedbooleanwriteFile;

    /**

     *Thefilethattheuserhasuploaded

     */

    protected FormFile theFile;

    /**

     *Thefilepathtowriteto

     */

protected String filePath;

/**

     *Checktomakesuretheclienthasn'texceededthemaximumalloweduploadsizeinsideofthis validatemethod.

     */

    public ActionErrors validate(

        ActionMapping mapping,

        HttpServletRequest request) {

        ActionErrors errors = super.validate(mapping, request);

        //has the maximum length been exceeded?

        Boolean maxLengthExceeded =

            (Boolean) request.getAttribute(

                MultipartRequestHandler.ATTRIBUTE_MAX_LENGTH_EXCEEDED);

        if ((maxLengthExceeded != null) && (maxLengthExceeded.booleanValue())) {

            if (errors == null) {

                errors = new ActionErrors();

            }

            errors.add(

                ActionMessages.GLOBAL_MESSAGE ,

                new ActionMessage("maxLengthExceeded"));

            errors.add(

                ActionMessages.GLOBAL_MESSAGE ,

                new ActionMessage("maxLengthExplanation"));

        }

        return errors;

}

}

ValidatorForm 表示可以允许validate xml进行数据验证。

<form-validation>

    <formset>

        <form name="uploadForm">

            <field property="theText" depends="required,minlength">

                <arg key="The Text" resource="false" position="0"/>

                <arg name="minlength" key="${var:minlength}" resource="false" position="1"/>

                <var>

                    <var-name>minlength</var-name>

                    <var-value>5</var-value>

                </var>

            </field>

            <field property="filePath" depends="validwhen">

                <arg key="File Path" resource="false" position="0"/>

                <var>

                    <var-name>test</var-name>

                    <var-value>((writeFile == "false") or (*this* != null))</var-value>

                </var>

            </field>

        </form>

    </formset>

</form-validation>

验证theText字段长度至少5个字符,如果有写文件标识,那么必填filePath文件路径名。

l 上传操作的Action

<action path="/upload-submit"

                type="org.apache.struts.webapp.upload.UploadAction"

                name="uploadForm"

                scope="request"

                validate="true"

                input="input">

            <forward name="input" path="/upload.jsp"/>

            <forward name="display" path="/display.jsp"/>

</action>

如果,标识为输出到其他文件,就把文件上传到服务器上;

否则,读取文件内容,然后打印到第二个页面上。

publicclass UploadAction extends Action

{

    public ActionForward execute(ActionMapping mapping,

                                 ActionForm form,

                                 HttpServletRequest request,

                                 HttpServletResponse response)

        throws Exception {

        if (form instanceof UploadForm) {

            //this line is here for when the input page is upload-utf8.jsp,

            //it sets the correct character encoding for the response

            String encoding = request.getCharacterEncoding();

            if ((encoding != null) && (encoding.equalsIgnoreCase("utf-8")))

            {

                response.setContentType("text/html; charset=utf-8");

            }

            UploadForm theForm = (UploadForm) form;

            //retrieve the text data

            String text = theForm.getTheText();

            //retrieve the query string value

            String queryValue = theForm.getQueryParam();

            //retrieve the file representation

            FormFile file = theForm.getTheFile();

            //retrieve the file name

            String fileName= file.getFileName();

            //retrieve the content type

            String contentType = file.getContentType();

            boolean writeFile = theForm.getWriteFile();

            //retrieve the file size

            String size = (file.getFileSize() + " bytes");

            String data = null;

            try {

                //retrieve the file data

                ByteArrayOutputStream baos = new ByteArrayOutputStream();

                InputStream stream = file.getInputStream();

                if (!writeFile) {

                    //only write files out that are less than 1MB

                    if (file.getFileSize() < (4*1024000)) {

                        byte[] buffer = newbyte[8192];

                        int bytesRead = 0;

                        while ((bytesRead = stream.read(buffer, 0, 8192)) != -1) {

                            baos.write(buffer, 0, bytesRead);

                        }

                        data = new String(baos.toByteArray());

                    }

                    else {

                        data = new String("The file is greater than 4MB, " +

                                " and has not been written to stream." +

                                " File Size: " + file.getFileSize() + " bytes. This is a" +

                                " limitation of this particular web application, hard-coded" +

                                " in org.apache.struts.webapp.upload.UploadAction");

                    }

                }

                else {

                    //write the file to the file specified

                    OutputStream bos = new FileOutputStream(theForm.getFilePath());

                   int bytesRead = 0;

                    byte[] buffer = newbyte[8192];

                    while ((bytesRead = stream.read(buffer, 0, 8192)) != -1) {

                        bos.write(buffer, 0, bytesRead);

                    }

                    bos.close();

                    data = "The file has been written to """ + theForm.getFilePath() + """";

                }

                //close the stream

                stream.close();

            }

            catch (FileNotFoundException fnfe) {

                returnnull;

            }

            catch (IOException ioe) {

                returnnull;

            }

            //place the data into the request for retrieval from display.jsp

            request.setAttribute("text", text);

            request.setAttribute("queryValue", queryValue);

            request.setAttribute("fileName", fileName);

            request.setAttribute("contentType", contentType);

            request.setAttribute("size", size);

            request.setAttribute("data", data);

            //destroy the temporary file created

            file.destroy();

            //return a forward to display.jsp

            return mapping.findForward("display");

        }

        //this shouldn't happen in this example

        returnnull;

    }

}

l 显示结果的页面是一个jsp,读取request上下文的属性。

posted on 2008-07-13 18:05 MingIsMe 阅读(105) 评论(0)  编辑  收藏 所属分类: 16 案例分析

 
Powered by:
BlogJava
Copyright © MingIsMe