1)<html:link>相當于<a href>標簽
A)href:要鏈接的目的地。比如:<html:link href="a.jsp">進(jìn)入a.jsp</html:link>
B)forward:要鏈接到全局轉發(fā)<html:link forward="msg" >this link</html:link>
首先要在struts-config.xml中配置全局轉發(fā)
<global-forwards>
<forward name="msg" path="/msg.jsp"/>
</global-forwards>
C)值得注意的是超連接也可以連接action。此時(shí)屬于get提交,因此不用ActionForm.
比如:
<html:link href="SumAction.do?n=4" >this link</html:link>
舉例:通過(guò)本頁(yè)面進(jìn)行處理。之后把結果返回本頁(yè)面,而不是其它的頁(yè)面
創(chuàng )建一個(gè)Action,但是不需要創(chuàng )建ActionForm,然后配置此Action的forward
讓此Action處理完結果之后,跳轉回自己
D)該標簽最具有特色是可以把四種范圍內(request,page,session.applicaion)內的
變量或javabean的值做為參數進(jìn)行傳遞, 比如:
pageContext.setAttribute("uid","chen");
<html:link page="msg.jsp" paramId="uid" paramName="uid">this pram link</html:link>
paramId:指定get參數名
paramName:指定參數的值
<jsp:useBean id="sum" class="ht.NetBook.sumBean" scope="page"/>
<jsp:setProperty name="sum" property="n1" value="10"/>
<html:link href="msg.jsp" paramId="n1" paramName="sum" paramProperty="n1">
paramName:指定javaBean的名字
paramProperty:指定javaBean屬性,該連接會(huì )自動(dòng)的把javaBean的屬性取出來(lái)做為get的提交值
舉例:通過(guò)數據庫取出員工的基本資料,然后顯示,注意放入刪除超連接
思路:通過(guò)javaBean結合超連接進(jìn)行
2)<html:img src="ab.jgp"/>圖像標簽。相當于<img>標簽
1)<html:form action="Action.do">
A:用于生成表單
B:action用來(lái)指定跳轉到struts-config.xml中配置的Action
對于<html:form>的說(shuō)明:
C:struts表單元素的標簽一定要嵌套在<html:form>標簽里面,否則會(huì )出現錯誤
比如:
<form id="frm" action="testAction.do"> //這是html表單
單價(jià):<html:text property="price"/>//而這是struts表單元素
</form>
D:一個(gè)Action可以沒(méi)有對應的ActionForm,但是<html:form action>對應的Action一定要
有ActionForm,否則會(huì )出現錯誤 比如:
<html:form action="testAction.do">
</html:form>
而struts-config.xml的Action配置如下:
<action path="/testAction" type="ht.NetBook.struts.Action.testAction" />
即表示testAction沒(méi)有與之對應的ActionFrom,運行時(shí)會(huì )出現以下錯誤:
Cannot retrieve definition for form bean null on action testAction.do
如果form Bean為空,則不能編譯
E:<html:form>不需要加id或name屬性,它經(jīng)過(guò)IE解釋之后其名字自動(dòng)變成與之對應的
ActionForm的名字,這也就是為什么如果不為Action指定ActionForm會(huì )出錯的原因
F:結論:如果要使用struts表單元素必須要放在<html:form>中,而此
<html:form>對應的Action一定要有ActionForm才可以正常運行,
如果不使用struts表單元素,也可以使用普通的html標簽,同樣
可以與struts結合,但是struts里面的有些標簽更加智能
3)<html:button property="register"/>生成普通的按鈕
4)<html:submit>提交</html:submit>生成提交按鈕
5)<html:reset>重置</html:reset>生成重置按鈕
6)<html:text property="uid"/>用于生成文本框
A:prperty中的值一定要與ActionForm中的屬性值保持一致
B:struts里面的文本框在表單提交以后值會(huì )依然保留這與一般的html標簽不一樣
C:可以使用<html:text property="uid" value="值"/>的方法賦值給文本框
7)<html:reset>復位</html:reset>與普通html的重置按鈕一樣
8)<html:submit>提交</html:submit>與普通的html的提交按鈕一致
9)<html:checkbox>相當于html的checkbox
格式:<html:checkbox property="discount"/>
其中的property中的屬性名一定要與ActionForm中類(lèi)型為布爾類(lèi)型的屬性關(guān)聯(lián) 比如:
public class discountActionForm extends ActionForm {
private boolean discount=true; //此處將會(huì )使checkbox自動(dòng)勾選
}
則可以使用<html:checkbox property="discount"/>與該discount的關(guān)聯(lián)
說(shuō)明:
1)該標簽一定要配合普通隱藏域標簽才能夠正常使用,否則只有勾選一次,以后不管是否勾選其值都是true
<html:checkbox property="discount"/>
<input type="hidden" name="discount" value="flase">
2)可以Action中的Excecute方法里面。去檢查discount的值
discountActionForm frm=(discountActionForm)form;
if (frm.getDiscount()) {做一些事件}
3)該標簽默認情況是在頁(yè)面初始化時(shí)不會(huì )自動(dòng)選中,可以設置ActionForm里面的屬性默認
值為true.比如:
舉例:讓用戶(hù)輸入商品單價(jià)與商品數量,實(shí)付款,用是否打折來(lái)顯示應付款與找零
10)<html:multibox/>產(chǎn)生復選框
與<html:checkbox>的區別在于,它可以與ActionForm中數組屬性對應 比如:
<html:multibox property="love" value="唱歌"/>唱歌<br>
<html:multibox property="love" value="跳舞"/>跳舞<br>
<html:multibox property="love" value="足球"/>足球<br>
<html:multibox property="love" value="音樂(lè )"/>音樂(lè )<br>
說(shuō)明:
1)其中每個(gè)mulitbox的property值必須一樣,才可以做為一個(gè)復選框組。
2)property="love"中的love必須是在A(yíng)ctionForm中定義的數組
private String love[];
public String[] getLove() {
return love;
}
public void setLove(String[] love) {
this.love = love;
}
3)當用戶(hù)選中某個(gè)復選框后,會(huì )把該復選框的值存入ActionForm中的love數組中
4)與<html:checkbox>一樣,也存在同樣的缺點(diǎn),就是如果選中之后取消勾選此時(shí)
還是會(huì )繼續勾選。解決的方案是增加一個(gè)隱藏域
<html:multibox property="love" value="唱歌"/>唱歌<br>
<html:multibox property="love" value="跳舞"/>跳舞<br>
<html:multibox property="love" value="足球"/>足球<br>
<html:multibox property="love" value="音樂(lè )"/>音樂(lè )<br>
<input type="hidden" name="love"/>//注意不要value=flae
5)加了隱藏域之后,struts會(huì )把隱藏域的值也加入到love數組中,所以在使用時(shí),
應該去掉。for(int i=0;i<frm.getLove().length-1;i++) //長(cháng)度減1去掉
舉例:
1)愛(ài)好,把人的愛(ài)好放入數據庫中保存
2)各模塊之間的權限管理
11)<html:radio>產(chǎn)生一組單選框
<html:radio property="degree" value="1">高中</html:radio><br>
<html:radio property="degree" value="2">中專(zhuān)</html:radio><br>
<html:radio property="degree" value="3">大學(xué)</html:radio><br>
<html:radio property="degree" value="4">小學(xué)</html:radio><br>
說(shuō)明:
1)property的值必須與ActionForm中的某個(gè)屬性關(guān)聯(lián),以后選擇了那個(gè)單選框
Struts就會(huì )與之對應的值賦給ActionForm的關(guān)聯(lián)的屬性
2)單選框不需要隱藏域,就可以正常的工作
3)如何要設置默認的選項,可以把ActionForm中關(guān)聯(lián)的屬性值設置成單選框中
與之對應的值
public class discountActionForm {
private String degree="1";//設置默認值。
}
這樣"高中"就默認選中了。因為ActionForm中的degree關(guān)聯(lián)的值與"高中"單選框的值相同
舉例:
1)愛(ài)好
2)投票---要求從數據庫讀取投票項,進(jìn)行投票
12) <html:select property="color">下拉列表框
A:一個(gè)<html:select>會(huì )包括多個(gè)<html:option>
B:property屬性值與ActionForm中對應的屬性關(guān)聯(lián)。
public class ActionForm extends ActionForm {
private String color;
get...
set...
}
C:<html:option>中有一個(gè)顯示值與實(shí)際值
<html:select property="color">
<html:option value="yellow">黃色</html:option>
<html:option value="green">綠色</html:option>
<html:option value="blue">藍色</html:option>
</html:select>
D:<html:select>不用設置默認值,它會(huì )自動(dòng)選中第一項
D:該類(lèi)型的下拉列表一般適合于不與數據庫發(fā)生交互的情況
13)<html:options collection>該options非常適合于與數據庫進(jìn)行交互
比如:<html:options collection="save" labelProperty="voteItem" property="id"/>
A:<html:options>標簽必須放在<html:select>里面
B:collection表示Javabean的一個(gè)集合,該集合一定要放在四個(gè)范圍里面
C:labelProperty表示顯示的值
D:property選中之后的值
比如:一個(gè)jsp頁(yè)面
<%
java.util.List save=votes.getAllvote();//返回投票的javabean的集合
request.setAttribute("save",save);
%>
<html:select property="id"> //此處的id一定要與ActionForm中的屬性對應
<html:options collection="save" labelProperty="voteItem" property="id"/>
</html:select> //此處不能用${save}
ActionForm的定義
public class voteActionForm extends ActionForm {
private String id;
get...
set...
}
JavaBean(描述類(lèi)vote)的定義
public class vote {
private String id;
private String voteItem;
get...
set...
}
管理類(lèi)(votes)的定義
pulbic class votes{
public static List getAllvote() {
vote newInstance=new vote();
newInstance.setId(rs.getInt(1));
newInstance.setVoteItem(rs.getString(2));
list.add(newInstance);
return list;
}
}
舉例:
1)用<html:select>結合<html:options>來(lái)重做投車(chē)票項目
2)讓用戶(hù)選擇部門(mén)編號,查詢(xún)出對應的職工信息。要求部門(mén)用下拉框實(shí)現
10)<html:file property="file">文件上傳組件
A:<html:file>必須嵌套在<html:form>標簽中
B:<html:form>標簽的method必須為post提交
C:<html:form>中的enctype必須為multipart/form-data method="post"
D:<html:file>標簽必須設置property屬性。這個(gè)屬性和ActionForm中的
的FormFile類(lèi)型的屬性對應
比如:
====================ActionForm========================
private private org.apache.struts.upload.FormFile file;
public get...
public set....
==========================jsp頁(yè)面===========================
<html:form action="SumAction.do" method="post" enctype="multipart/form-data" >
<html:file property="file"/>注意這里面的file與ActionForm中的file屬性對應
<html:submit>提交</html:submit>
</html:form>
============================Action===========================
uploadActionForm frm=(uploadActionForm)from;
FormFile file=frm.getFile();//得到file對像
String fileName=file.getFileName();//得到要上傳的文件名
String dir=servlet.getServletContext().getRealPath("upload");
//得到文件的路徑,這里面統一上傳到upload文件夾下面(WebRoot\upload)
String serverPath=dir+"\\"+fileName; //服務(wù)器的實(shí)際文件路徑
InputStream inputStream=file.getInputStream();//輸入流
OutputStream outputStream=new FileOutputStream(serverPath);//輸出流
int readBinary=0;
byte buffer[]=new byte[8192];//緩沖區為1024*8也就是8字節
while((readBinary=inputStream.read(buffer, 0, 8192))!=-1) {
//從inputStream中每次讀取8字節的的數據到byte數據組中
outputStream.write(buffer, 0, readBinary); //寫(xiě)入到outputStream中
}
outputStream.close();
inputStream.close();
===========================inputStream.read的介紹==================
inputStream.read(byte b[],int off,int len)
讀取len個(gè)字節,放置到下標off開(kāi)始的字節數組b中,返回實(shí)際讀取的字節的數量
一般off都是零,因為是從數組的第一個(gè)位置開(kāi)始填充
聯(lián)系客服