首先定义一个接口,用来取下拉框的数据。
- package com.ssh.tag;
- import java.util.List;
- /**
- * @author 孙程亮 E-mail:sclsch@188.com
- * @version 创建时间:Oct 27, 2008 6:59:05 PM
- * 取得下拉框数据接口
- */
- public interface SelectorInterface {
- public List getVableValueList();
- }
例如:
- package com.ssh.entity.board.service;
- import java.util.ArrayList;
- import java.util.List;
- import com.ssh.common.vo.ValueLabelBean;
- import com.ssh.entity.board.dao.IBoardDao;
- import com.ssh.entity.board.model.Board;
- import com.ssh.tag.SelectorInterface;
- import com.sun.java_cup.internal.internal_error;
- /**
- * @author 孙程亮 E-mail:sclsch@188.com
- * @version 创建时间:Sep 4, 2008 6:36:22 PM
- */
- public class BoardServiceImpl implements IBoardService,SelectorInterface{
- private IBoardDao boardDao;
- public void addBoard(Board b) {
- boardDao.addBorad(b);
- }
- public IBoardDao getBoardDao() {
- return boardDao;
- }
- public void setBoardDao(IBoardDao boardDao) {
- this.boardDao = boardDao;
- }
- public List getAllBoards() {
- return this.boardDao.getAllBoards();
- }
- /**
- * 用来实现下拉框的方法,
- * 把下拉数据存放在ValuLabelBean中,再存放在list中返回
- * 给自定义标签。
- * @return 下拉数据集合
- */
- public List getVableValueList() {
- List list = this.boardDao.getAllBoards();
- List valueLableList = new ArrayList();
- for(int i=0;i
- Board board = (Board)list.get(i);
- ValueLabelBean vlb = new ValueLabelBean();
- vlb.setValue(board.getId().toString());
- vlb.setLabel(board.getName());
- valueLableList.add(vlb);
- }
- return valueLableList;
- }
- }
这个bean:
- package com.ssh.common.vo;
- import java.io.Serializable;
- /**
- * @author 孙程亮 E-mail:sclsch@188.com
- * @version 创建时间:Oct 27, 2008 7:00:36 PM
- */
- public class ValueLabelBean implements Serializable {
- private String value;
- private String label;
- public String getValue() {
- return value;
- }
- public void setValue(String value) {
- this.value = value;
- }
- public String getLabel() {
- return label;
- }
- public void setLabel(String label) {
- this.label = label;
- }
- }
tagId:select 的 id 属性值。
serviceBean:对应于spring容器中service的id。
title:select的默认选中项。
- package com.ssh.tag;
- import java.io.IOException;
- import java.lang.reflect.Method;
- import java.util.List;
- import javax.servlet.jsp.JspException;
- import javax.servlet.jsp.tagext.TagSupport;
- import org.springframework.context.support.AbstractApplicationContext;
- import org.springframework.util.StringUtils;
- import org.springframework.web.context.WebApplicationContext;
- import org.springframework.web.context.support.WebApplicationContextUtils;
- import org.springframework.web.util.JavaScriptUtils;
- import com.ssh.common.util.*;
- import com.ssh.entity.board.service.IBoardService;
- import com.sun.org.apache.xml.internal.utils.ObjectPool;
- import com.ssh.common.vo.*;
- import com.ssh.tag.*;
- /**
- *
- * @author 孙程亮 E-mail:sclsch@188.com
- * @version 创建时间:Oct 25, 2008 10:22:18 AM
- */
- public class SelectorTag extends TagSupport {
-
- private String tagId; //select's id
- private String serviceBean;//service
- private String title; //select's title
-
- public int doEndTag() throws JspException {
- WebApplicationContext applicationContext = WebApplicationContextUtils.getWebApplicationContext(pageContext.getServletContext());
- SelectorInterface selectorInterface = (SelectorInterface)applicationContext.getBean(serviceBean);
- List list1 = selectorInterface.getVableValueList();
- //List list = ServiceLocator.getSelectorService(serviceBean).getVableValueList();
- StringBuffer sBuffer = new StringBuffer();
- sBuffer.append("
- sBuffer.append("">
- if(!StringUtil.isBlank(title)){
- sBuffer.append(""+title+"");
- }
- for(int i=0;i
- ValueLabelBean vlb = (ValueLabelBean)list1.get(i);
- sBuffer.append(""+vlb.getLabel()+"");
- }
- sBuffer.append("");
- try {
- pageContext.getOut().println(sBuffer.toString());
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- return EVAL_PAGE;
- }
- public void setTagId(String tagId) {
- this.tagId = tagId;
- }
- public void setServiceBean(String serviceBean) {
- this.serviceBean = serviceBean;
- }
- public void setTitle(String title) {
- this.title = title;
- }
- }
下面是tld文件:
- xml version="1.0" encoding="UTF-8" ?>
- "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN"
- "http://java.sun.com/dtd/web-jsptaglibrary_1_2.dtd">
- <taglib>
- <tlib-version>1.0tlib-version>
- <jsp-version>1.0jsp-version>
- <short-name>sclschTagshort-name>
- <description>sclschTagdescription>
- <tag>
- <name>selectorTagname>
- <tag-class>com.ssh.tag.SelectorTagtag-class>
- <body-content>JSPbody-content>
- <description>
- description>
- <attribute>
- <name>tagIdname>
- <required>truerequired>
- <rtexprvalue>truertexprvalue>
- attribute>
- <attribute>
- <name>serviceBeanname>
- <required>truerequired>
- <rtexprvalue>truertexprvalue>
- attribute>
- <attribute>
- <name>titlename>
- <required>falserequired>
- <rtexprvalue>truertexprvalue>
- attribute>
- tag>
- taglib>
- <%@ page language="java" contentType="text/html; charset=UTF-8"
- pageEncoding="UTF-8"%>
- <%@ taglib uri="/WEB-INF/tld/selectorTag.tld" prefix="sclsch"%>
- <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>
没有评论:
发表评论