|
今天要實(shí)現(xiàn)一個(gè)在頁(yè)面中動(dòng)態(tài)添加以及刪除一行列表的功能,查找了幾種方法,在此備份,以便日后使用:
========================此方法比較簡(jiǎn)潔,而且可以解決問題========================
function deleteCurrentRow()//刪除當(dāng)前行
{
var currRowIndex=event.srcElement.parentNode.parentNode.rowIndex;
document.all.table10.deleteRow(currRowIndex);//table10--表格id
}
function insertRow()
{
var nRow=document.all.table10.rows.length; //表格的總行數(shù)
var objTheRow=document.all.table10.insertRow(nRow);//在最下邊新增一行
objTheRow.insertCell(0);//新增一個(gè)單元格
objTheRow.insertCell(1);
objTheRow.insertCell(2);
objTheRow.cells(0).innerHTML=nRow;//對(duì)新增的單元格?容
objTheRow.cells(1).innerHTML=" ";
objTheRow.cells(2).innerHTML="<input type='button' value='del this row' onClick='deleteCurrentRow()'>";
}
====================我的程序代碼======================
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML>
<HEAD>
<META http-equiv="Content-Type" content="text/html; charset=GB18030">
<META name="GENERATOR" content="IBM WebSphere Studio">
<TITLE>cfbcard.html</TITLE>
</HEAD>
<SCRIPT language="JavaScript">
var j_1 = 1;
function add_row_family(){
newRow=document.all.family.insertRow(-1)
newcell=newRow.insertCell()
newRow.bgColor='#FFFFFF';
newcell.className='STYLE3';
newcell.align='center';
//newcell.innerHTML="<input type='text' name='familyname"+j_1+"' style='WIDTH: 60px; font-size:9pt; color:#000000' />";
newcell.innerHTML="<SELECT name='thesistogether"+j_1+"'>"+
" <option value='請(qǐng)選擇'>"+
" 請(qǐng)選擇"+
" </option>"+
" <option value='1'>"+
" 111"+
" </option>"+
" <option value='2'>"+
" 222"+
" </option>"+
" <option value='3'>"+
" 333"+
" </option>"+
" <option value='4'>"+
" 444"+
" </option>"+
" <option value='5'>"+
" 555"+
" </option>"+
"</SELECT>";
for(var i = 0;i<12;i++){
newcell=newRow.insertCell() ;
newRow.bgColor='#FFFFFF';
newcell.className='STYLE3';
newcell.align='center';
newcell.innerHTML="<input type='text' name='familyrelation"+j_1+"' style='WIDTH: 60px; font-size:9pt; color:#000000' />";
}
newcell=newRow.insertCell() ;
newRow.bgColor='#FFFFFF';
newcell.className='STYLE3';
newcell.align='center';
//newcell.innerHTML="<a href='javascript:delTableRow(\""+1+"\")'>刪除</a>";
newcell.innerHTML="<input type='button' value='刪除' onClick='deleteCurrentRow()'>";
j_1++;
document.all.j_1.value=j_1;
document.all.family.focus();
}
function deleteCurrentRow()//刪除當(dāng)前行
{
var currRowIndex=event.srcElement.parentNode.parentNode.rowIndex;
document.all.family.deleteRow(currRowIndex);//table10--表格id
}
</script>
<body bgcolor="#F5F1F5" >
<form name="form1" method="post" action="" onsubmit="">
<table>
<tr>
<td align="right"><INPUT type="button" name="add" onclick="add_row_family();" value="添加"></td>
</tr>
<tr>
<td>
<table id="family" style="width:100%" border="1" cellspacing="1" cellpadding="2" class="tbMain">
<tr>
<td class="td_name">111</td>
<td class="td_name">222</td>
<td class="td_name">333</td>
<td class="td_name">444</td>
<td class="td_name">555</td>
<td class="td_name">666</td>
<td class="td_name">777</td>
<td class="td_name">888</td>
<td class="td_name">999</td>
<td class="td_name">000</td>
<td class="td_name">123</td>
<td class="td_name">456</td>
<td class="td_name">789</td>
<td class="td_name">刪除</td>
</tr>
</table>
</td>
</tr>
</table>
</form>
</body>
</html>
=================================另外一種方法==============
如何刪除表格的行上次講到了如何動(dòng)態(tài)給表格增加行,那么這次就講講如何刪除表格的行了。首先建立一個(gè)表格,
<table border="1">
<tr>
<td>姓名</td>
<td>地址</td>
</tr>
<tbody id="mainbody">
<tr id="delCell">
<td>name</td>
<td>address</td>
</tr>
</tbody>
</table>
取得tbody的元素var mailbody = document.getElementById("mainbody");,
接著取得要?jiǎng)h除行的元素var cell = document.getElementById("delCell");
最后就是從tbody中移去要?jiǎng)h除的行就可以了mainbody.removeChild(cell);
完整的代碼如下:
<html>
<head>
<title>動(dòng)態(tài)刪除表格的行</title>
<script type="text/javascript">
function deleteCell(){
var mailbody = document.getElementById("mainbody");
var cell = document.getElementById("delCell");
if(cell!=undefined){
mainbody.removeChild(cell);
}
}
</script>
</head>
<body>
<table border="1">
<tr>
<td>姓名</td>
<td>地址</td>
</tr>
<tbody id="mainbody">
<tr id="delCell">
<td>name</td>
<td>address</td>
</tr>
</tbody>
</table>
<input type="button" value="刪除" onclick="deleteCell()"/>
</body>
<html>
先說一下提交的頁(yè)面,smartupload組件要求用字節(jié)流的方式來提交<FORM action="upload.jsp" encType=multipart/form-data method=post>。下面就是個(gè)例子upload.htm:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<!-- saved from url=(0057)http://localhost:8080/jspsmartfile/jsp/uploadTemplate.jsp -->
<HTML><HEAD>
<META content="text/html; charset=gb2312" http-equiv=Content-Type>
<META content="MSHTML 5.00.2920.0" name=GENERATOR></HEAD>
<BODY bgColor=#e6e6e6><BR>
<FORM action="upload.jsp" encType=multipart/form-data method=post>
<TABLE>
<TBODY>
<TR>
<TD><FONT color=#000000 face=helv,helvetica size=1> File
: </FONT> <INPUT size=60 type=file name="file"></TD></TR>
<TR>
<TR>
<TD><FONT color=#000000 face=helv,helvetica size=1> File
: </FONT> <INPUT size=60 type=file name="file1"></TD></TR>
<TR>
<TD><FONT color=#000000 face=helv,helvetica size=1> File
: </FONT> <INPUT size=60 type=text name="text"></TD></TR>
<TR>
<TD
align=right><INPUT type=submit value=Send name="send"></TD></TR></TBODY></TABLE></FORM></BODY></HTML>
再來看一下接收的頁(yè)面 ,我們把文件上傳到服務(wù)器以后就直接把它再存入數(shù)據(jù)庫(kù)中:upload.jsp
<%@ page contentType="text/html;charset=gb2312"%>
<%@ page import="java.sql.*"%>
<%@ page import="com.jspsmart.upload.*" %>
<%@ page import="DBstep.iDBManager2000.*"%>
<%
//實(shí)例化上載bean
com.jspsmart.upload.SmartUpload mySmartUpload=new com.jspsmart.upload.SmartUpload();
//初始化
mySmartUpload.initialize(pageContext);
//設(shè)置上載的最大值
mySmartUpload.setMaxFileSize(500 * 1024*1024);
//上載文件
mySmartUpload.upload();
//循環(huán)取得所有上載的文件
for (int i=0;i<mySmartUpload.getFiles().getCount();i++){
//取得上載的文件
com.jspsmart.upload.File myFile = mySmartUpload.getFiles().getFile(i);
if (!myFile.isMissing())
{
//取得上載的文件的文件名
String myFileName=myFile.getFileName();
//取得不帶后綴的文件名
String suffix=myFileName.substring(0,myFileName.lastIndexOf('.'));
//取得后綴名
String ext= mySmartUpload.getFiles().getFile(0).getFileExt();
//取得文件的大小
int fileSize=myFile.getSize();
//保存路徑
String aa=getServletContext().getRealPath("/")+"jsp\\";
String trace=aa+myFileName;
//取得別的參數(shù)
String explain=(String)mySmartUpload.getRequest().getParameter("text");
String send=(String)mySmartUpload.getRequest().getParameter("send");
//將文件保存在服務(wù)器端
myFile.saveAs(trace,mySmartUpload.SAVE_PHYSICAL);
//下面的是將上載的文件保存到數(shù)據(jù)庫(kù)中
//將文件讀到流中
java.io.File file = new java.io.File(trace);
java.io.FileInputStream fis = new java.io.FileInputStream(file);
out.println(file.length());
//打開數(shù)據(jù)庫(kù)
ResultSet result=null;
String mSql=null;
PreparedStatement prestmt=null;
DBstep.iDBManager2000 DbaObj=new DBstep.iDBManager2000();
DbaObj.OpenConnection();
//將文件寫到數(shù)據(jù)庫(kù)中
mSql="insert into marklist (markname,password,marksize,markdate,MarkBody) values (?,?,?,?,?)";
prestmt =DbaObj.Conn.prepareStatement(mSql);
prestmt.setString(1, "aaa1");
prestmt.setString(2, "0000");
prestmt.setInt(3, fileSize);
prestmt.setString(4, DbaObj.GetDateTime());
prestmt.setBinaryStream(5,fis,(int)file.length());
DbaObj.Conn.setAutoCommit(true) ;
prestmt.executeUpdate();
DbaObj.Conn.commit();
out.println(("上載成功!!!").toString());
}
else
{ out.println(("上載失敗!!!").toString()); }
}//與前面的if對(duì)應(yīng)
%>
再說一下下載,下載分兩種情況1。從數(shù)據(jù)庫(kù)直接下載2。從服務(wù)器上下載
先說從數(shù)據(jù)庫(kù)直接下載的情形:就是把輸入流從數(shù)據(jù)庫(kù)里讀出來,然后轉(zhuǎn)存為文件
<%@ page contentType="text/html; charset=gb2312" %>
<%@ page import="java.sql.*"%>
<%@ page import="java.io.*" %>
<%@ page import="DBstep.iDBManager2000.*"%>
<%
int bytesum=0;
int byteread=0;
//打開數(shù)據(jù)庫(kù)
ResultSet result=null;
String Sql=null;
PreparedStatement prestmt=null;
DBstep.iDBManager2000 DbaObj=new DBstep.iDBManager2000();
DbaObj.OpenConnection();
//取得數(shù)據(jù)庫(kù)中的數(shù)據(jù)
Sql="select * from t_local_zhongzhuan ";
result=DbaObj.ExecuteQuery(Sql);
result.next();
//將數(shù)據(jù)庫(kù)中的數(shù)據(jù)讀到流中
InputStream inStream=result.getBinaryStream("content");
FileOutputStream fs=new FileOutputStream( "c:/dffdsafd.doc");
byte[] buffer =new byte[1444];
int length;
while ((byteread=inStream.read(buffer))!=-1)
{
out.println("<DT><B>"+byteread+"</B></DT>");
bytesum+=byteread;
System.out.println(bytesum);
fs.write(buffer,0,byteread);
}
%>
再說從服務(wù)器上下載的情形:
<%@ page contentType="text/html; charset=gb2312" %>
<%@ page import="java.io.*" %>
<%
String fileName = "zsc104.swf".toString();
f//讀到流中
InputStream inStream=new FileInputStream("c:/zsc104.swf");
//設(shè)置輸出的格式
response.reset();
response.setContentType("bin");
response.addHeader("Content-Disposition","attachment; filename=\"" + fileName + "\"");
//循環(huán)取出流中的數(shù)據(jù)
byte[] b = new byte[100];
int len;
while((len=inStream.read(b)) >0)
response.getOutputStream().write(b,0,len);
inStream.close();
%>
好了,到這里只要不是太大的文件的上傳下載的操作都可以完成了。
在頁(yè)面中先設(shè)置一個(gè)hidden來傳遞參數(shù)以便其他頁(yè)面用request.getParameter()獲得,
hidden必須寫在窗體form中,而且另一個(gè)要得到它的頁(yè)面必須是form中的action指向的頁(yè)
面,不然得不到。切記!!!
很多網(wǎng)頁(yè)都是框架結(jié)構(gòu)的,在很多的情況下會(huì)通過按鈕點(diǎn)擊事件或鏈接,跳出框架轉(zhuǎn)到其它界面。例如說點(diǎn)擊“注銷登錄”返回到登錄界面。
一、通過運(yùn)行腳本跳出框架有以下幾種寫法:
1. <script language = javascript>window.open('Login.aspx','_top')</script>"
2. <script language = javascript>window.open('Login.aspx','_parent')</script>"
3. <script language = javascript>window.parent.location.href='login.aspx'</script>
4. Response.Write("<script>window.parent.opener=null;window.top.close();</script>")
Response.Write("<script>window.open('index.aspx','');</script>")
這種方法會(huì)先關(guān)閉原框架窗口,再重新打開一個(gè)新的窗口。這在很多功能界面對(duì)瀏覽器進(jìn)行了改變?cè)O(shè)置,而回到登陸界面又用缺省設(shè)置的情況下適用。
二、鏈接跳出框架
這種情況就很簡(jiǎn)單了,加上 target="_top" 屬性就可以了。
zilong
轉(zhuǎn)之ewolf的工作專欄
Hibernate 是當(dāng)前最流行的 O/R mapping 框架,當(dāng)前版本是 3.05 。它出身于 sf.net ,現(xiàn)在已經(jīng)成為 Jboss 的一部分了
iBATIS 是另外一種優(yōu)秀的 O/R mapping 框架,當(dāng)前版本是 2.0 。目前屬于 apache 的一個(gè)子項(xiàng)目了。
相對(duì) Hibernate “ O/R ”而言, iBATIS 是一種“ Sql Mapping ”的 ORM 實(shí)現(xiàn)。
Hibernate 對(duì)數(shù)據(jù)庫(kù)結(jié)構(gòu)提供了較為完整的封裝, Hibernate 的 O/R Mapping 實(shí)現(xiàn)了 POJO 和數(shù)據(jù)庫(kù)表之間的映射,以及 SQL 的自動(dòng)生成和執(zhí)行。程序員往往只需定義好了 POJO 到數(shù)據(jù)庫(kù)表的映射關(guān)系,即可通過 Hibernate 提供的方法完成持久層操作。程序員甚至不需要對(duì) SQL 的熟練掌握, Hibernate/OJB 會(huì)根據(jù)制定的存儲(chǔ)邏輯,自動(dòng)生成對(duì)應(yīng)的 SQL 并調(diào)用 JDBC 接口加以執(zhí)行。
而 iBATIS 的著力點(diǎn),則在于 POJO 與 SQL 之間的映射關(guān)系。也就是說, iBATIS 并不會(huì)為程序員在運(yùn)行期自動(dòng)生成 SQL 執(zhí)行。具體的 SQL 需要程序員編寫,然后通過映射配置文件,將 SQL 所需的參數(shù),以及返回的結(jié)果字段映射到指定 POJO 。
使用 iBATIS 提供的 ORM 機(jī)制,對(duì)業(yè)務(wù)邏輯實(shí)現(xiàn)人員而言,面對(duì)的是純粹的 Java 對(duì)象,
這一層與通過 Hibernate 實(shí)現(xiàn) ORM 而言基本一致,而對(duì)于具體的數(shù)據(jù)操作, Hibernate 會(huì)自動(dòng)生成 SQL 語(yǔ)句,而 iBATIS 則要求開發(fā)者編寫具體的 SQL 語(yǔ)句。相對(duì) Hibernate 而言, iBATIS 以 SQL 開發(fā)的工作量和數(shù)據(jù)庫(kù)移植性上的讓步,為系統(tǒng)設(shè)計(jì)提供了更大的自由空間。
1. ? iBATIS 非常簡(jiǎn)單易學(xué), Hibernate 相對(duì)較復(fù)雜,門檻較高。
2. ? 二者都是比較優(yōu)秀的開源產(chǎn)品
3. ? 當(dāng)系統(tǒng)屬于二次開發(fā) , 無(wú)法對(duì)數(shù)據(jù)庫(kù)結(jié)構(gòu)做到控制和修改 , 那 iBATIS 的靈活性將比 Hibernate 更適合
4. ? 系統(tǒng)數(shù)據(jù)處理量巨大,性能要求極為苛刻,這往往意味著我們必須通過經(jīng)過高度優(yōu)化的 SQL 語(yǔ)句(或存儲(chǔ)過程)才能達(dá)到系統(tǒng)性能設(shè)計(jì)指標(biāo)。在這種情況下 iBATIS 會(huì)有更好的可控性和表現(xiàn)。
5. ? iBATIS 需要手寫 sql 語(yǔ)句,也可以生成一部分, Hibernate 則基本上可以自動(dòng)生成,偶爾會(huì)寫一些 Hql 。同樣的需求 ,iBATIS 的工作量比 Hibernate 要大很多。類似的,如果涉及到數(shù)據(jù)庫(kù)字段的修改, Hibernate 修改的地方很少,而 iBATIS 要把那些 sql mapping 的地方一一修改。
6. ? 以數(shù)據(jù)庫(kù)字段一一對(duì)應(yīng)映射得到的 PO 和 Hibernte 這種對(duì)象化映射得到的 PO 是截然不同的,本質(zhì)區(qū)別在于這種 PO 是扁平化的,不像 Hibernate 映射的 PO 是可以表達(dá)立體的對(duì)象繼承,聚合等等關(guān)系的,這將會(huì)直接影響到你的整個(gè)軟件系統(tǒng)的設(shè)計(jì)思路。
7. ? Hibernate 現(xiàn)在已經(jīng)是主流 O/R Mapping 框架,從文檔的豐富性,產(chǎn)品的完善性,版本的開發(fā)速度都要強(qiáng)于 iBATIS
8. ? 最關(guān)鍵的一句話是 iBATIS 的作者說的:
If you are starting a new project and you're in full control of your object model and database design, Hibernate is a good choice of O/R tool.
If you are accessing any 3rd party databases (e.g. vendor supplied), or you're working with a legacy database, or even just a really poorly designed database, then an O/R mapper might not be capable of handling the situation. That's were an SQL Mapper comes in handy
Hibernate 和iBATIS可以說是互相補(bǔ)充,共同發(fā)展的關(guān)系.具體你想用什么要看實(shí)際情況.如果看了上面的文字還是拿不定注意,那就Just to try it.實(shí)踐是檢驗(yàn)真理的唯一標(biāo)準(zhǔn).鞋合不合適,只有試了才知道
?
結(jié)論:
結(jié)論:
Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=413018
j2ee的O/R方案真是多,和Hibernate相比,iBatis最大的特點(diǎn)就是小巧,上手很快。看iBatis的文檔2小時(shí)就會(huì)用了,這個(gè)O/R Mapping特點(diǎn)就是簡(jiǎn)單易用。只要有SQL基礎(chǔ),相信你不用教程也能看明白。最新版本2.0(下載)。
構(gòu)建ibatis基礎(chǔ)代碼
ibatis 基礎(chǔ)代碼包括:
1. ibatis 實(shí)例配置
一個(gè)典型的配置文件如下(具體配置項(xiàng)目的含義見后):
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE sqlMapConfig
PUBLIC "-//iBATIS.com//DTD SQL Map Config 2.0//EN"
"http://www.ibatis.com/dtd/sql-map-config-2.dtd">
<sqlMapConfig>
<settings
cacheModelsEnabled="true"
enhancementEnabled="true"
lazyLoadingEnabled="true"
errorTracingEnabled="true"
maxRequests="32"
maxSessions="10"
maxTransactions="5"
useStatementNamespaces="false"
/>
<transactionManager type="JDBC">
IBATIS Developer’s Guide Version 1.0
September 2, 2004 So many open source projects. Why not Open your Documents?
<dataSource type="SIMPLE">
<property name="JDBC.Driver"
value="com.p6spy.engine.spy.P6SpyDriver"/>
<property name="JDBC.ConnectionURL"
value="jdbc:mysql://localhost/sample"/>
<property name="JDBC.Username" value="user"/>
<property name="JDBC.Password" value="mypass"/>
<property name="Pool.MaximumActiveConnections"
value="10"/>
<property name="Pool.MaximumIdleConnections" value="5"/>
<property name="Pool.MaximumCheckoutTime"
value="120000"/>
<property name="Pool.TimeToWait" value="500"/>
<property name="Pool.PingQuery" value="select 1 from
ACCOUNT"/>
<property name="Pool.PingEnabled" value="false"/>
<property name="Pool.PingConnectionsOlderThan"
value="1"/>
<property name="Pool.PingConnectionsNotUsedFor"
value="1"/>
</dataSource>
</transactionManager>
<sqlMap resource="com/ibatis/sample/User.xml"/>
</sqlMapConfig>
2. POJO(Plain Ordinary Java Object)
下面是我們用作示例的一個(gè)POJO:
public class User implements Serializable {
private Integer id;
private String name;
private Integer sex;
private Set addresses = new HashSet();
/** default constructor */
public User() {
}
public Integer getId() {
return this.id;
IBATIS Developer’s Guide Version 1.0
September 2, 2004 So many open source projects. Why not Open your Documents?
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
public Integer getSex() {
return this.sex;
}
public void setSex(Integer sex) {
this.sex = sex;
}
}
Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=669112