答案:使用struts,ibaits和JSTL开发简便通用的文件上传系统
文件上传也是一个老生常谈的问题了。struts提供了很方便的文件上传组件,利用struts,很容易开发文件上传的系统。本文结合ibatis和JSTL开发一个简便而通用的文件上传系统。通过本文,你可以学到如何在struts里配置文件上传功能、struts1.1的动态form配置和使用、如果简单的使用ibatis,如果简单使用的JSTL。
首先我们要在struts-config里加上一句,来指定临时目录以及限制大小。
<controller maxFileSize="2M" tempDir="d:/temp"/>
然后只要在form中加上enctype="multipart/form-data" 就可以实现struts的上传了,十分简单吧?
下面我们在数据库中建立一张表。
create sequence Attachment_seq;
DROP TABLE ATTACHMENT;
create table Attachment
(
AttachID INT not null, --自动增长号 AttachFilename VARCHAR2(250) null, --文件名 AttachFileSize INT not null, --文件大小 AttachMimeType VARCHAR2(70) null, --文件类型 AttachDesc VARCHAR2(250) null, --说明 AttachCreationIP VARCHAR2(20) not null, --上传的IP AttachCreationDate TIMESTAMP not null, --创建时间 AttachModifiedDate TIMESTAMP not null, --文件保存路径 ATTACHFILEPATH VARCHAR2 (250) NOT NULL,
primary key (AttachID)
);
create or replace trigger Attach_trig_autoinc
before insert on Attachment
for each row
begin
if (:new.AttachID is null) then
select Attachment_seq.nextval into :new.AttachID from dual;
end if;
end;
字段的内容都很简单。
下面建立ibatis的SQL map文件。建立标准的insert、update、delete和find的SQL。相信看过前面系列文章的朋友对此已经很熟悉了。
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE sql-mapPUBLIC "-//iBATIS.com//DTD SQL Map 1.0//EN""http://www.ibatis.com/dtd/sql-map.dtd";><sql-map name="attachment"><!-- ============================================= mapped-statement insert ============================================= --><dynamic-mapped-statement name="insertattachmentDao" > insert into ATTACHMENT ( <dynamic prepend=""> <isPropertyAvailable prepend="," property="ATTACHID" > <isNotNull prepend="" property="ATTACHID" > ATTACHID </isNotNull> </isPropertyAvailable> <isPropertyAvailable prepend="," property="ATTACHFILENAME" > <isNotNull prepend="" property="ATTACHFILENAME" > ATTACHFILENAME </isNotNull> </isPropertyAvailable> <isPropertyAvailable prepend="," property="ATTACHFILEPATH" > <isNotNull prepend="" property="ATTACHFILEPATH" > ATTACHFILEPATH </isNotNull> </isPropertyAvailable> <isPropertyAvailable prepend="," property="ATTACHFILESIZE" > <isNotNull prepend="" property="ATTACHFILESIZE" > ATTACHFILESIZE </isNotNull> </isPropertyAvailable> <isPropertyAvailable prepend="," property="ATTACHMIMETYPE" > <isNotNull prepend="" property="ATTACHMIMETYPE" > ATTACHMIMETYPE </isNotNull> </isPropertyAvailable> <isPropertyAvailable prepend="," property="ATTACHDESC" > <isNotNull prepend="" property="ATTACHDESC" > ATTACHDESC </isNotNull> </isPropertyAvailable> <isPropertyAvailable prepend="," property="ATTACHCREATIONIP" > <isNotNull prepend="" property="ATTACHCREATIONIP" > ATTACHCREATIONIP </isNotNull> </isPropertyAvailable> <isPropertyAvailable prepend="," property="ATTACHCREATIONDATE" > <isNotNull prepend="" property="ATTACHCREATIONDATE" > ATTACHCREATIONDATE </isNotNull> </isPropertyAvailable> <isPropertyAvailable prepend="," property="ATTACHMODIFIEDDATE" > <isNotNull prepend="" property="ATTACHMODIFIEDDATE" > ATTACHMODIFIEDDATE </isNotNull> </isPropertyAvailable> </dynamic> ) valueS (
<dynamic prepend="">
<isPropertyAvailable prepend="," property="ATTACHID" >
<isNotNull prepend="" property="ATTACHID" >
#ATTACHID#
</isNotNull>
</isPropertyAvailable>
<isPropertyAvailable prepend="," property="ATTACHFILENAME" >
<isNotNull prepend="" property="ATTACHFILENAME" >
#ATTACHFILENAME#
</isNotNull>
</isPropertyAvailable>
<isPropertyAvailable prepend="," property="ATTACHFILEPATH" >
<isNotNull prepend="" property="ATTACHFILEPATH" >
#ATTACHFILEPATH#
</isNotNull>
</isPropertyAvailable>
<isPropertyAvailable prepend="," property="ATTACHFILESIZE" >
<isNotNull prepend="" property="ATTACHFILESIZE" >
#ATTACHFILESIZE#
</isNotNull>
</isPropertyAvailable>
<isPropertyAvailable prepend="," property="ATTACHMIMETYPE" >
<isNotNull prepend="" property="ATTACHMIMETYPE" >
#ATTACHMIMETYPE#
</isNotNull>
</isPropertyAvailable>
<isPropertyAvailable prepend="," property="ATTACHDESC" >
<isNotNull prepend="" property="ATTACHDESC" >
#ATTACHDESC#
</isNotNull>
</isPropertyAvailable>
<isPropertyAvailable prepend="," property="ATTACHCREATIONIP" >
<isNotNull prepend="" property="ATTACHCREATIONIP" >
#ATTACHCREATIONIP#
</isNotNull>
</isPropertyAvailable>
<isPropertyAvailable prepend="," property="ATTACHCREATIONDATE" >
<isNotNull prepend="" property="ATTACHCREATIONDATE" >
#ATTACHCREATIONDATE#
</isNotNull>
</isPropertyAvailable>
<isPropertyAvailable prepend="," property="ATTACHMODIFIEDDATE" >
<isNotNull prepend="" property="ATTACHMODIFIEDDATE" >
#ATTACHMODIFIEDDATE#
</isNotNull>
</isPropertyAvailable>
</dynamic>)
</dynamic-mapped-statement>
<!-- =============================================
mapped-statement update
============================================= -->
<dynamic-mapped-statement name="updateattachmentDao" >
update ATTACHMENT
<dynamic prepend="set">
<isPropertyAvailable prepend="," property="ATTACHID" >
ATTACHID=#ATTACHID#
</isPropertyAvailable>
<isPropertyAvailable prepend="," property="ATTACHFILENAME" >
ATTACHFILENAME=#ATTACHFILENAME:VARCHAR#
</isPropertyAvailable>
<isPropertyAvailable prepend="," property="ATTACHFILEPATH" >
ATTACHFILEPATH=#ATTACHFILEPATH:VARCHAR#
</isPropertyAvailable>
<isPropertyAvailable prepend="," property="ATTACHFILESIZE" >
ATTACHFILESIZE=#ATTACHFILESIZE#
</isPropertyAvailable>
<isPropertyAvailable prepend="," property="ATTACHMIMETYPE" >
ATTACHMIMETYPE=#ATTACHMIMETYPE:VARCHAR#
</isPropertyAvailable>
<isPropertyAvailable prepend="," property="ATTACHDESC" >
ATTACHDESC=#ATTACHDESC:VARCHAR#
</isPropertyAvailable>
<isPropertyAvailable prepend="," property="ATTACHCREATIONIP" >
ATTACHCREATIONIP=#ATTACHCREATIONIP:VARCHAR#
</isPropertyAvailable>
<isPropertyAvailable prepend="," property="ATTACHCREATIONDATE" >
ATTACHCREATIONDATE=#ATTACHCREATIONDATE#
</isPropertyAvailable>
<isPropertyAvailable prepend="," property="ATTACHMODIFIEDDATE" >
ATTACHMODIFIEDDATE=#ATTACHMODIFIEDDATE#
</isPropertyAvailable>
</dynamic>
<dynamic prepend="where">
<isPropertyAvailable prepend="and" property="ATTACHID" >
ATTACHID=#ATTACHID#
</isPropertyAvailable>
<isPropertyAvailable prepend="and" property="ATTACHFILENAME" >
ATTACHFILENAME=#ATTACHFILENAME:VARCHAR#
</isPropertyAvailable>
<isPropertyAvailable prepend="and" property="ATTACHFILEPATH" >
ATTACHFILEPATH=#ATTACHFILEPATH:VARCHAR#
</isPropertyAvailable>
<isPropertyAvailable prepend="and" property="ATTACHFILESIZE" >
ATTACHFILESIZE=#ATTACHFILESIZE#
</isPropertyAvailable>
<isPropertyAvailable prepend="and" property="ATTACHMIMETYPE" >
ATTACHMIMETYPE=#ATTACHMIMETYPE:VARCHAR#
</isPropertyAvailable>
<isPropertyAvailable prepend="and" property="ATTACHDESC" >
ATTACHDESC=#ATTACHDESC:VARCHAR#
</isPropertyAvailable>
上一个:配置Eclpise+tomcat并实现JSP的编写与部署
下一个:Struts开发技巧