官方文檔
:
http://struts.apache.org/1.2.9/userGuide/struts-html.html
與form相關的標簽包括?標簽本身以及所有必須包含在其中的標簽。比如,和標簽就是和form相關的標簽,因為如果不把它們放到一個form中它們就沒有意義。
<form>標簽
標簽用于生成一個HTML?form。使用該標簽時必須遵循許多規則。
首先,標簽中必須包含一個action屬性,它是這個標簽中唯一必需的屬性。如果不具備該屬性則JSP頁面會拋出一個異常。之后你必須給這個action屬性指定一個有效值。一個有效值是指應用程序的Struts配置文件中元素里的任何一個子元素的訪問路徑。而且相應的元素中必須有一個name屬性,它的值是form?bean的名稱。
例如,如果你有這樣一個標簽:??<html:form?action=\"/login\"?>?
?
那么你的Struts配置文件的元素中必須有一個如下顯示為粗體的元素:?????<action-mappings>?
?????<action?path=\"/login\"?
??????type=\"com.javapro.struts.LoginAction\"??
??????name=\"loginForm\"
??????scope=\"request\"
??????input=\"/login.jsp\">
??????<forward?name=\"success\"?path=\"/mainMenu.jsp\"/>
????</action>
????.
????.
????.
??</action-mappings>
這就是說一個form標簽是和form?bean相關聯的。
另一個要遵循的規則是:任何包含在<form>中用來接收用戶輸入的標簽(<text>、<password>、<hidden>、<textarea>、<radio>、<checkbox>、<select>)必須在相關的form?bean中有一個指定的屬性值。比如,如果你有一個屬性值被指定為“username”的<text>標簽,那么相關的form?bean中也必須有一個名為“username”的屬性。輸入<text>標簽中的值會被用于生成form?bean的userName屬性。
除了上面所講的屬性之外,<form>標簽還有一些不是必須但加上更好的“次要”的屬性。比如,你可以用focus屬性來生成JavaScript,它會“定焦”(focus)到該form所包含的一個元素上。使用focus屬性時你需要給它指定元素的名稱。比如,以下代碼是定焦在第二個Text元素上的:?<body>
<html:form?action=\"/login\"?focus=\"password\">
User?Name:?<html:text?property=\"userName\"/>
<br>Password:?<html:text?property=\"password\"/>
<br><html:submit/>
</html:form>
</body>
該段代碼會被轉換成:?<body>
<form?name=\"loginForm\"?method=\"post\"?
???action=\"/myStrutsApp6/login.do\">
User?Name:?<input?type=\"text\"?name=\"userName\"?
???value=\"\">
<br>Password:?<input?type=\"text\"?
???name=\"password\"?value=\"\">
<br><input?type=\"submit\"?
???value=\"Submit\">
</form>
<script?language=\"JavaScript\"?
???type=\"text/javascript\">
??<!--
?if?(document.forms[\"loginForm\"].elements[
??????\"password\"].type?!=?\"hidden\")?
????document.forms[\"loginForm\"].elements[
??????\"password\"].focus()
??//?-->
</script>
</body>
注意,<form>標簽中method屬性的缺省值是POST。另外,有沒有看到這個標簽庫是如何建立JavaScript來定焦到password元素上的??這也是該庫讓人著迷的地方之一。你不用擔心如何在客戶端進行編程,它會幫你自動生成。
在運行前面的例子時還要注意,你必須有一個包含userName和password屬性的相關form。?
<text>標簽
<text>標簽用于生成一個文本的輸入區域。它必須包含和相關form?bean中的相同屬性對應的“property”屬性。該標簽只有在嵌入到一個<form>標簽中時才有效。
例如:?<html:text?property=\"userName\"/>
會被轉換成:?<input?type=\"text\"?name=\"userName\"?value=\"\">
<password>標簽
<password>標簽用于生成一個口令字(type?password)的輸入區域。它必須包含和相關form?bean中的相同屬性對應的“property”屬性。該標簽只有在嵌入到一個<form>標簽中時才有效。該標簽中的一個很重要的屬性是“redisplay”,它用于重新顯示以前輸入到這個區域中的值。該屬性的缺省值為true。然而,為了使password不能被重新顯示,你或許希望將該屬性的值設為false。
例如:?<html:password?property=\"password\"?
???redisplay=\"false\"/>
會被轉換成:?<input?type=\"password\"?name=\"password\"?
???value=\"\">
<hidden>標簽
<hidden>標簽用于生成一個隱藏文本的輸入區域。它必須包含和相關form?bean中的相同屬性對應的“property”屬性。該標簽只有在嵌入到一個<form>標簽中時才有效:
例如:?<html:hidden?property=\"userName\"/>
會被轉換成:?<input?type=\"hidden\"?name=\"userName\"?value=\"\">
<textarea>標簽
<textarea>標簽用于生成一個文本區元素(text?area?element)。它必須包含和相關form?bean中的相同屬性對應的“property”屬性。
比如:?<html:textarea?property=\"details\"?
??cols=\"80\"
??rows=\"20\"
??value=\"Enter?details?here\"/>
會被轉換成:?<textarea?name=\"details\"?cols=\"80\"?
??rows=\"20\">Enter?details?here</textarea>
<radio>標簽
<html:radio>標記用來顯示HTML單選鈕控件,屬性如下:
屬性 | 描述 |
Name | Bean的名稱,其屬性會被用來確定單選鈕是否以選中的狀態顯示。如果沒有設置,將使用與這個內嵌表單相關的ActionFrom bean的名稱。 |
property | 當表單被提交時送回到服務器的請求參數的名稱,以及用來確定單選鈕是否以被選中狀態進行顯示的bean屬性的名稱 |
Value | 當單選鈕被選中時返回到服務器的值 |
<radio>標簽用于顯示一個單選按鈕(radio?button)。它必須包含“value”屬性。比如這段代碼:?<html:radio?property=\"title\"?value=\"1\"/>Mr.
<html:radio?property=\"title\"?value=\"2\"/>Ms.
<html:radio?property=\"title\"?value=\"3\"/>Dr.
會被轉換成這樣的HTML:?<input?type=\"radio\"?name=\"title\"?
???value=\"1\">Mr.
<input?type=\"radio\"?name=\"title\"?
???value=\"2\">Ms.
<input?type=\"radio\"?name=\"title\"?
???value=\"3\">Dr.
<checkbox>標簽
<checkbox>標簽用于顯示checkbox類型的輸入區域。比如:?<html:checkbox?property=
???\"notify\"/>Please?send?me?notification
會被轉換成:?<input?type=\"checkbox\"?name=\"notify\"?
???value=\"on\">Please?send?me?notification
<submit>標簽
<submit>標簽用于生成提交類型(type?submit)的輸入區域。比如:?<html:submit?value=\"Login\"/>
會被轉換成:?<input?type=\"submit\"?value=\"Login\">
<reset>標簽
<reset>標簽用于生成重置類型(type?reset)的輸入區域。比如:?<html:reset/>
會被轉換成:?<input?type=\"reset\"?value=\"Reset\">
<option>標簽
<option>標簽用于顯示select?box中的一個選項。參照下面的<select>標簽。
<select>標簽?
<select>標簽用于顯示一個包含零個或更多選項的HTML選擇元素。它必須被嵌入一個<form>標簽中才有效。下面這個例子將說明如何使用該標簽:?<html:select?property=\"color\"?size=\"3\">
??<html:option?value=
??????\"r\">red</html:option>
??<html:option?value=
??????\"g\">green</html:option>
??<html:option?value=
??????\"b\">blue</html:option>
</html:select>
會被轉換成:?<select?name=\"color\"?size=\"3\"><option?
??????value=\"r\">red</option>
??<option?value=\"g\">green</option>
??<option?value=\"b\">blue</option>
</select>
<html:file>標簽
<html:file>標簽需要注意以下幾點:
a:<html:file>必須嵌套在<html:form>標簽中.
b:<html:form>標簽的method屬性必須設為"post".
c:<html:form>標簽的編碼類型enctype屬性必須設為"multipart/form-data".
d:<html:file>標簽必須設置property屬性,這個屬性和ActionForm Bean中FormFile類性的屬性對應.
在ActionForm Bean中設置FormFile屬性
/**
*The file that the user has uploaded
*/
private FormFile file;
public FormFile getFile(){
???return this.file;
}
public void setFile(FormFile file){
???this.file? file;
}
在Action類中處理文件上傳
String dir = servlet.getServletContext().getRealPath("/upload");
HtmlFileForm hff = (HtmlFileForm)form;
FormFile file = hff.getFile();
if(file == null)
???? return mapping.findForward("error");
String fname = file.getFileName();
String size = Integer.toString(file.getFileSize())+"bytes";
InputStream streamIn = file.getInputStream();
OutputStream streamOut = new FileOutputStream(dir+"/"+fname);
int bytesRead = 0;
byte[] buffer = new byte[8192];
while((bytesRead = streamIn.read(buffer,0,8192)) != -1){
???streamOut.write(buffer,0,bytesRead);
}
streamOut.clase();
streamIn.close();