1.<html:html>標籤:
<html:html>標籤用於在網頁開頭生成HTML的<html>元素,它只有一個用於顯示用戶語言的lang屬性: <html:html lang="true">2.<html:base>標籤<html:base>標籤在網頁的<head>部分生成HTML<base>元素。HTML<base>元素用於生成當前網頁的絕對URL路徑3.<html:link>和<html:rewrite>標籤<html:link>標籤用於生成HTML<a>元素。<html:link>在創建超鏈接時,有兩個優點:● 允許在url中以多種方式包含請求參數。● 當用戶瀏覽關閉Cookie時,會重寫url,把SessionId作爲請求參數包含在URL中,用於跟蹤用戶的Session狀態。<html:bace>標籤有以下重要屬性:● forward :指定全局轉發鏈接● href:指定完整的URL鏈接● page: 指定相對於當前網頁的URL<html:rewrite>用於輸出超鏈接中的URl部分,但它並不生成HTML<a>元素。 URL指的是URL中的協議,主機和端口以後的內容,URL用於指定具體的資源。例如:對於urlhttp://localhost:8080/htmltaglibs/HtmlBasic.do,它的url爲/htmltaglibs/HtmlBasic.do。以下用一些例子演示如何使用<html:link>和<html:rewrite>標籤:1>.創建全局轉發鏈接首先在struts配置文件的<global-forwords>元素中定義一個<forward>元素:<global-forwords><forword name="index" path="/index.jsp"></global-forwords>接着在JSP文件中創建<html:link>標籤:<html:link forward="index">Link to Global ActionFoward</html:link><html:link>中的forward屬性和<global-forwords>元素中的<forward>子元素匹配。以上代碼生成如下html內容:<a href="/htmltaglibs/index.jsp">Link to Global ActionFoward</a>2>.創建具有完整URL的鏈接如果web站點需要鏈接到其他站點,應該給出其他站點的完整URL,例如:<html:link href="http://jakarta.apache.org/struts/index.html">Generate an "href" directly</html:link>以上代碼生成:<a href="http://jakarta.apache.org/struts/index.html">Generate an "href" directly</a>3>.從當前網頁中創建相對URL如果從一個網頁鏈接到同一個應用中的另一個網頁(相對地址跳轉),可採用:<html:link page="HtmlBasics.do">A relative link from this page</html:link>生成 HTML 代碼如下:<a href="htmltaglibs/HtmlBasics.do">A relative link from this page</a>4>.在url或url中包含請求參數<html:link page="HtmlBasics.do?prop1=abc&prop2=123">Hard-code the url parameters</html:link>或rewrite:<html:rewrite page="HtmlBasics.do?prop1=abc&prop2=123"/>生成以下代碼:<a page="/htmltaglibs/HtmlBasics.do?prop1=abc&prop2=123">Hard-code the url parameters</a>rewrite:/htmltaglibs/HtmlBasics.do?prop1=abc&prop2=1234.<html:img>標籤1>.生成基本的HTML<img>元素<html:img page="/struts-power.gif"/>生成 html代碼如下:<img src="/htmltaglibs/struts-power.gif">Html標籤庫中基本的表單標籤:1.<html:form>標籤<html:form action="FormBasic.do">生成html代碼:<form name="FormBasicAction" mothod="post" action="/htmltaglibs/FormBasic.do/">Struts將參照Struts配置文件來查找相應的Action組件,在struts-config.xml文件中,與"FormBasic.do"對應的代碼爲:<action path="/FormBasic"type="htmltaglibs.actions.FormBasicAction"name="FormBacsicForm"scope="session"input="/FormBasic.jsp"validate="false"><foreard name="success" path="/FormBasic.jsp"></action>2.<html:text>標籤在表單上創建HTML文本框字段。如下:<html:text property="userName">因爲指定了一個userName的屬性名,它應該匹配ActionForm 中的一個屬性,所一在FormBasicForm 中也必須有uerName這個屬性和相應的getter(),setter()方法。當表單提交時,struts框架會把userName字段的內容賦給form的userName屬性。3.<html:cancel>標籤<html:cancel>標籤在表單中生成取消按鈕,當用戶按下取消按鈕使,將產生一個取消事件,這個事件由Action類來捕獲,至於如何處理這個事件,可以在Action類的execute()方法中編程來完成。這裏不再詳細說明:創建按鈕:<html:cancel>Cancel</html:cancel>4.<html:reset>標籤<html:reset>標籤生成表單的復位按鈕,使用方法如下:<html:reset></html:reset>這個標籤的用法很簡單,它和html元素<input type="reset">在功能上相同。5.<html:submit>標籤<html:submit>Submit</html:submit>6.<html: hidden>標籤在表單上生成隱藏字段,存放用戶不希望看到和不允許修改的信息:如下兩種方式:一.<html:hidden property="userName">生成Html:<input type="hidden" name="userName" value="propValue">二.添加了write屬性<html:hidden property="userName" write="true"/>生成Html:<input type="hidden" name="userName" value="propValue">propValue檢查框和單選按鈕標籤:下面就簡單說一下在HTML表單上生成檢查框和單選按鈕的標籤,這些標籤必須嵌套在<html:form>標籤中:1.<html:checkbox>標籤<html:checkbox>標籤在表單上生成標準的HTML檢查框,例如ActionForm Bean 中的某個屬性只有兩種可選值(如true和false),就可以在表單中用<html:checkbox>標籤來表示。<html:checkbox>的使用方法爲:<html:checkbox property="ch1"/>生成HTML代碼:<input type="checkbox" name="cb1" value="true">a.jap包含兩個<html:checkbox>標籤,它們分別和AForm Bean中的ch1和ch2屬性關聯。在Bean中,ch1和ch2屬性必須定義爲boolean類型<html:checkbox>有一個value屬性,用來設置用戶選中檢查框時的值,value的默認值爲true,可以用以下方式改變value屬性:<html:checkbox property="ch1" value="true"/>以上代碼說明當用戶選擇了這個檢查框,就把相應Bean中的ch1屬性設置爲true.其實這樣也會容易讓人搞混,例如當value="false"時,如果用戶沒有選擇這個檢查框,就把Bean中對應的屬性設置爲true.爲了檢查框能正常工作,必須在Bean的reset()方法中對其進行復位,當<html:checkbox>的 value屬性爲true時,必須在reset()方法中把對應的屬性設置爲false.當<html:checkbox>的 value屬性爲false時,必須在reset()方法中把對應的屬性設置爲true.以下演示一下ch1屬性復位:this.setCh1(false);//Note:With checkbox2 nerver reset here,it won't ever appear "unset"//this.setCh2(false)以上reset()方法沒有復位ch2的值,在這種情況下,會導致ch2無法正常工作。一旦用戶選擇了ch2,以後ch2將永遠爲選中狀態,即使用戶取消了選擇,ch2的值仍然爲true,如圖:首次 ch1=false 選中ch1和ch2 ch1=true 取消選中ch1和 ch1=false● → → →訪問 ch2=false 後提交表單 ch2=true ch2後提交表單 ch2=truech1和ch2的狀態圖2.<html:multibox>標籤<html:multibox>標籤和<html:checkbox>一樣,可以提供html<input type="ch1">元素,區別在於<html:multibox>可以生成複選框,它和Form 的關聯方式不一樣。如果應用中有多個CheckBox,並且希望在Form中用單個數組來表示它們,就可以採用<html:multibox>.<html:multibox>的使用方法如下:1>.在Form中定義一個數組,來存放所有的 CheckBox的值:private String strArray[]=new String[0];public String[] getStrArray(){return (this.strArray);}public void setStrArray(String strArray[]){this.strArray=strArray;}2>.其次在表單中加入<html:multibox>元素,通過設置property="strArray"來把它和Form中的數組關聯。3>.對於每個<html:multibox>元素,設置它的初始值,有以下兩種方式:<html:multibox property="strArray" value="Value1"/>或<html:multibox property="strArray">Value2</html:multibox>當用戶提交表單時,所有被選中的複選框的值都會被存放在Form中的相應數組中。如果某個複選框沒有被選中,那麼數組就不會包含它的值,例如,如果用戶選擇了上例的兩個複選框,那麼數組的內容爲{"Value1","Value2"}.3.<html:radio>標籤<html:radio>標籤提供HTML<input type="radio">元素,表示單選按鈕,多個<html:radio>標籤可以成組使用,如下:<html:radio property="r1" value="v1"/><html:radio property="r1" value="v2"/>以上標籤的property屬性相同,而僅僅是value不同,它們都和Form中的V1屬性對應,生成的HTML如下:<input type="radio" name="r1" value="v1"><input type="radio" name="r1" value="v2">1.<html:select>標籤<html:select>標籤生成HTML<select>元素。它可以在表單上創建下拉列表或多選列表。在<html:select>標籤中可以包含多個<html:option>,<html:options>和<html:optionCollection>標籤。<html:select>標籤的基本形式爲:
<html:select>標籤有以下重要屬性:▲size屬性:指每次在網頁上顯示的可選項的數目。▲multiple屬性:指定是否支持多項選擇,如果設置爲true,就表示多選列表,支持多項選擇;否則表示下拉列表,只支持單項選擇,默認爲false.▲property屬性:與ActionForm Bean中的對應屬性對應,這個屬性用來存放用戶在列表上選中選項的值,在單選的情況下,Bean中的對應屬性應該定義爲簡單類型(不能爲數組)。在多項選擇的情況下,Bean中的對應屬性應該定義爲數組類型,以便存放用戶選擇的多個選項。例如:在 cust.jsp中的客戶列表爲下拉列表,顏色列表爲多選列表:<html:select property="custId"/>
<html:select property="colors" multiple="true" size="6"/>對應的Bean爲:private int custId;
private String colors[];
public String[] getColors() {
return colors;
}
public void setColors(String[] colors) {
this.colors = colors;
}
public int getCustId() {
return custId;
}
public void setCustId(int custId) {
this.custId = custId;
}2.<html:option>標籤<html:option>標籤生成HTML<option>元素,這個標籤被嵌套在<html:select>標籤中,代表列表的一個可選項,它的label有兩個來源:▲ 在<html:option>和</html:option>之間的文本內容。例如:<html:select property="colors" size="6" multiple="true"><!--在<html:option>和</html:option>之間的文本內容-->
<html:option value="a.orange">Orange</html:option>
<html:option value="a.purple">Purple</html:option><!--由<html:option>標籤的key,locale和bundle屬性指定的Resource Bundle中的內容-->
<html:option value="a.red"bundle="a.Colors"key="a.red"></html:option>
<html:option value="a.blue" bundle="a.Colors"key="a.blue"></html:option>
</html:select>3.<html:options>標籤<html:options>標籤提供一組HTML<option>元素。在<html:select>中可以包含多個<html:options>元素,如下:<html:select property="colors" size="6" multiple="true">
.......
<html:options collection="colorCollection" property="value"labelProperty="label"/></html:select>當然在這個jsp頁面之前還定義了colorCollection集合,被存放在page範圍中:<%
Vector colorCollection=new Vector();
colorCollection.add(new org.apache.struts.util.LabelValueBean("Pink","a.pink"));colorCollection.add(new org.apache.struts.util.LabelValueBean("Brown","a.brown"));
pageContext.setAttribute("colorCollection",colorCollection);
%>4.<html:optionCollection>標籤在<html:select>元素中可包含多個<html:optionCollection>元素:如下是包含一個<html:optionCollection>標籤:<html:select property="custId">
<html:optionsCollection property="customers" label="name"value="custId"/></html:select>以下爲Form類中定義customers屬性的代碼:private CustomerBean customers[];
public CustomerBean[] getCustomers() {
return customers;
}
public void setCustomers(CustomerBean[] customers) {
this.customers = customers;
}以下爲CustomerBean中定義name和custId屬性的代碼:private int custId;
private String name;
private String[] favColors=new String[0];
public CustomerBean(){
}
public int getCustId() {
return custId;
}
public void setCustId(int custId) {
this.custId = custId;
}
public String[] getFavColors() {
return favColors;
}
public void setFavColors(String[] favColors) {
this.favColors = favColors;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
在表單中上傳文件標籤:看一下下面這個例子我想你就會用了,非常easy:
要想實現上面的結果,請看下面的代碼,HtmlFile.jsp的代碼:<%@ page language="java" pageEncoding="UTF-8" contentType="text/html; charset=UTF-8"%>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-bean" prefix="bean" %>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-html" prefix="html" %>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-logic" prefix="logic" %><html:html locale="true">
<body><html:form action="htmlFile.do" enctype="multipart/form-data">
Please select the file that you would lile to upload:<br/>
<html:file property="file"/><br/><br/>
<html:submit></html:submit>
</html:form><logic:notEmpty name="htmlFileForm" property="fname">
The file just uploaded was:<p>
<ul>
<li>Name:<bean:write name="htmlFileForm" property="fname"/></li>
<li>Size:<bean:write name="htmlFileForm" property="size"/></li>
</ul>
</logic:notEmpty>
</body>
</html:html>與HtmlFile.jsp網頁上對應的ActionForm爲HtmlFileForm bean,如下爲HtmlFileForm.java的源程序:
package com.lcf.struts.form;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.upload.FormFile;public class HtmlFileForm extends ActionForm {
private String fname;private String size;
private FormFile file;public FormFile getFile() {
return file;
}public void setFile(FormFile file) {
this.file = file;
}public ActionErrors validate(ActionMapping mapping,
HttpServletRequest request) {
return null;
}public void reset(ActionMapping mapping, HttpServletRequest request) {
// TODO Auto-generated method stub
}public String getFname() {
return fname;
}public void setFname(String fname) {
this.fname = fname;
}public String getSize() {
return size;
}public void setSize(String size) {
this.size = size;
}
}
具體的文件上傳操作由HtmlFileAction來完成,如下爲源代碼:
package com.lcf.struts.action;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.upload.FormFile;
import org.omg.CORBA_2_3.portable.OutputStream;import com.lcf.struts.form.HtmlFileForm;
public class HtmlFileAction extends Action {
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
String dir=servlet.getServletContext().getRealPath("/upload");
HtmlFileForm htmlFileForm = (HtmlFileForm) form;
FormFile ff=htmlFileForm.getFile();
if(ff==null){
return mapping.findForward("ok");
}
String fname=ff.getFileName();
String size=Integer.toString(ff.getFileSize())+"bytes";
try {
InputStream streanIn=ff.getInputStream();
FileOutputStream streamOut=new FileOutputStream(dir+"/"+fname);
int bytesRead=0;
byte[] buffer=new byte[8192];
while((bytesRead=streanIn.read(buffer,0,8192))!=-1){
streamOut.write(buffer,0,bytesRead);
}
streanIn.close();
streamOut.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
htmlFileForm.setFname(fname);
htmlFileForm.setSize(size);
ff.destroy();
return mapping.findForward("ok");
}
}
當然了在做的同時還要注意以下幾點,否則就會出問題了:
1.<html:file>標籤必須嵌套在<html:form>標籤中。
2.<html:from>標籤的method屬性必須設置爲"POST".
3.<html:from>標籤的編碼類型enctype屬性必須爲"multipart/form-data".
4.<html:file>標籤必須設置爲property屬性,這個屬性和Bean中FormFile類型的屬性對應。
Struts Html標籤庫
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.