您的位置:首页 > Web前端 > JavaScript

JSP自定义标签(三) 多选控件

2012-05-09 16:21 507 查看
一、效果图



二、标签定义代码

package com.swcares.util.tags;
import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

/**
 *
 * @author HHB
 *
 *  多选自定义标签标签处理类
 *
 */
public class MultiSelectorTag extends TagSupport {

    private static final long serialVersionUID = 1164199147616542853L;
    //标签name属性
    private String name;
    
    //所需图片的路径
    private String imgPath;
    
    //所需javascript文件的路径
    private String scriptPaht;
    
    //所需css文件的路径
    private String cssPath;
    
    //项目的根路径
    private String rootPath;
    
    //标签的value属性
    private String value;
    
    private HttpServletRequest request=null;
    
    public String getValue() {
        return value;
    }
    public void setValue(String value) {
        this.value = value;
    }
    public String getImgPath() {
        return imgPath;
    }
    public void setImgPath(String imgPath) {
        this.imgPath = imgPath;
    }
    public String getScriptPaht() {
        return scriptPaht;
    }
    
    public void setScriptPaht(String scriptPaht) {
        this.scriptPaht = scriptPaht;
    }
    public String getCssPath() {
        return cssPath;
    }
    
    public void setCssPath(String cssPath) {
        this.cssPath = cssPath;
    }
    
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    /**
     * 初始化变量
     */
    private void initAbttributes()
    {
        request=(HttpServletRequest)this.pageContext.getRequest();
        rootPath=request.getContextPath();
        this.imgPath="/images/";
        this.scriptPaht="/js/tags/";
        this.cssPath="/css/";
        
    }
    @Override
    public int doStartTag() throws JspException {
        initAbttributes();
        JspWriter out=pageContext.getOut();
        try {
            String tName=name;
            //引入javascript文件
            out.println("<script type='text/javascript' charset='UTF-8' src='"+rootPath+scriptPaht+"multiSelector.js'></script>");
            
            StringBuilder tag=new StringBuilder("<div style='display:inline;float:left;' ");
            tag.append("id='multiParent_").append(id).append("'>");
            tag.append("</div>");
            tag.append("<script type='text/javascript'>")
                .append("showMulti('multiParent_").append(id)
                .append("','").append(name)
                .append("','").append(id)
                .append("',\"").append(value)
                .append("\")")
                .append("</script>");
            out.println(tag.toString());
        } catch (IOException e) {
            e.printStackTrace();
        }
        return SKIP_BODY;
    }
    
    
}

待续。。。。。。。。。。。。。。。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息