隨筆-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 閱讀(11608) 評論(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
          急  回復  更多評論
            
          主站蜘蛛池模板: 河津市| 合川市| 历史| 西丰县| 于田县| 屯门区| 江津市| 丹凤县| 阜南县| 枝江市| 仁寿县| 白河县| 柏乡县| 宿松县| 东阳市| 彰武县| 新昌县| 高密市| 上杭县| 大丰市| 米泉市| 都兰县| 宜都市| 曲沃县| 巧家县| 慈溪市| 淳化县| 峡江县| 镇安县| 招远市| 怀安县| 仙桃市| 景泰县| 许昌县| 临夏县| 阜阳市| 莱州市| 贵州省| 阿坝县| 波密县| 山阳县|