Struts中html:options的使用
html:options是Struts中比較複雜的一個tage lib,用法靈活,但是Sturts提供的原始碼exercise taglib中沒有提出常用jsp+ActionForm這樣形式的最直接的總結,現從中總結如下,分兩種情況:陣列和Collection。
需求,要達到:
要實現上述效果,需要兩步:
第一:設定ActionForm,
也分兩小步:第一小步必須在ActionForm中,有一句
private Collection beanCollection;
public Collection getBeanCollection();
Collection beanCollection要確保是一個實現,如ArrayList,如果不是則會報No collection found的錯誤,Struts的最大不方便就是一旦出問題,定位很難,不知道什麼地方使用錯誤,或忘記設定什麼了。
因為前面需求中option的value值和label值不一樣,那麼在beanCollection中儲存的就是一個value和label組成的物件,名為LabelValueBean,在LabelValueBean中有兩個屬性value和label,
在程式某個地方要為beanCollection賦值,如:
然後執行setBeanCollection(entries);
這樣ActionForm中的beanCollection算有值了。
第二小步,需要設定Selected,selected有兩種,單選和多選:
在ActionForm中必須有:
或多選,多選必須是陣列:
第二:在Jsp中寫入tang lib語句如下:
其中testbean是ActionForm的名稱。
以上是html:options的Collection解決方案,如果option值很少,簡單地可以實現為陣列,兩步:
第一:在ActionForm中,
第二步在jsp中:
需求,要達到:
<select name="beanCollectionSelect" multiple="multiple" size="10"><option value="Value 0">Label 0</option> <option value="Value 1" selected="selected">Label 1</option> <option value="Value 2">Label 2</option> <option value="Value 3" selected="selected">Label 3</option> <option value="Value 4">Label 4</option> <option value="Value 5" selected="selected">Label 5</option> <option value="Value 6">Label 6</option> <option value="Value 7">Label 7</option> <option value="Value 8">Label 8</option> <option value="Value 9">Label 9</option></select> <p class="indent"> |
要實現上述效果,需要兩步:
第一:設定ActionForm,
也分兩小步:第一小步必須在ActionForm中,有一句
private Collection beanCollection;
public Collection getBeanCollection();
Collection beanCollection要確保是一個實現,如ArrayList,如果不是則會報No collection found的錯誤,Struts的最大不方便就是一旦出問題,定位很難,不知道什麼地方使用錯誤,或忘記設定什麼了。
因為前面需求中option的value值和label值不一樣,那麼在beanCollection中儲存的就是一個value和label組成的物件,名為LabelValueBean,在LabelValueBean中有兩個屬性value和label,
在程式某個地方要為beanCollection賦值,如:
Vector entries = new Vector(10); entries.add(new LabelValueBean("Label 0", "Value 0")); entries.add(new LabelValueBean("Label 1", "Value 1")); entries.add(new LabelValueBean("Label 2", "Value 2")); entries.add(new LabelValueBean("Label 3", "Value 3")); entries.add(new LabelValueBean("Label 4", "Value 4")); entries.add(new LabelValueBean("Label 5", "Value 5")); entries.add(new LabelValueBean("Label 6", "Value 6")); entries.add(new LabelValueBean("Label 7", "Value 7")); entries.add(new LabelValueBean("Label 8", "Value 8")); entries.add(new LabelValueBean("Label 9", "Value 9")); |
然後執行setBeanCollection(entries);
這樣ActionForm中的beanCollection算有值了。
第二小步,需要設定Selected,selected有兩種,單選和多選:
在ActionForm中必須有:
private String singleSelect = "Single 5"; public String getSingleSelect() { return (this.singleSelect); } public void setSingleSelect(String singleSelect) { this.singleSelect = singleSelect; } <p class="indent"> |
或多選,多選必須是陣列:
private String[] beanCollectionSelect = { "Value 1", "Value 3", "Value 5" }; public String[] getBeanCollectionSelect() { return (this.beanCollectionSelect); } public void setBeanCollectionSelect(String beanCollectionSelect[]) { this.beanCollectionSelect = beanCollectionSelect; } <p class="indent"> |
第二:在Jsp中寫入tang lib語句如下:
<html:select property="beanCollectionSelect" size="10" multiple="true"> <html:optionsCollection name="testbean" property="beanCollection"/> </html:select> <p class="indent"> |
其中testbean是ActionForm的名稱。
以上是html:options的Collection解決方案,如果option值很少,簡單地可以實現為陣列,兩步:
第一:在ActionForm中,
private String values[] = { "Magazine", "Journal", "News Paper","Other" }; private String labels[] = { "L-Magazine", "L-Journal", "L-News Paper","L-Other"}; private String selected = "Magazine"; public String getSelected(){ return selected; } public void setSelected(String selected){ this.selected = selected; } public String[] getValues(){ return values; } public void setValues(String[] values){ this.values = values; } public String[] getLabels(){ return values; } public void setLabels(String[] labels){ this.labels = labels; } <p class="indent"> |
第二步在jsp中:
<html:select property="selected" > <html:options name="testbean" property="values" labelProperty="label"/> </html:select> <p class="indent"> |
相關文章
- struts2的使用
- .Net Core 中的選項Options
- HTML中的標籤的使用HTML
- Html 中如何使用javaScriptHTMLJavaScript
- html中如何使用svg?HTMLSVG
- Struts框架_9 Struts2的驗證框架
- .Net Core之選項模式Options使用模式
- HTML中CSS引用:選擇器的使用HTMLCSS
- 在 IDEA 中配置 Struts2Idea
- 什麼是Apache Access Log中的OPTIONS *的含義Apache
- 79、概述struts,以及struts如何實現MVC架構的?MVC架構
- html中的定位HTML
- Flutter Analysis OptionsFlutter
- 4.5.1.2.1 Syntax and Options
- 4.5.1.4.1 Syntax and Options
- ffmpeg Advanced options
- axios中POST請求變成OPTIONS處理iOS
- Struts1入門級程式hello world(解決struts1中文亂碼)
- html背景圖的使用HTML
- JAVAEE框架學習——Struts2——Action API 使用Java框架API
- html中引入另一個html的方法HTML
- Net6 Configuration & Options 原始碼分析 Part2 Options原始碼
- 【struts2】
- 在Struts2中寫介面,返回json格式JSON
- JavaScript select options 集合JavaScript
- struts2總結(三)--官網下載struts2的jar包JAR
- HTML常用標籤的使用HTML
- STRUTS2的getClassLoader漏洞利用
- HTML5中Audio使用踩坑彙總HTML
- WordPress 和 Apache Struts 佔所有被武器化和利用的漏洞中的 55%Apache
- HTML 中的特殊符號HTML符號
- HTML中<a>標籤無法使用垂直邊距的解決方法HTML
- [轉]Golang Functional Options PatternGolangFunction
- Performance and High-Availability OptionsORMAI
- 刪除_JAVA_OPTIONSJava
- idea VM Options 設定Idea
- ASP.NET MVC下使用AngularJs語言(三):ng-optionsASP.NETMVCAngularJS
- 談談axios中Post請求變成OPTIONS的幾種解決方案iOS
- Struts2所有依賴關係(struts-2.5.14.1)