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

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

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

    隨筆-12  評論-6  文章-0  trackbacks-0
    jsp頁面引入jstl標簽:

    1.網上下載jstl.jar包和standard.jar包放到項目的lib文件夾下,jar包下載:jar包下載;

    2.然后在你的jsp頁面里引入如下代碼:

    1 <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>

    ok了,在你的頁面使用c標簽吧~



    下面來說說自定義標簽的開發:
    jsp自定義標簽,用于項目便捷開發。在實際項目開發中,我們大多數時候會采用數據字典來儲存項目中一些數據,比如性別、國際、類型等,用數據字典存儲很 方便,因此在數據庫中添加一張數據字典表t_dict_value,有做過的項目中采用兩張表進行數據字典的管理,個人在設計數據字典的時候感覺一張表也 夠用了,不多說看建表語句:
                    
    我的自定義標簽是基于數據字典表使用的,當然后續業務中有需要也可以制作特定的自定義標簽,接下來開始自定義標簽,首先寫一個DictSelectTag類,代碼如下:
    package com.infopatent.juangetljc.web.controller.core;
    import java.io.IOException;
    import java.util.List;

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

    import org.apache.commons.lang3.StringUtils;

    import com.infopatent.juangetljc.core.DictValue;

    public class DictSelectTag extends TagSupport  {

        private String dictName = "";
        private boolean defaultValue;
        private String value;
        private String name;
        private String id;
        private String cssClass;
        private String styleClass;
        private String multiple;
        private String onChange;
        private String dataType;

        public String getDataType() {
            return dataType;
        }

        public void setDataType(String dataType) {
            this.dataType = dataType;
        }

        public String getCssClass() {
            return cssClass;
        }

        public void setCssClass(String cssClass) {
            this.cssClass = cssClass;
        }

        public String getStyleClass() {
            return styleClass;
        }

        public void setStyleClass(String styleClass) {
            this.styleClass = styleClass;
        }

        public String getMultiple() {
            return multiple;
        }

        public void setMultiple(String multiple) {
            this.multiple = multiple;
        }

        public String getOnChange() {
            return onChange;
        }

        public void setOnChange(String onChange) {
            this.onChange = onChange;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public String getId() {
            return id;
        }

        public void setId(String id) {
            this.id = id;
        }

        public String getValue() {
            return value;
        }

        public void setValue(String value) {
            this.value = value;
        }

        public String getDictName() {
            return dictName;
        }

        public void setDictName(String dictName) {
            this.dictName = dictName;
        }
        
        public boolean isDefaultValue() {
            return defaultValue;
        }

        public void setDefaultValue(boolean defaultValue) {
            this.defaultValue = defaultValue;
        }
        
        
        @Override
        public int doEndTag() throws JspException{
            DictValue dict = new DictValue();
            List<DictValue> dict_list = dict.getRepository().findByProperty(DictValue.class,"dictName",dictName);
            JspWriter out = pageContext.getOut();
            StringBuffer sb = new StringBuffer();
            sb.append("<select name='"+this.getName()+"' id='"+this.getId()+"' dataType='"+this.getDataType()+"'");
            if(!StringUtils.isEmpty(this.getCssClass())){
                sb.append("class=\"" + this.getCssClass() + "\" ");
            }
            if(!StringUtils.isEmpty(this.getStyleClass())){
                sb.append("style=\"" + this.getStyleClass() + "\" ");
            }
            if(!StringUtils.isEmpty(this.getMultiple())){
                sb.append("multiple=\"" + this.getMultiple() + "\" ");
            }
            if(!StringUtils.isEmpty(this.getOnChange())){
                sb.append("onchange=\"" + this.getOnChange() + "\" ");
            }
            sb.append(">");
            if(this.isDefaultValue()){  
                sb.append("<option value=''>--請選擇--</option>");  
            }
            for(DictValue dc:dict_list){
                if(dc.getItemDesc().equals(this.getValue())){
                    sb.append("<option value='"+dc.getItemCode()+"' selected='selected'>");
                }else{
                    sb.append("<option value='"+dc.getItemCode()+"'>");
                }
                sb.append(dc.getItemDesc()+"</option>");
            }
            sb.append("</select>");
            try {
                out.write(sb.toString());
            } catch (IOException e) {
                // TODO Auto-generated catch block
                throw new JspException(e);
            }
            return TagSupport.EVAL_PAGE;
        }
        
    }
     再寫一個DictLabelTag類用于顯示字典中的值,代碼如下:
      package com.infopatent.juangetljc.web.controller.core;

    import java.io.IOException;

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

    import org.springframework.web.servlet.tags.form.OptionsTag;

    import com.infopatent.juangetljc.core.DictValue;

    public class DictLabelTag extends TagSupport {

        private String dictName = "";
        private String itemCode;

        public String getDictName() {
            return dictName;
        }

        public void setDictName(String dictName) {
            this.dictName = dictName;
        }

        public String getItemCode() {
            return itemCode;
        }

        public void setItemCode(String itemCode) {
            this.itemCode = itemCode;
        }

        @Override
        public int doEndTag() throws JspException {
            DictValue dict = new DictValue();
            JspWriter out = pageContext.getOut();
            try {
                out.write(dict.codeToName(getDictName(),getItemCode()));
            } catch (IOException e) {
                throw new JspException(e);
            }
            return TagSupport.EVAL_PAGE;
        }
    }

    接下來配置tld文件,代碼如下:

    <?xml version="1.0" encoding="UTF-8"?>
    <taglib xmlns="http://java.sun.com/xml/ns/j2ee"
            xmlns:xsi
    ="http://www.w3.org/2001/XMLSchema-instance"
            xsi:schemaLocation
    ="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd"
            version
    ="2.0">

        <description>SPay JSP Form Tag Library</description>
        <tlib-version>1.0</tlib-version>
        <short-name>dict</short-name>
        <uri>http://www.tljc.com/dict_tag</uri>

        <tag>
            <description>Renders an HTML 'select' element. Supports databinding to the selected option.</description>
            <name>select</name>
            <tag-class>com.infopatent.juangetljc.web.controller.core.DictSelectTag</tag-class>
            <body-content>JSP</body-content>
            <attribute>  
                   <name>defaultValue</name>  
                <required>true</required>  
                <rtexprvalue>true</rtexprvalue>  
            </attribute>  
            <attribute>  
                   <name>dataType</name>  
                <required>true</required>  
                <rtexprvalue>true</rtexprvalue>  
            </attribute> 
        <attribute>  
                <name>value</name>  
                <required>false</required>  
                <rtexprvalue>true</rtexprvalue>  
        </attribute>
        <attribute>
            <name>dictName</name>
            <required>true</required>  
            <rtexprvalue>true</rtexprvalue>
        </attribute>
        <attribute>
            <name>name</name>
            <required>true</required>  
            <rtexprvalue>true</rtexprvalue>
        </attribute>
        <attribute>
            <name>id</name>
            <required>true</required>  
            <rtexprvalue>true</rtexprvalue>
        </attribute>
        <attribute>
            <name>cssClass</name>
            <required>false</required>  
            <rtexprvalue>true</rtexprvalue>
        </attribute>
        <attribute>
            <name>styleClass</name>
            <required>false</required>  
            <rtexprvalue>true</rtexprvalue>
        </attribute>
        <attribute>
            <name>multiple</name>
            <required>false</required>  
            <rtexprvalue>true</rtexprvalue>
        </attribute>
        <attribute>
            <name>onChange</name>
            <required>false</required>  
            <rtexprvalue>true</rtexprvalue>
        </attribute>
        </tag>
        
        

        <tag>
            <name>itemdesc</name>
            <tag-class>com.infopatent.juangetljc.web.controller.core.DictLabelTag</tag-class>
            <body-content>JSP</body-content>
            <attribute>
                <description>The Dict Name config in dict_value</description>
                <name>dictName</name>
                <required>true</required>
                <rtexprvalue>true</rtexprvalue>
            </attribute>
            <attribute>
                <description>The Dict Code config in dict_value</description>
                <name>itemCode</name>
                <required>true</required>
                <rtexprvalue>true</rtexprvalue>
            </attribute>
        </tag>
    </taglib>
    接下來在jsp頁面里引入標簽:

    <%@taglib prefix="dict" uri="http://www.tljc.com/dict_tag" %>

    這樣便可以在jsp頁面里使用定義的標簽了:

    <dict:select defaultValue="true" name="GJ" id="GJ" dictName="GJ" cssClass="form-control"/>

    前提是要在字典表里加上“GJ”這條數據。

    OK了!

    posted on 2014-05-21 17:17 小人物_Amor 閱讀(11602) 評論(6)  編輯  收藏 所屬分類: web

    評論:
    # re: jsp頁面引入jstl標簽以及如何開發自定義標簽[未登錄] 2014-05-21 17:36 | xx
    Good!贊一個!  回復  更多評論
      
    # re: jsp頁面引入jstl標簽以及如何開發自定義標簽 2014-05-22 09:35 | 小人物_Amor
    @xx
    謝謝!  回復  更多評論
      
    # re: jsp頁面引入jstl標簽以及如何開發自定義標簽 2014-05-22 23:13 | houyong
    上學的時候用過jstl
    上班了沒見人用過
    我以為沒人用了,java是不會被遺忘的




    -------------------------------------------
    我的個人空間www.itqx.net  回復  更多評論
      
    # re: jsp頁面引入jstl標簽以及如何開發自定義標簽 2014-05-23 09:56 | 小人物_Amor
    @houyong
    java依舊很流行~  回復  更多評論
      
    # re: jsp頁面引入jstl標簽以及如何開發自定義標簽 2014-12-25 10:01 | wedtlcy
    你好!能發一下源碼或者dict.codeToName(getDictName(),getItemCode()), dict.getRepository().findByProperty()這2個方法嗎?郵箱:873418765@qq.com謝謝!  回復  更多評論
      
    # re: jsp頁面引入jstl標簽以及如何開發自定義標簽 2014-12-25 14:02 | wedtlcy
    急  回復  更多評論
      
    主站蜘蛛池模板: 亚洲AV无码乱码在线观看性色扶| 亚洲AV人人澡人人爽人人夜夜| 一区二区三区在线免费观看视频 | 成年女人色毛片免费看| 色噜噜的亚洲男人的天堂| 亚洲一区二区三区在线观看精品中文| 4444www免费看| 日韩在线观看视频免费| 4480yy私人影院亚洲| 国产免费午夜a无码v视频| 免费污视频在线观看| 亚洲av永久无码精品秋霞电影秋| 国产亚洲A∨片在线观看| 24小时免费直播在线观看| 久久久久久久国产免费看| 亚洲伊人久久大香线蕉AV| 国产AV无码专区亚洲AV毛网站| 日韩免费高清一级毛片在线| 免费在线中文日本| 鲁啊鲁在线视频免费播放| 91亚洲国产成人久久精品| 久久久亚洲精品蜜桃臀| 在线观看人成网站深夜免费| 午夜免费啪视频在线观看| 大片免费观看92在线视频线视频| 2020年亚洲天天爽天天噜| 亚洲AV综合色区无码一区爱AV| 波多野结衣中文一区二区免费| 在线观看H网址免费入口| 9久久免费国产精品特黄| 国产精品亚洲专区无码WEB| 亚洲欧洲日产国码二区首页| 亚洲愉拍99热成人精品热久久| 国产成人免费a在线资源| 国产精品色拉拉免费看| 国产精品免费高清在线观看| 国产精品免费久久久久久久久| 国产亚洲精品2021自在线| 一本色道久久综合亚洲精品蜜桃冫 | 免费黄色大片网站| 动漫黄网站免费永久在线观看|