jsp-tag

作者: KeDaiBiaO1 | 来源:发表于2018-01-29 17:13 被阅读0次

tag-support 有2种

  1. 只产生结果 比如:公式(format数据,转日期格式等)
    这个不会生成html代码,只会根据输入获取计算后的值 并显示。
  2. 产生html代码
    产生标签, 比如下面讲的生成下拉框。
目的:

生成的jsp(html)select 标签 ,select中有属性,以及 option子标签(子标签包含属性)

问题:(只产生结果,分页 是可以的)

如果修改怎么办。 需要回传select中的值。 应该也没问题 回传使用的是js拼接html。

代码:
  1. Java tag
    生成
  2. tld文件
注意:
  1. set值
    <handler:select class="selectpicker" name='color' size='1' optionsList='${colorList}' />
    jsp中有4个值

可以使用2种方式set值先执行第一个,然后剩下的属性值 逐个进入2方法(每个属性调用一次)

  1. 指定和前端的属性值一样的 然后实现它的set方法
  public void setOptionsList(List value){
      this.optionsList = value;
  }
  public void setName(String value) {
      this.name = value;
  }
  1. 使用DynamicAttributes接口的 setDynamicAttributes方法
  public void setDynamicAttribute(String uri, String name, Object value) {
      tagAttrs.put(name, value);
  }

这个会逐个的set进属性值

余下的操作
  1. 获取JspWriter 对象out
    然后就可以往out中printhtml的代码。
  2. String.format()
PageContext pageContext = (PageContext) getJspContext();
        JspWriter out = pageContext.getOut();
    //附加两个属性
    private List optionsList = null;
    private String name;
    //tagAttrs 其他全部的属性
    private Map<String,Object> tagAttrs = new HashMap<String,Object>();
public class SelectTagHandler extends SimpleTagSupport implements DynamicAttributes{
    //附加两个属性
    private List optionsList = null;
    private String name;
    //tagAttrs 其他全部的属性
    private Map<String,Object> tagAttrs = new HashMap<String,Object>();
    private static final String ATTR_TEMPLATE = "%s='%s' ";
    private static final String OPTION_TEMPLATE = " <option value='%1$s'> %1$s </option>";
    public void setOptionsList(List value){
        this.optionsList = value;
    }
    public void setName(String value) {
        this.name = value;
    }
    // store all other (dynamic) attributes
    public void setDynamicAttribute(String uri, String name, Object value) {
        tagAttrs.put(name, value);
    }
    @Override
    public void doTag() throws JspException, IOException {
        PageContext pageContext = (PageContext) getJspContext();
        JspWriter out = pageContext.getOut();
        out.print(" <select ");
        out.print(String.format(ATTR_TEMPLATE, "name", this.name));
        for(String attrName : tagAttrs.keySet()){
            String attrDefinition = String.format(ATTR_TEMPLATE, attrName, tagAttrs.get(attrName));
            out.print(attrDefinition);

        }
        out.print(">");
        // Generate the <option> tags from the optionsList
        for ( Object option : optionsList ) {
            String optionTag = String.format(OPTION_TEMPLATE, option.toString());
            out.println(optionTag);
        }
        // End the HTML </select> tag
        out.println(" </select>");
    }
}

<?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>JSTL 1.1 functions library</description>
    <display-name>JSTL functions sys</display-name>
    <tlib-version>1.1</tlib-version>
    <short-name>handler</short-name>
    <uri>http://java.sun.com/jsp/jstl/f</uri>
    <description>
        An example tab library of replacements for the HTML form tags.
    </description>
    <tag>
        <name>select</name>
        <tag-class>cn.chinapost.jdpt.ewms.web.common.utils.tags.SelectTagHandler</tag-class>
        <body-content>empty</body-content>
        <description>
            This tag constructs an HTML form ‘select’ tag. It also generates
            the ‘option’ tags based on the set of items in a list passed in
            by the optionsList tag attribute.
        </description>
        <attribute>
            <name>optionsList</name>
            <type>java.util.List</type>
            <required>true</required>
            <rtexprvalue>true</rtexprvalue>
        </attribute>
        <attribute>
            <name>name</name>
            <required>true</required>
        </attribute>
        <dynamic-attributes>true</dynamic-attributes>
    </tag>
</taglib>

相关文章

  • jsp-tag

    tag-support 有2种 只产生结果 比如:公式(format数据,转日期格式等)这个不会生成html代码,...

网友评论

      本文标题:jsp-tag

      本文链接:https://www.haomeiwen.com/subject/vkixzxtx.html