<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    sclsch

    java備忘

    BlogJava 首頁 新隨筆 聯系 聚合 管理
      10 Posts :: 0 Stories :: 6 Comments :: 0 Trackbacks
        下拉框是網頁的重要元素,動態取數據并不難,通常的思路是在action中取數據,然后把數據放到request中,最后在頁面上用標簽遍歷數據,但寫多了,是不是很煩,我想做一個通用的下拉框標簽,只要指明了業務接口,并且該接口實現了特定方法,就可以了。
        首先定義一個接口,用來取下拉框的數據。
       
       1package com.ssh.tag;
       
    2.
       
    3import java.util.List;
       
    4.
       
    5/** 
       6.  * 
    @author 孫程亮 E-mail:sclsch@188.com 
       7.  * 
    @version 創建時間:Oct 27, 2008 6:59:05 PM
       8.  * 取得下拉框數據接口 
       9.  
    */
      
    10public interface SelectorInterface {
      
    11.   public List getVableValueList();
      
    12. }
       
        如果哪個業務層service需要增加下拉框的功能,就需要實現它。
    例如:
      
       1package com.ssh.entity.board.service;
       
    2.
       
    3import java.util.ArrayList;
       
    4import java.util.List;
       
    5.
       
    6import com.ssh.common.vo.ValueLabelBean;
       
    7import com.ssh.entity.board.dao.IBoardDao;
       
    8import com.ssh.entity.board.model.Board;
       
    9import com.ssh.tag.SelectorInterface;
      
    10import com.sun.java_cup.internal.internal_error;
      
    11.
      
    12/**
      13.  * 
    @author 孫程亮 E-mail:sclsch@188.com
      14.  * 
    @version 創建時間:Sep 4, 2008 6:36:22 PM
      15.  
    */
      
    16public class BoardServiceImpl implements IBoardService,SelectorInterface{
      
    17.     private IBoardDao boardDao;
      
    18.
      
    19.     public void addBoard(Board b) {
      
    20.        boardDao.addBorad(b);
      
    21.     }
      
    22.
      
    23.     public IBoardDao getBoardDao() {
      
    24.         return boardDao;
      
    25.     }
      
    26.
      
    27.     public void setBoardDao(IBoardDao boardDao) {
      
    28.         this.boardDao = boardDao;
      
    29.     }
      
    30.
      
    31.     public List getAllBoards() {
      
    32.         return this.boardDao.getAllBoards();
      
    33.     }
      
    34.     /**
      35.      * 用來實現下拉框的方法,
      36.      * 把下拉數據存放在ValuLabelBean中,再存放在list中返回
      37.      * 給自定義標簽。
      38.      * 
    @return 下拉數據集合
      39.      
    */
      
    40.     public List getVableValueList() {
      
    41.         List list = this.boardDao.getAllBoards();
      
    42.         List valueLableList = new ArrayList();
      
    43.         for(int i=0;i<list.size();i++){
      
    44.           Board board = (Board)list.get(i);
      
    45.           ValueLabelBean vlb = new ValueLabelBean();
      
    46.           vlb.setValue(board.getId().toString());
      
    47.           vlb.setLabel(board.getName());
      
    48.           valueLableList.add(vlb);
      
    49.         }
      
    50.         return valueLableList;
      
    51.     }
      
    52. }
        注意數據必須放在ValueLabelBean中,label表示下拉框顯示的數據,value表示下拉框的value值,下面是ValueLabelBean
    這個bean:
       1package com.ssh.common.vo;
       
    2.
       
    3import java.io.Serializable;
       
    4.
       
    5/**
       6.  * 
    @author 孫程亮 E-mail:sclsch@188.com
       7.  * 
    @version 創建時間:Oct 27, 2008 7:00:36 PM
       8.  
    */
       
    9public class ValueLabelBean implements Serializable {
      
    10.     private String value;
      
    11.     private String label;
      
    12.
      
    13.     public String getValue() {
      
    14.         return value;
      
    15.     }
      
    16.
      
    17.     public void setValue(String value) {
      
    18.         this.value = value;
      
    19.     }
      
    20.
      
    21.     public String getLabel() {
      
    22.         return label;
      
    23.     }
      
    24.
      
    25.     public void setLabel(String label) {
      
    26.         this.label = label;
      
    27.     }
      
    28. }

       下面就是寫tag了,暫時設置了三個屬性 tagId,serviceBean和title,
    tagId:select 的 id 屬性值。
    serviceBean:對應于spring容器中service的id。
    title:select的默認選中項。
       1package com.ssh.tag;
       
    2.
       
    3import java.io.IOException;
       
    4import java.lang.reflect.Method;
       
    5import java.util.List;
       
    6.
       
    7import javax.servlet.jsp.JspException;
       
    8import javax.servlet.jsp.tagext.TagSupport;
       
    9.
      
    10import org.springframework.context.support.AbstractApplicationContext;
      
    11import org.springframework.util.StringUtils;
      
    12import org.springframework.web.context.WebApplicationContext;
      
    13import org.springframework.web.context.support.WebApplicationContextUtils;
      
    14import org.springframework.web.util.JavaScriptUtils;
      
    15import com.ssh.common.util.*;
      
    16import com.ssh.entity.board.service.IBoardService;
      
    17import com.sun.org.apache.xml.internal.utils.ObjectPool;
      
    18import com.ssh.common.vo.*;
      
    19import com.ssh.tag.*;
      
    20/**
      21.  * 
      22.  * 
    @author 孫程亮 E-mail:sclsch@188.com
      23.  * 
    @version 創建時間:Oct 25, 2008 10:22:18 AM
      24.  
    */
      
    25public class SelectorTag extends TagSupport {
      
    26.     
      
    27.     private String tagId;      //select's id
      28.     private String serviceBean;//service
      29.     private String title;      //select's title
      30.     
      
    31.     public int doEndTag() throws JspException {
      
    32.       WebApplicationContext applicationContext =  WebApplicationContextUtils.getWebApplicationContext(pageContext.getServletContext());
      
    33.       SelectorInterface selectorInterface = (SelectorInterface)applicationContext.getBean(serviceBean);
      
    34.       List list1 = selectorInterface.getVableValueList();
      
    35.       //List list = ServiceLocator.getSelectorService(serviceBean).getVableValueList();
      36.       StringBuffer sBuffer = new StringBuffer();
      
    37.       sBuffer.append("<select id='"+this.tagId);
      
    38.
      
    39.       sBuffer.append("'>");
      
    40.       if(!StringUtil.isBlank(title)){
      
    41.           sBuffer.append("<option value='-1' selected>"+title+"</option>");
      
    42.       }
      
    43.       for(int i=0;i<list1.size();i++){
      
    44.         ValueLabelBean vlb =  (ValueLabelBean)list1.get(i);
      
    45.         sBuffer.append("<option value='"+vlb.getValue()+"'>"+vlb.getLabel()+"</option>");
      
    46.       }
      
    47.       sBuffer.append("</select>");
      
    48.       try {
      
    49.         pageContext.getOut().println(sBuffer.toString());
      
    50.     } catch (IOException e) {
      
    51.         // TODO Auto-generated catch block
      52.         e.printStackTrace();
      
    53.     }
      
    54.       return EVAL_PAGE;
      
    55.     }
      
    56.     public void setTagId(String tagId) {
      
    57.         this.tagId = tagId;
      
    58.     }
      
    59.     public void setServiceBean(String serviceBean) {
      
    60.         this.serviceBean = serviceBean;
      
    61.     }
      
    62.     public void setTitle(String title) {
      
    63.         this.title = title;
      
    64.     }
      
    65. }

    在標簽中可以用WebApplicationContextUtils來得到context,曾一度起了彎路,想到用一個工具類加載容器,倒也能實現,也想到用反射,但是行不通的。 看來變通一下,可能會少走很多彎路。
       下面是tld文件:
    <?xml version="1.0" encoding="UTF-8" ?>
     <!DOCTYPE taglib PUBLIC
         "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN"
         "http://java.sun.com/dtd/web-jsptaglibrary_1_2.dtd">

     <taglib>

         <tlib-version>1.0</tlib-version>
         <jsp-version>1.0</jsp-version>
         <short-name>sclschTag</short-name>
         <description>sclschTag</description>

         <tag>
             <name>selectorTag</name>
             <tag-class>com.ssh.tag.SelectorTag</tag-class>
             <body-content>JSP</body-content>
             <description>
             </description>
             <attribute>
                 <name>tagId</name>
                 <required>true</required>
                 <rtexprvalue>true</rtexprvalue>
             </attribute>

             <attribute>
                 <name>serviceBean</name>
                 <required>true</required>
                 <rtexprvalue>true</rtexprvalue>
             </attribute>
             <attribute>
                 <name>title</name>
                 <required>false</required>
                 <rtexprvalue>true</rtexprvalue>
             </attribute>
         </tag>

     </taglib>
    最后就剩頁面了:
      <%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding
    ="UTF-8"%>
     
    <%@ taglib uri="/WEB-INF/tld/selectorTag.tld" prefix="sclsch"%>
     
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
     
    <html xmlns="http://www.w3.org/1999/xhtml" lang="zh-CN">
     
    <head>
     
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
     
    <title>mytag(sclsch@188.com)</title>
     
    </head>
     
    <body>
     
    <sclsch:selectorTag tagId='myid' title="--請選擇--" serviceBean="boardService" />
     
    </body>
     
    </html>

        好了,盡管這個tag很簡陋,但為以后省了不少工,只要在業務層實現一個SelectorInterface接口,在頁面上擺個標簽就可以了。我剛學標簽的編寫,有什么不足請指正,如果有更好的設計一定告訴我額。


    posted on 2008-10-28 20:57 sclsch 閱讀(741) 評論(0)  編輯  收藏

    只有注冊用戶登錄后才能發表評論。


    網站導航:
     
    主站蜘蛛池模板: 亚洲欧洲日产国码二区首页| 精品国产_亚洲人成在线高清| 亚洲美女大bbbbbbbbb| 未满十八18禁止免费无码网站| 亚洲AV无码专区国产乱码电影| 久久大香伊焦在人线免费| 久久99亚洲网美利坚合众国| 91精品免费久久久久久久久| 亚洲成年人免费网站| 久久久高清免费视频| 亚洲欧洲专线一区| 无人在线直播免费观看| 亚洲图片在线观看| 亚洲成av人片不卡无码久久| 亚洲欧洲精品久久| 午夜不卡久久精品无码免费| 亚洲AV人无码综合在线观看| 亚洲成a人片在线网站| 在线观看H网址免费入口| 亚洲丰满熟女一区二区哦| 亚洲成a人一区二区三区| 国产免费人成视频尤勿视频 | 一级毛片免费在线| 18以下岁毛片在免费播放| 亚洲人成小说网站色| 97国产在线公开免费观看| 麻豆狠色伊人亚洲综合网站| 亚洲综合免费视频| 亚洲gay片在线gv网站| JLZZJLZZ亚洲乱熟无码| 无码av免费一区二区三区| 亚洲人成电影网站色www| 亚洲中文字幕无码久久综合网| 免费国产黄网站在线观看动图 | 中国一级特黄高清免费的大片中国一级黄色片| 国产亚洲一区二区三区在线不卡 | 亚洲精品无码久久久久A片苍井空| 免费国产一级特黄久久| 亚洲性色AV日韩在线观看| 亚洲免费在线观看| 97碰公开在线观看免费视频|