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中,最后在页面上用标签遍历数据,但写多了,是不是很烦,我想做一个通用的下拉框标签,只要指明了业务接口,并且该接口实现了特定方法,就可以了。
首先定义一个接口,用来取下拉框的数据。
如果哪个业务层service需要增加下拉框的功能,就需要实现它。
例如:
注意数据必须放在ValueLabelBean中,label表示下拉框显示的数据,value表示下拉框的value值,下面是ValueLabelBean
这个bean:
下面就是写tag了,暂时设置了三个属性 tagId,serviceBean和title,
tagId:select 的 id 属性值。
serviceBean:对应于spring容器中service的id。
title:select的默认选中项。
在标签中可以用WebApplicationContextUtils来得到context,曾一度起了弯路,想到用一个工具类加载容器,倒也能实现,也想到用反射,但是行不通的。 看来变通一下,可能会少走很多弯路。
下面是tld文件:
最后就剩页面了:
好了,尽管这个tag很简陋,但为以后省了不少工,只要在业务层实现一个SelectorInterface接口,在页面上摆个标签就可以了。我刚学标签的编写,有什么不足请指正,如果有更好的设计一定告诉我额。
首先定义一个接口,用来取下拉框的数据。
- 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<list.size();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("<select id='"+this.tagId);
- sBuffer.append("'>");
- if(!StringUtil.isBlank(title)){
- sBuffer.append("<option value='-1' selected>"+title+"</option>");
- }
- for(int i=0;i<list1.size();i++){
- ValueLabelBean vlb = (ValueLabelBean)list1.get(i);
- sBuffer.append("<option value='"+vlb.getValue()+"'>"+vlb.getLabel()+"</option>");
- }
- sBuffer.append("</select>");
- 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" ?>
- <!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>