html標籤庫
生成html元素的標籤
1)<html:link>相當於<a href>標籤
A)href:要鏈接的目的地。比如:<html:link href="a.jsp">進入a.jsp</html:link>
B)forward:要鏈接到全局轉發<html:link forward="msg" >this link</html:link>
首先要在struts-config.xml中配置全局轉發
<global-forwards>
<forward name="msg" path="/msg.jsp"/>
</global-forwards>
C)值得注意的是超連接也可以連接action。此時屬於get提交,因此不用ActionForm.
比如:
<html:link href="SumAction.do?n=4" >this link</html:link>
舉例:通過本頁面進行處理。之後把結果返回本頁面,而不是其它的頁面
創建一個Action,但是不需要創建ActionForm,然後配置此Action的forward
讓此Action處理完結果之後,跳轉回自己
D)該標籤最具有特色是可以把四種範圍內(request,page,session.applicaion)內的
變量或javabean的值做爲參數進行傳遞, 比如:
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屬性,該連接會自動的把javaBean的屬性取出來做爲get的提交值
舉例:通過數據庫取出員工的基本資料,然後顯示,注意放入刪除超連接
思路:通過javaBean結合超連接進行
2)<html:img src="ab.jgp"/>圖像標籤。相當於<img>標籤
生成html表單元素的標籤
1)<html:form action="Action.do">
A:用於生成表單
B:action用來指定跳轉到struts-config.xml中配置的Action
對於<html:form>的說明:
C:struts表單元素的標籤一定要嵌套在<html:form>標籤裏面,否則會出現錯誤
比如:
<form id="frm" action="testAction.do"> //這是html表單
單價:<html:text property="price"/>//而這是struts表單元素
</form>
D:一個Action可以沒有對應的ActionForm,但是<html:form action>對應的Action一定要
有ActionForm,否則會出現錯誤 比如:
<html:form action="testAction.do">
</html:form>
而struts-config.xml的Action配置如下:
<action path="/testAction" type="ht.NetBook.struts.Action.testAction" />
即表示testAction沒有與之對應的ActionFrom,運行時會出現以下錯誤:
Cannot retrieve definition for form bean null on action testAction.do
如果form Bean爲空,則不能編譯
E:<html:form>不需要加id或name屬性,它經過IE解釋之後其名字自動變成與之對應的
ActionForm的名字,這也就是爲什麼如果不爲Action指定ActionForm會出錯的原因
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裏面的文本框在表單提交以後值會依然保留這與一般的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中類型爲布爾類型的屬性關聯 比如:
public class discountActionForm extends ActionForm {
private boolean discount=true; //此處將會使checkbox自動勾選
}
則可以使用<html:checkbox property="discount"/>與該discount的關聯
說明:
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)該標籤默認情況是在頁面初始化時不會自動選中,可以設置ActionForm裏面的屬性默認
值爲true.比如:
舉例:讓用戶輸入商品單價與商品數量,實付款,用是否打折來顯示應付款與找零
10)<html:multibox/>產生複選框
與<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="音樂"/>音樂<br>
說明:
1)其中每個mulitbox的property值必須一樣,纔可以做爲一個複選框組。
2)property="love"中的love必須是在ActionForm中定義的數組
private String love[];
public String[] getLove() {
return love;
}
public void setLove(String[] love) {
this.love = love;
}
3)當用戶選中某個複選框後,會把該複選框的值存入ActionForm中的love數組中
4)與<html:checkbox>一樣,也存在同樣的缺點,就是如果選中之後取消勾選此時
還是會繼續勾選。解決的方案是增加一個隱藏域
<html:multibox property="love" value="唱歌"/>唱歌<br>
<html:multibox property="love" value="跳舞"/>跳舞<br>
<html:multibox property="love" value="足球"/>足球<br>
<html:multibox property="love" value="音樂"/>音樂<br>
<input type="hidden" name="love"/>//注意不要value=flae
5)加了隱藏域之後,struts會把隱藏域的值也加入到love數組中,所以在使用時,
應該去掉。for(int i=0;i<frm.getLove().length-1;i++) //長度減1去掉
舉例:
1)愛好,把人的愛好放入數據庫中保存
2)各模塊之間的權限管理
11)<html:radio>產生一組單選框
<html:radio property="degree" value="1">高中</html:radio><br>
<html:radio property="degree" value="2">中專</html:radio><br>
<html:radio property="degree" value="3">大學</html:radio><br>
<html:radio property="degree" value="4">小學</html:radio><br>
說明:
1)property的值必須與ActionForm中的某個屬性關聯,以後選擇了那個單選框
Struts就會與之對應的值賦給ActionForm的關聯的屬性
2)單選框不需要隱藏域,就可以正常的工作
3)如何要設置默認的選項,可以把ActionForm中關聯的屬性值設置成單選框中
與之對應的值
public class discountActionForm {
private String degree="1";//設置默認值。
}
這樣"高中"就默認選中了。因爲ActionForm中的degree關聯的值與"高中"單選框的值相同
舉例:
1)愛好
2)投票---要求從數據庫讀取投票項,進行投票
12) <html:select property="color">下拉列表框
A:一個<html:select>會包括多個<html:option>
B:property屬性值與ActionForm中對應的屬性關聯。
public class ActionForm extends ActionForm {
private String color;
get...
set...
}
C:<html:option>中有一個顯示值與實際值
<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>不用設置默認值,它會自動選中第一項
D:該類型的下拉列表一般適合於不與數據庫發生交互的情況
13)<html:options collection>該options非常適合於與數據庫進行交互
比如:<html:options collection="save" labelProperty="voteItem" property="id"/>
A:<html:options>標籤必須放在<html:select>裏面
B:collection表示Javabean的一個集合,該集合一定要放在四個範圍裏面
C:labelProperty表示顯示的值
D:property選中之後的值
比如:一個jsp頁面
<%
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(描述類vote)的定義
public class vote {
private String id;
private String voteItem;
get...
set...
}
管理類(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>來重做投車票項目
2)讓用戶選擇部門編號,查詢出對應的職工信息。要求部門用下拉框實現
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屬性。這個屬性和ActionForm中的
的FormFile類型的屬性對應
比如:
====================ActionForm========================
private private org.apache.struts.upload.FormFile file;
public get...
public set....
==========================jsp頁面===========================
<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; //服務器的實際文件路徑
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); //寫入到outputStream中
}
outputStream.close();
inputStream.close();
===========================inputStream.read的介紹==================
inputStream.read(byte b[],int off,int len)
讀取len個字節,放置到下標off開始的字節數組b中,返回實際讀取的字節的數量
一般off都是零,因爲是從數組的第一個位置開始填充