jsp页面:
<%@ page contentType="text/html; charset=utf-8" %>
<%@include file="/common.jsp"%>
<html>
<head>
<title>
testArr
</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"></head>
<body bgcolor="#ffffff">
<html:form action="/testArr.do" enctype="multipart/form-data">
<input type="hidden" name="fileUpLoadNumberRow" id="fileUpLoadNumberRow" value="1">
<table <%=tableStyle%> id="testArr">
<tr class="ListTableHeader" >
<td width="13%" align="center"><b>操作</b></td>
<td width="33%" align="center"><b>文件描述</b></td>
<td width="54%" align="center"><b>源文件</b></td>
</tr>
<tr class="ListTableRow" >
<td width="13%"> <input type="button" name="Submit" value="删除" class="Button4" onclick="delCode(1)"></td>
<td width="33%" align="center"><b><input type="hidden" name="fileUpLoadHiddenIDNewObject1" value="" id="fileUpLoadHiddenIDNewObject1"><input type="text" name="fileTitle1" <%=input100Change%> id="fileTitle1"></b></td>
<td width="54%" align="center"><b><input type="file" name="filePath1" <%=input100%> id="filePath1" onchange="showFileChange(1,'filePath','fileUpLoadHiddenIDNewObject','fileTitle')"></b></td>
</tr>
<tr class="ListTableRow" >
<td width="100%" colspan="3" ><input type="submit" name="Submit" value="提交" class="Button4">
<input type="button" name="Submit" value="新增附件" class="Button4" onclick="addCode()">
</td>
</tr>
</table>
</html:form>
<script language="javascript" type="text/javascript">
String.prototype.trim = function()
{
return this.replace(/(^[\s]*)|([\s]*$)/g, "");
}
function addCode(){
var objTable=document.all("testArr");
var objTableLength=objTable.rows.length
var objChild=document.all("fileUpLoadNumberRow");
objChild.value=parseInt(objChild.value,10)+1;
var myrow=objTable.insertRow(objTableLength-1);
var mycell=myrow.insertCell();
mycell.innerHTML="<input type='button' name='Submit' value='删除' class='Button4' onClick='delCode("+(objTableLength-2+1)+")'>";
mycell=myrow.insertCell();
mycell.innerHTML="<input type='hidden' name='fileUpLoadHiddenIDNewObject"+objChild.value+"' id='fileUpLoadHiddenIDNewObject"+objChild.value+"'><input type='text' name='fileTitle"+objChild.value+"' <%=input100Change%> id='fileTitle"+objChild.value+"' value='' trim='true' dataType='Require' msg='文件描述不能为空!'>";
mycell=myrow.insertCell();
var html=""
html=html+"<input type='file' name='filePath"+objChild.value+"' <%=input100%> id='filePath"+objChild.value+"' onChange=showFileChange("+objChild.value+",'filePath','fileUpLoadHiddenIDNewObject','fileTitle')>";
mycell.innerHTML=html
}
function delCode(rowNum)
{
var objTable=document.all("testArr");
if(window.confirm("真的要删除所选项!"))
{
objTable.deleteRow(rowNum);
}
for(var i=1;i<objTable.rows.length-1;i++)
{
objTable.rows(i).cells(0).innerHTML="<input type='button' name='Submit' value='删除' class='Button4' onClick='delCode("+i+")'>";
}
}
function changeButtomHtml()
{
}
</script>
</body>
</html>
common.jsp 不要的代码请去除
<!--引入系统中要用的标签-->
<%@taglib uri="/WEB-INF/struts-html.tld" prefix="html"%>
<%@taglib uri="/WEB-INF/fireeagle.tld" prefix="fireeagle"%>
<!--引入系统中要用的类-->
<%@page import="java.util.*"%>
<%@page import="com.ytstm.frame.IConstants"%>
<%@page import="com.ytstm.util.*"%>
<%
String baseURL = request.getContextPath(); //得到当前发布的根路经
Object objectRes=request.getAttribute("org.apache.struts.action.MESSAGE"); //得到struts当前配置的资源文件
org.apache.struts.util.MessageResources resources =(org.apache.struts.util.MessageResources)objectRes;
String exitPro=""; //当页面处在修改状态时的提示语
String pageNum_fireeagle=""; //翻页时当输入的不是数字时的提示语
if(resources!=null)
{
exitPro= resources.getMessage("exitPro");
pageNum_fireeagle=resources.getMessage("pageNum");
}
%>
<script language="javascript" type="text/javascript">
var baseURL='<%=baseURL%>'; //得到当前发布的根路经
var editModel_fireeagle=false; //用户操作的当前页面的状态. true以修改数据,false没有修改数据
var exitPro='<%=exitPro%>' //当页面处在修改状态时的提示语
var pageNum_fireeagle='<%=pageNum_fireeagle%>' //翻页时当输入的不是数字时的提示语
</script>
<%
//tableStyle 表格的样式,可以减少页面的html代码量
String tableStyle="width=\"100%\" border=\"1\" cellSpacing=\"0\" cellpadding=\"3\" bordercolorlight=\"#E2D7CF\" bordercolordark=\"#FAF8F6\"";
String frame = baseURL + "/frame"; //框架模块的前缀
String project = baseURL + "/project";//项目立项模块的前缀
String contract = baseURL + "/contract";//合同模块的前缀
String process = baseURL + "/process";//过程模块的前缀
//input 的样式和键盘按下事件,它会转入到下一个可输入控件,可以减少页面的html代码量
String input="class='formStyleall' onKeyDown='next_focus(this)' onChange='changeEditModel()'";
String input4="class='formStyleall' size='4' onKeyDown='next_focus(this)' onChange='changeEditModel()'";
String input10="class='formStyleall' size='12' onKeyDown='next_focus(this)' onChange='changeEditModel()'";
String input12="class='formStyleall' size='12' onKeyDown='next_focus(this)' onChange='changeEditModel()'";
String input15="class='formStyleall' size='15' onKeyDown='next_focus(this)' onChange='changeEditModel()'";
String input20="class='formStyleall' size='20' onKeyDown='next_focus(this)' onChange='changeEditModel()'";
String input25="class='formStyleall' size='25' onKeyDown='next_focus(this)' onChange='changeEditModel()'";
String input30="class='formStyleall' size='30' onKeyDown='next_focus(this)' onChange='changeEditModel()'";
String input35="class='formStyleall' size='35' onKeyDown='next_focus(this)' onChange='changeEditModel()'";
String input40="class='formStyleall' size='40' onKeyDown='next_focus(this)' onChange='changeEditModel()'";
String input50="class='formStyleall' size='50' onKeyDown='next_focus(this)' onChange='changeEditModel()'";
String input60="class='formStyleall' size='60' onKeyDown='next_focus(this)' onChange='changeEditModel()'";
String input100="class='formStyleall' style='width=100%' onKeyDown='next_focus(this)'";
String input100Change="class='formStyleall' style='width=100%' onKeyDown='next_focus(this)' onChange='changeEditModel()'";
//onchange事件的全局变量,这个方法用于修改用户操作的当前页面的状态.
String onChange="onChange='changeEditModel()'";
String changeName="'changeEditModel()'";
//给色*号标示.和()标示,可以减少页面的html代码量
String red=" <font color='red'><b>*</b></font>";
String redLeft="<font color='#FF0000'>(</font>";
String redRight="<font color='#FF0000'>)</font>";
String orderTitle="style='cursor:hand' align='center'";
//查看,新增,修改.用在联接?后面
String modifyView=IConstants.FRAME_MODIFY_FLAG+"="+IConstants.FRAME_MODIFY_FLAG_VIEW;
String modifyAdd=IConstants.FRAME_MODIFY_FLAG+"="+IConstants.FRAME_MODIFY_FLAG_ADD;
String modifyEdit=IConstants.FRAME_MODIFY_FLAG+"="+IConstants.FRAME_MODIFY_FLAG_EDIT;
//页面退出是要执行的方法,当用户修改了页面内容后退出页面时,要给提示信息给用户
String onbeforeunload ="onbeforeunload='javacript:return(isClose())'";
//屏蔽页面的右键功能
// String disableCopy="ondragstart=self.event.returnValue=true onselectstart=self.event.returnValue=true oncontextmenu=self.event.returnValue=false";
String disableCopy="";
%>
<!--引入公用的js代码-->
<script type="text/javascript" src="<%=baseURL%>/js/public.js"></script>
<!--引入公验证的js代码-->
<script type="text/javascript" src="<%=baseURL%>/js/validator.js"></script>
<script type="text/javascript" src="<%=baseURL%>/js/sorttable/sortabletable.js"></script>
<script type="text/javascript" src="<%=baseURL%>/js/selectdate1.js"></script>
<link href="<%=baseURL%>/css/main.css" rel="stylesheet" type="text/css">
<!--有错误信息和提示信息时层要显示的位置-->
<style type="text/css">
<!--
#errorDiv {
position: absolute;
left: 20%;
top: 20%;
width: 352px;
height: 21px;
background-color: #FFFFFF;
}
-->
</style>
publlic.js 不要的代码请去除
function next_focus(text)
{
if(window.event.keyCode ==13)
{
window.event.keyCode=9;
}
}
function mouseovertd (o)
{
o.style.color='#ffffff';
o.style.backgroundColor='#B3B3B3';
}
function mouseouttd (o)
{
o.style.color='#000000';
o.style.backgroundColor='';
}
function goPage(flag,currPage,formId,formAction,formTarget,formMethod,pageNumId)
{
var formObj=document.all(formId)
formObj.target=formTarget;
formObj.method=formMethod;
if(flag=='go')
{
var obj=document.all(pageNumId)
if(isNaN(obj.value))
{
alert(pageNum_fireeagle);
obj.select();
return;
}
else
{
currPage=obj.value
}
}
pageMethod="pageMethod="+flag+"¤tPage="+currPage;
formAction=formAction+"&"+pageMethod;
formObj.action=formAction;
top.bottom.document.all("process").innerHTML="<img name='pro' src='"+baseURL+"/images/public/process.gif' border='0'>";
formObj.submit();
}
function submitForm(formId,submitbutton)
{
editModel_fireeagle=false;
submitbutton.disabled=true
var objForm =document.all(formId);
if(Validator.Validate(objForm,2))
{
top.bottom.document.all("process").innerHTML="<img name='pro' src='"+baseURL+"/images/public/process.gif' border='0'>";
objForm.submit();
}
else
{
submitbutton.disabled=false
}
}
function delOnClick(url,target)
{
if(window.confirm("确定删除!"))
{
top.bottom.document.all("process").innerHTML="<img name='pro' src='"+baseURL+"/images/public/process.gif' border='0'>"
if(target=='parent')
{
parent.location.href=url
}else if(target=='self')
{
location.href=url
}
}
}
function submitFormQuery(formId,queryFlag)
{
var objForm =document.all(formId);
objForm.is_QUERY.value=queryFlag;
top.bottom.document.all("process").innerHTML="<img name='pro' src='"+baseURL+"/images/public/process.gif' border='0'>";
objForm.submit();
}
function buttomOnClick(url,target)
{
top.bottom.document.all("process").innerHTML="<img name='pro' src='"+baseURL+"/images/public/process.gif' border='0'>"
if(target=='parent')
{
parent.location.href=url
}else if(target=='self')
{
location.href=url
}
}
function changeEditModel()
{
editModel_fireeagle=true
}
function isClose()
{
if(editModel_fireeagle)
{
top.bottom.document.all("process").innerHTML="<img name='pro' src='"+baseURL+"/images/public/prosessEnd.gif' border='0'>"
return exitPro
}
}
//只能输入数字
//页面使用:<input type=text name=test value="" onKeypress="JHshNumberText()">
function JHshNumberText()
{
if ( !(((window.event.keyCode >= 48) && (window.event.keyCode <= 57))
|| (window.event.keyCode == 13) || (window.event.keyCode == 46)
|| (window.event.keyCode == 45)))
{
window.event.keyCode = 0 ;
}
}
document.onkeydown = openHelp
function openHelp()
{
if(window.event.keyCode==113)
{
if(top.main.document.title.indexOf("HELP")>=0)
{
showModalDialog(baseURL+"/frame/help/"+top.main.document.title);
}
}
if(window.event.keyCode==116)
{
event.keyCode=0;
event.returnValue=false;
}
}
function jtrim(sstr) //去掉左右空格
{
var astr="";
var dstr="";
var flag=0;
for (i=0;i<sstr.length;i++)
{if ((sstr.charAt(i)!=' ')||(flag!=0))
{dstr+=sstr.charAt(i);
flag=1;
}
}
flag=0;
for (i=dstr.length-1;i>=0;i--)
{if ((dstr.charAt(i)!=' ')||(flag!=0))
{astr+=dstr.charAt(i);
flag=1;
}
}
dstr="";
for (i=astr.length-1;i>=0;i--) dstr+=astr.charAt(i);
return dstr;
}
function popup(page,w,h){ //在窗口中央弹出窗口
var width=screen.width;
var height=screen.height;
var arg;
l=(width-w)/2;
t=(height-h)/2;
if (width>w && height>h) arg="top="+t+",left="+l+",";
win=window.open (page,"select", arg+"height="+h+", width="+w+",toolbar=no, menubar=no, scrollbars=auto, resizable=no, location=no, status=no");
return win;
}
function popupProject(page,w,h){ //在窗口中央弹出窗口
var width=screen.width;
var height=screen.height;
var arg;
l=(width-w)/2;
t=(height-h)/2;
if (width>w && height>h) arg="top="+t+",left="+l+",";
win=window.open (page,"select", arg+"height="+h+", width="+w+",toolbar=no, menubar=no, scrollbars=yes, resizable=tes, location=no, status=no");
return win;
}
var LookProjectWin=null;
function lookProject(url)
{
LookProjectWin= popupProject(url,750,450)
}
function exitWindow()
{
if(LookProjectWin!=null)
{
LookProjectWin.close();
}
}
//flag 文件标示
//filePath 文件路经标示
//fileUpLoadHiddenIDNewObject 文件隐藏控件名标示
//fileTitle 文件标题控件名标示
function showFileChange(flag,filePath,fileUpLoadHiddenIDNewObject,fileTitle)
{
var sorFileName=document.all(filePath+flag).value
var pos=sorFileName.lastIndexOf("\\");
var fuleName=sorFileName.substring(pos+1);
document.all(fileUpLoadHiddenIDNewObject+flag).value=fuleName
var dotPos=fuleName.lastIndexOf(".");
var fuleNameDot=fuleName.substring(0,dotPos);
if(document.all(fileTitle+flag).value=="")
document.all(fileTitle+flag).value=fuleNameDot
}
struts配置
<form-beans>
<form-bean name="testForm" type="com.ytstm.TestForm"/>
</form-beans>
<action path="/testArr" type="com.ytstm.TestArr" name="testForm">
<forward name="test" path="/testArr.jsp" />
</action>
com.ytstm.TestForm
package com.ytstm;
import org.apache.struts.action.*;
public class TestForm extends ActionForm{
public TestForm() {
}
}
com.ytstm.TestArr
package com.ytstm;
import org.apache.struts.action.ActionForward;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionMapping;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import java.util.*;
import org.apache.struts.upload.*;
import java.io.*;
import com.ytstm.util.UpLoadUtil;
public class TestArr extends org.apache.struts.action.Action{
public TestArr() {
}
public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception
{
List returnArr=UpLoadUtil.upload(form,request,"testTable","F:/ytstm/YTSTMWeb/fileset/","fileUpLoadNumberRow","fileUpLoadHiddenIDNewObject","fileTitle");
if(returnArr!=null)
{
for(int i=0;i<returnArr.size();i++)
{
Map map=(Map)returnArr.get(i);
System.out.println("TABLENAME==="+map.get("TABLENAME"));
System.out.println("FILENAME==="+map.get("FILENAME"));
System.out.println("FILEPATH==="+map.get("FILEPATH"));
System.out.println("FILETYPE==="+map.get("FILETYPE"));
System.out.println("FORMERLYFILENAME==="+map.get("FORMERLYFILENAME"));
}
}
return mapping.findForward("test");
}
}
com.ytstm.util.UpLoadUtil
package com.ytstm.util;
import org.apache.struts.action.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.*;
import org.apache.struts.upload.*;
public class UpLoadUtil {
public UpLoadUtil() {
}
/**
*
* @param form ActionForm
* @param request HttpServletRequest
* @param tableName String 表名
* @param storePath String 路经
* @param fileNumControl String 表示附件个数的页面控件名
* @param fileNumControl String 表示附件隐藏的页面控件名
* @return List
* @throws IOException
*/
public static List upload(ActionForm form, HttpServletRequest request,String tableName,String storePath,String fileNumControl,String hiddenIDNewObject,String fileTitle) throws
IOException
{
List retList = new ArrayList(); //存放附件的信息
Hashtable files = form.getMultipartRequestHandler().getFileElements();
String strnumberRowNew = request.getParameter(fileNumControl);
int intnumberRowNew = 0;
FormFile ff[] = null;
ff = new FormFile[files.size()];
Enumeration fileKey = files.keys();
int i = 0;
while (fileKey.hasMoreElements()) {
String ffname = (String) fileKey.nextElement();
ff[i] = (FormFile) files.get(ffname);
i++;
}
for (int a = 0; a < ff.length; a++) {
if (ff[a] == null || ff[a].getFileSize() == 0 ||
ff[a].getFileName() == null
|| ff[a].getFileName().equals("")) {
} else {
Map fileMap=new HashMap();
fileMap.put("TABLENAME",tableName); //表名,设为信息表
InputStream stream = ff[a].getInputStream();
java.util.Date date = new java.util.Date(System.
currentTimeMillis()); //获取当前时间
String virFileName = date.getTime()+""+a;
fileMap.put("FORMERLYFILENAME",ff[a].getFileName()); //标题文件的名字
if (strnumberRowNew != null) {
intnumberRowNew = Integer.parseInt(strnumberRowNew);
for (int n = 1; n<= intnumberRowNew; n++) {
String filesor = request.getParameter(hiddenIDNewObject + n);
if (filesor!=null&&filesor.equals(ff[a].getFileName())) {
String fileTitleValue = request.getParameter(fileTitle + n);
fileMap.put("FILENAME",fileTitleValue);
break;
}
}
}
int dotIndex = ff[a].getFileName().lastIndexOf(".");
String fileType = ff[a].getFileName().substring(dotIndex + 1);
fileMap.put("FILEPATH",virFileName + "." + fileType); //路径
fileMap.put("FILETYPE",fileType);
retList.add(fileMap);
OutputStream bos =
new FileOutputStream(storePath + virFileName +
"." + fileType);
int bytesRead = 0;
byte[] buffer = new byte[8192];
while ((bytesRead = stream.read(buffer, 0, 8192)) != -1) {
bos.write(buffer, 0, bytesRead);
}
bos.close();
}
}
return retList;
}
/**
*
* @param request HttpServletRequest
* @param list List 数据中以存在附件
* @param fileNumControl String 同上
* @param hiddenIDNewObject String 同上
* @param fileTitle String 同上
* @return List
*/
public List combinateEditList(HttpServletRequest request, List list,String fileNumControl,String hiddenIDNewObject,String fileTitle) {
List result=new ArrayList();
if(list==null||list.size()==0) return new ArrayList();
String strnumberRowNew = request.getParameter(fileNumControl);
int intnumberRowNew=0;
if (strnumberRowNew != null) {
intnumberRowNew = Integer.parseInt(strnumberRowNew);
for (int n = 1; n <= intnumberRowNew; n++) {
String filesor = request.getParameter("hiddenIDNewObject" + n);
String fileTitleValue = request.getParameter(fileTitle + n);
for(int i=0;i<list.size();i++)
{
Map fileMap = (Map)list.get(i);
if((fileMap.get("ACCESSORYID").toString()).equals(filesor))
{
fileMap.put("FILENAME",fileTitleValue);
result.add(fileMap);
break;
}
}
}
}
return result;
}
}
如果出现中文乱码,请修改struts源程序的相关类把编码修改成utf-8,网上可以查到。
posted on 2007-02-02 20:34
☜♥☞MengChuChen 阅读(1078)
评论(0) 编辑 收藏 所属分类:
struts