2006年7月8日

          Struts 中的 html:options(摘抄)

          html:options是Struts中比較復雜的一個tage lib,用法靈活,但是Sturts提供的源碼exercise taglib中沒有提出常用jsp+ActionForm這樣形式的最直接的總結(jié),現(xiàn)從中總結(jié)如下,分兩種情況:數(shù)組和Collection。
            
            需求,要達到:
            <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>
            
            要實現(xiàn)上述效果,需要兩步:
            第一:設(shè)置ActionForm,
            也分兩小步:第一小步必須在ActionForm中,有一句
            private Collection beanCollection;
            public Collection getBeanCollection();
            
            Collection beanCollection要確保是一個實現(xiàn),如ArrayList,如果不是則會報No collection found的錯誤,Struts的最大不方便就是一旦出問題,定位很難,不知道什么地方使用錯誤,或忘記設(shè)置什么了。
            
            因為前面需求中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"));
            
            然后執(zhí)行setBeanCollection(entries);
            這樣ActionForm中的beanCollection算有值了。
            第二小步,需要設(shè)置Selected,selected有兩種,單選和多選:
            在ActionForm中必須有:
            
            private String singleSelect = "Single 5"; 
            public String getSingleSelect()
             {
               return (this.singleSelect);
              } 
            public void setSingleSelect(String singleSelect)
             {
               this.singleSelect = singleSelect;
              }
            
            或多選,多選必須是數(shù)組:
            
            private String[] beanCollectionSelect = { "value 1", "value 3",
                                   "value 5" }; 
            public String[] getBeanCollectionSelect() {
              return (this.beanCollectionSelect);  }
              public void setBeanCollectionSelect(String beanCollectionSelect[])
             {
                this.beanCollectionSelect = beanCollectionSelect;
              }
            
            第二:在Jsp中寫入tang lib語句如下:
            
            <html:select property="beanCollectionSelect" size="10" multiple="true">
                <html:optionsCollection name="testbean" property="beanCollection"/>  
             </html:select>
            
            其中testbean是ActionForm的名稱。
            
            以上是html:options的Collection解決方案,如果option值很少,簡單地可以實現(xiàn)為數(shù)組,兩步:
            第一:在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;
              }
            
            第二步在jsp中:
            
            <html:select property="selected" >     
            <html:options name="testbean" property="values" labelProperty="label"/>   </html:select>
            
            Struts標簽庫的使用還是需要小心,不必完全使用Struts的標簽庫,個人感覺Struts這種替代Html語句的標簽庫有一種牽強附會,給使用者掌握帶來難度,使用者除熟悉html外,還必須理解Struts的對應(yīng)標簽庫用法,而且這種調(diào)試出錯,問題也無法準確定位,總是抽象地告訴你,no bean 或no form
          posted @ 2006-07-09 00:58 Redish 閱讀(817) | 評論 (1)編輯 收藏

          中文轉(zhuǎn)碼Filter

          在Struts中經(jīng)常會碰到中文轉(zhuǎn)碼的問題,最簡單的辦法就是直接寫一個ServletFilter
          代碼如下:
          ChineseFilter.class


          import java.io.IOException;
          import javax.servlet.Filter;
          import javax.servlet.FilterChain;
          import javax.servlet.FilterConfig;
          import javax.servlet.ServletException;
          import javax.servlet.ServletRequest;
          import javax.servlet.ServletResponse;
          import javax.servlet.http.HttpServlet;
          import javax.servlet.http.HttpServletRequest;
          import javax.servlet.http.HttpServletResponse;

          public class ChineseFilter extends HttpServlet implements Filter {

          ?private FilterConfig filterConfig;
          ???? //Handle the passed-in FilterConfig
          ???? public void init(FilterConfig filterConfig) throws ServletException {
          ???????? this.filterConfig = filterConfig;
          ???? }
          ???? //Process the request/response pair
          ???? public void doFilter(ServletRequest request, ServletResponse response,
          ????????????????????????? FilterChain filterChain) {
          ???????? try {
          ???????? ?
          ???????? ?String encoding=filterConfig.getInitParameter("encoding");//從WEB.xml配置文件中取出參數(shù),這樣我們可以通過配置修改編碼格式.??????????????????
          ???????????? request.setCharacterEncoding(encoding);//設(shè)置請求的編碼格式
          ???????????? filterChain.doFilter(request, response);
          ???????? } catch (ServletException sx) {
          ???????????? filterConfig.getServletContext().log(sx.getMessage());
          ???????? } catch (IOException iox) {
          ???????????? filterConfig.getServletContext().log(iox.getMessage());
          ???????? }
          ???? }

          ???? //Clean up resources
          ???? public void destroy() {
          ???? }

          ??protected void doGet(HttpServletRequest arg0, HttpServletResponse arg1) throws ServletException, IOException {
          ???// TODO Auto-generated method stub
          ???super.doGet(arg0, arg1);??
          ??}

          ??protected void doPost(HttpServletRequest arg0, HttpServletResponse arg1) throws ServletException, IOException {
          ???// TODO Auto-generated method stub
          ???super.doPost(arg0, arg1);???
          ??}?
          }

          寫完過濾器剩下的就是配置Web.xml了

          ?<filter>
          ? <filter-name>ChineseFilter</filter-name>
          ? <filter-class>com.blog.filter.ChineseFilter</filter-class>
          <init-param>
          ??? <param-name>encoding</param-name>
          ??? <param-value>GBK</param-value> 配置編碼格式,可以配置成你想要的編碼(GBK,bg-2312)

          </init-param>
          ? </filter>
          ?? <filter-mapping>
          ??? <filter-name>ChineseFilter</filter-name>
          ??? <url-pattern>/*</url-pattern>
          ? </filter-mapping>

          好了,簡單的Filter就完成了.當然也有現(xiàn)成的包,比如你用Struts+Spring的話,那Spring中就有現(xiàn)成的轉(zhuǎn)碼過濾器,我們只要在WEB.xml中配置一下就OK了.

          <filter>
          <filter-name>encodingFilter</filter-name>
          ?<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
          ?<init-param>
          ?<param-name>encoding</param-name>
          . <param-value>GBK</param-value>
          ?</init-param>
          ?</filter>
          <filter-mapping>
          ?<filter-name>encodingFilter</filter-name>
          ?<url-pattern>/*</url-pattern>
          ?</filter-mapping>

          posted @ 2006-07-08 15:21 Redish 閱讀(829) | 評論 (0)編輯 收藏
          主站蜘蛛池模板: 临清市| 松滋市| 海安县| 富平县| 利川市| 湟中县| 长海县| 日喀则市| 康保县| 明溪县| 黔江区| 兴海县| 嘉黎县| 区。| 久治县| 墨竹工卡县| 视频| 江源县| 怀柔区| 高碑店市| 军事| 徐闻县| 隆林| 松潘县| 上虞市| 临江市| 太白县| 惠安县| 班玛县| 巴林右旗| 友谊县| 江津市| 青海省| 深圳市| 昔阳县| 睢宁县| 普陀区| 恩平市| 平原县| 如东县| 西青区|