如何设置自定义标签

1.什么是自定义标签,查询单独的某种用户的特定信息时可以使用自定义标签。

2.如何设置自定义标签。

   1.

   2.全部代码如下:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<taglib xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.0" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd">
 
  <display-name>Ruiec Tags</display-name>
  <tlib-version>1.0</tlib-version>
  <short-name>ruiec</short-name>
  <uri>/ruiec-tags</uri>

    <tag>
        <name>hasPerms</name>
        <tag-class>com.ruiec.web.template.jsp.directive.HasPermsTag</tag-class>
        <body-content>scriptless</body-content>
        <attribute>
          <name>perms</name>
          <required>true</required>
          <rtexprvalue>true</rtexprvalue>
        </attribute>
    </tag>
    
    <tag>
        <name>date</name>
        <tag-class>com.ruiec.web.template.jsp.directive.DateTag</tag-class>
        <body-content>scriptless</body-content>
        <attribute>
            <name>startDate</name>
            <required>true</required>
            <rtexprvalue>true</rtexprvalue>
        </attribute>
        <attribute>
            <name>endDate</name>
            <required>true</required>
            <rtexprvalue>true</rtexprvalue>
        </attribute>
        <attribute>
            <name>type</name>
            <required>true</required>
            <rtexprvalue>true</rtexprvalue>
        </attribute>
        <attribute>
            <name>var</name>
            <required>true</required>
            <rtexprvalue>true</rtexprvalue>
        </attribute>
    </tag>
    <tag>
        <name>module</name>
        <tag-class>com.ruiec.web.template.jsp.directive.ModuleTag</tag-class>
        <body-content>scriptless</body-content>
        <attribute>
            <name>parentId</name>
            <type>java.lang.Integer</type>
            <rtexprvalue>true</rtexprvalue>
        </attribute>
        <attribute>
            <name>var</name>
            <rtexprvalue>true</rtexprvalue>
        </attribute>
    </tag>
    <tag>
        <name>department</name>
        <tag-class>com.ruiec.web.template.jsp.directive.DepartmentTag</tag-class>
        <body-content>scriptless</body-content>
        <attribute>
            <name>parentId</name>
            <type>java.lang.Integer</type>
            <rtexprvalue>true</rtexprvalue>
        </attribute>
        <attribute>
            <name>var</name>
            <rtexprvalue>true</rtexprvalue>
        </attribute>
    </tag>
    
    
    <tag>
        <name>user</name>
        <tag-class>com.ruiec.web.template.jsp.directive.UserTag</tag-class>
        <body-content>scriptless</body-content>
        <attribute>
            <name>userId</name>
            <type>java.lang.Integer</type>
            <rtexprvalue>true</rtexprvalue>
        </attribute>
        <attribute>
            <name>var</name>
            <required>true</required>
            <rtexprvalue>true</rtexprvalue>
        </attribute>
    </tag>
    
    <tag><!-- 查询回复帖子 -->
        <name>replyArticle</name>
        <tag-class>com.ruiec.web.template.jsp.directive.ReplyArticleTag</tag-class>
        <body-content>scriptless</body-content>
        <attribute>
            <name>replyArticleId</name>
            <type>java.lang.Integer</type>
            <rtexprvalue>true</rtexprvalue>
        </attribute>
        <attribute>
            <name>var</name>
            <required>true</required>
            <rtexprvalue>true</rtexprvalue>
        </attribute>
    </tag>
    

    
</taglib>

   3.配置Tag文件如下,这里相当一个查询。

package com.ruiec.web.template.jsp.directive;

import java.io.IOException;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.SimpleTagSupport;

import com.ruiec.web.model.Department;
import com.ruiec.web.service.DepartmentService;
import com.ruiec.web.util.SpringUtils;

/**
 * 部门标签
 * 
 * @author wang w<br>
 * @date 2017年10月26日 下午3:11:08
 */
public class DepartmentTag extends SimpleTagSupport {

    /** ID */
    private Integer parentId;
    /** 存值变量 */
    private String var;

    /** 获取 parentId */
    public Integer getParentId() {
        return parentId;
    }

    /** 设置parentId */
    public void setParentId(Integer parentId) {
        this.parentId = parentId;
    }

    /** 获取 var */
    public String getVar() {
        return var;
    }

    /** 设置var */
    public void setVar(String var) {
        this.var = var;
    }

    /**
     * 根据父级ID,返回模型
     * 
     * @author 王伟<br>
     * @date 2017年10月26日 下午5:19:56
     */
    @Override
    public void doTag() throws JspException, IOException {
        DepartmentService departmentService = SpringUtils.getBean("departmentServiceImpl", DepartmentService.class);
        Department department = departmentService.selectByPrimaryKey(parentId);
        if (department != null) {
            // 输出版块名称
            getJspContext().getOut().write(department.getName());
        } else {
            // 输出标签体内容
            if (getJspBody() != null) {
                getJspBody().invoke(null);
            }
        }
    }

}

 

 

3.如何使用自定标签。

    1.在jsp页面上面引入初始页面,prefix="ruiec"这个是可以自定义的

     

    2. department 对应最上面的名称parentId对应传入的id   最后拿到他的名称。

 

 

 4.这个方法适用于,较多地方使用同一条数据。快速的获取到值。

 

转载于:https://www.cnblogs.com/javawangwei/p/8056341.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值