自定义下拉框初版

href="/Controls/PlainComboEditor/StyleLibary/DesignBoxStyle.css" type="text/css" rel="stylesheet"> href="/Controls/PlainComboEditor/StyleLibary/Highlighter.css" type="text/css" rel="stylesheet"> DesignBox href="Controls/PlainComboEditor/StyleLibary/DesignBoxStyle.css" type="text/css" rel="stylesheet"> href="Controls/PlainComboEditor/StyleLibary/Highlighter.css" type="text/css" rel="stylesheet">    下拉框是网页的重要元素,动态取数据并不难,通常的思路是在action中取数据,然后把数据放到request中,最后在页面上用标签遍历数据,但写多了,是不是很烦,我想做一个通用的下拉框标签,只要指明了业务接口,并且该接口实现了特定方法,就可以了。
    首先定义一个接口,用来取下拉框的数据。
  1. package com.ssh.tag;
  2. import java.util.List;
  3. /** 
  4.  * @author 孙程亮 E-mail:sclsch@188.com 
  5.  * @version 创建时间:Oct 27, 2008 6:59:05 PM
  6.  * 取得下拉框数据接口 
  7.  */
  8. public interface SelectorInterface {
  9.   public List getVableValueList();
  10. }
    如果哪个业务层service需要增加下拉框的功能,就需要实现它。
例如:
  1. package com.ssh.entity.board.service;
  2. import java.util.ArrayList;
  3. import java.util.List;
  4. import com.ssh.common.vo.ValueLabelBean;
  5. import com.ssh.entity.board.dao.IBoardDao;
  6. import com.ssh.entity.board.model.Board;
  7. import com.ssh.tag.SelectorInterface;
  8. import com.sun.java_cup.internal.internal_error;
  9. /**
  10.  * @author 孙程亮 E-mail:sclsch@188.com
  11.  * @version 创建时间:Sep 4, 2008 6:36:22 PM
  12.  */
  13. public class BoardServiceImpl implements IBoardService,SelectorInterface{
  14.     private IBoardDao boardDao;
  15.     public void addBoard(Board b) {
  16.        boardDao.addBorad(b);
  17.     }
  18.     public IBoardDao getBoardDao() {
  19.         return boardDao;
  20.     }
  21.     public void setBoardDao(IBoardDao boardDao) {
  22.         this.boardDao = boardDao;
  23.     }
  24.     public List getAllBoards() {
  25.         return this.boardDao.getAllBoards();
  26.     }
  27.     /**
  28.      * 用来实现下拉框的方法,
  29.      * 把下拉数据存放在ValuLabelBean中,再存放在list中返回
  30.      * 给自定义标签。
  31.      * @return 下拉数据集合
  32.      */
  33.     public List getVableValueList() {
  34.         List list = this.boardDao.getAllBoards();
  35.         List valueLableList = new ArrayList();
  36.         for(int i=0;i<list.size();i++){
  37.           Board board = (Board)list.get(i);
  38.           ValueLabelBean vlb = new ValueLabelBean();
  39.           vlb.setValue(board.getId().toString());
  40.           vlb.setLabel(board.getName());
  41.           valueLableList.add(vlb);
  42.         }
  43.         return valueLableList;
  44.     }
  45. }
    注意数据必须放在ValueLabelBean中,label表示下拉框显示的数据,value表示下拉框的value值,下面是ValueLabelBean
这个bean:
   
  1. package com.ssh.common.vo;
  2. import java.io.Serializable;
  3. /**
  4.  * @author 孙程亮 E-mail:sclsch@188.com
  5.  * @version 创建时间:Oct 27, 2008 7:00:36 PM
  6.  */
  7. public class ValueLabelBean implements Serializable {
  8.     private String value;
  9.     private String label;
  10.     public String getValue() {
  11.         return value;
  12.     }
  13.     public void setValue(String value) {
  14.         this.value = value;
  15.     }
  16.     public String getLabel() {
  17.         return label;
  18.     }
  19.     public void setLabel(String label) {
  20.         this.label = label;
  21.     }
  22. }
   下面就是写tag了,暂时设置了三个属性 tagId,serviceBean和title,
tagId:select 的 id 属性值。
serviceBean:对应于spring容器中service的id。
title:select的默认选中项。
  1. package com.ssh.tag;
  2. import java.io.IOException;
  3. import java.lang.reflect.Method;
  4. import java.util.List;
  5. import javax.servlet.jsp.JspException;
  6. import javax.servlet.jsp.tagext.TagSupport;
  7. import org.springframework.context.support.AbstractApplicationContext;
  8. import org.springframework.util.StringUtils;
  9. import org.springframework.web.context.WebApplicationContext;
  10. import org.springframework.web.context.support.WebApplicationContextUtils;
  11. import org.springframework.web.util.JavaScriptUtils;
  12. import com.ssh.common.util.*;
  13. import com.ssh.entity.board.service.IBoardService;
  14. import com.sun.org.apache.xml.internal.utils.ObjectPool;
  15. import com.ssh.common.vo.*;
  16. import com.ssh.tag.*;
  17. /**
  18.  * 
  19.  * @author 孙程亮 E-mail:sclsch@188.com
  20.  * @version 创建时间:Oct 25, 2008 10:22:18 AM
  21.  */
  22. public class SelectorTag extends TagSupport {
  23.     
  24.     private String tagId;      //select's id
  25.     private String serviceBean;//service
  26.     private String title;      //select's title
  27.     
  28.     public int doEndTag() throws JspException {
  29.       WebApplicationContext applicationContext =  WebApplicationContextUtils.getWebApplicationContext(pageContext.getServletContext());
  30.       SelectorInterface selectorInterface = (SelectorInterface)applicationContext.getBean(serviceBean);
  31.       List list1 = selectorInterface.getVableValueList();
  32.       //List list = ServiceLocator.getSelectorService(serviceBean).getVableValueList();
  33.       StringBuffer sBuffer = new StringBuffer();
  34.       sBuffer.append("<select id='"+this.tagId);
  35.       sBuffer.append("'>");
  36.       if(!StringUtil.isBlank(title)){
  37.           sBuffer.append("<option value='-1' selected>"+title+"</option>");
  38.       }
  39.       for(int i=0;i<list1.size();i++){
  40.         ValueLabelBean vlb =  (ValueLabelBean)list1.get(i);
  41.         sBuffer.append("<option value='"+vlb.getValue()+"'>"+vlb.getLabel()+"</option>");
  42.       }
  43.       sBuffer.append("</select>");
  44.       try {
  45.         pageContext.getOut().println(sBuffer.toString());
  46.     } catch (IOException e) {
  47.         // TODO Auto-generated catch block
  48.         e.printStackTrace();
  49.     }
  50.       return EVAL_PAGE;
  51.     }
  52.     public void setTagId(String tagId) {
  53.         this.tagId = tagId;
  54.     }
  55.     public void setServiceBean(String serviceBean) {
  56.         this.serviceBean = serviceBean;
  57.     }
  58.     public void setTitle(String title) {
  59.         this.title = title;
  60.     }
  61. }
   在标签中可以用WebApplicationContextUtils来得到context,曾一度起了弯路,想到用一个工具类加载容器,倒也能实现,也想到用反射,但是行不通的。 看来变通一下,可能会少走很多弯路。
   下面是tld文件:
   
  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <!DOCTYPE taglib PUBLIC
  3.     "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN"
  4.     "http://java.sun.com/dtd/web-jsptaglibrary_1_2.dtd">
  5. <taglib>
  6.     <tlib-version>1.0</tlib-version>
  7.     <jsp-version>1.0</jsp-version>
  8.     <short-name>sclschTag</short-name>
  9.     <description>sclschTag</description>
  10.     <tag>
  11.         <name>selectorTag</name>
  12.         <tag-class>com.ssh.tag.SelectorTag</tag-class>
  13.         <body-content>JSP</body-content>
  14.         <description>
  15.         </description>
  16.         <attribute>
  17.             <name>tagId</name>
  18.             <required>true</required>
  19.             <rtexprvalue>true</rtexprvalue>
  20.         </attribute>
  21.         <attribute>
  22.             <name>serviceBean</name>
  23.             <required>true</required>
  24.             <rtexprvalue>true</rtexprvalue>
  25.         </attribute>
  26.         <attribute>
  27.             <name>title</name>
  28.             <required>false</required>
  29.             <rtexprvalue>true</rtexprvalue>
  30.         </attribute>
  31.     </tag>
  32. </taglib>
   最后就剩页面了:
  1. <%@ page language="java" contentType="text/html; charset=UTF-8"
  2.     pageEncoding="UTF-8"%>
  3. <%@ taglib uri="/WEB-INF/tld/selectorTag.tld" prefix="sclsch"%>
  4. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  5. <html xmlns="http://www.w3.org/1999/xhtml" lang="zh-CN">
  6. <head>
  7. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  8. <title>mytag(sclsch@188.com)</title>
  9. </head>
  10. <body>
  11.  <sclsch:selectorTag tagId='myid' title="--请选择--" serviceBean="boardService" />
  12. </body>
  13. </html>
   好了,尽管这个tag很简陋,但为以后省了不少工,只要在业务层实现一个SelectorInterface接口,在页面上摆个标签就可以了。我刚学标签的编写,有什么不足请指正,如果有更好的设计一定告诉我额。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值