1、自定义方法标签
引入方式示例:
<%@ taglib prefix="fns" uri="/WEB-INF/tlds/fns.tld" %>
写法示例:
1 <?xml version="1.0" encoding="UTF-8" ?> 2 3 <taglib xmlns="http://java.sun.com/xml/ns/j2ee" 4 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 5 xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd" 6 version="2.0"> 7 8 <description>JSTL 1.1 functions library</description> 9 <display-name>JSTL functions sys</display-name> 10 <tlib-version>1.1</tlib-version> 11 <short-name>fns</short-name> 12 <uri>http://java.sun.com/jsp/jstl/functionss</uri> 13 14 <!-- DictUtils --> 15 16 <function> 17 <description>获取字典对象列表</description> 18 <name>getDictList</name> 19 <function-class>com.sdyy.base.sys.utils.DictUtils</function-class> 20 <function-signature>java.util.List getDictList(java.lang.String)</function-signature> 21 <example>${fns:getDictList(typeCode)}</example> 22 </function> 23 24 <function> 25 <description>获取字典对象列表</description> 26 <name>getDictListJson</name> 27 <function-class>com.sdyy.base.sys.utils.DictUtils</function-class> 28 <function-signature>java.lang.String getDictListJson(java.lang.String)</function-signature> 29 <example>${fns:getDictListJson(typeCode)}</example> 30 </function> 31 32 33 <function> 34 <description>对象变json</description> 35 <name>toJSONString</name> 36 <function-class>com.alibaba.fastjson.JSON</function-class> 37 <function-signature>java.lang.String toJSONString(java.lang.Object)</function-signature> 38 </function> 39 </taglib>
function-class就是该方法的实体所在类路径,
function-signature就是该方法的方法名,值得一提的是,这个方法必须是个static方法。
example就是使用方法示例
2、通过jsp自定义标签
<%@ taglib prefix="sys" tagdir="/WEB-INF/tags/sys" %>
如此 tags目录下的sys目录下的jsp形式的tag就会自动被加载,标签jsp写法示例:
1 <%@ tag language="java" pageEncoding="UTF-8"%> 2 <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> 3 <%@ taglib prefix="fns" uri="/WEB-INF/tlds/fns.tld" %> 4 <%@ attribute name="typeCode" type="java.lang.String" required="true" description="字典code"%> 5 <%@ attribute name="defaultValue" type="java.lang.String" description="默认选中"%> 6 <%@ attribute name="style" type="java.lang.String" description="默认选中"%> 7 <%@ attribute name="cls" type="java.lang.String" description="默认选中"%> 8 <%@ attribute name="name" type="java.lang.String" description="默认选中"%> 9 <select style="${style}" class="${cls}" name="${name}" id="${name}" > 10 <option value="" >请选择... </option> 11 <c:if test="${not empty typeCode}"> 12 <c:forEach items="${fns:getDictList(typeCode)}" var='dict'> 13 <option value='${dict.VALUE}' ${defaultValue==dict.VALUE?'selected':''}>${dict.TEXT}</option> 14 </c:forEach> 15 </c:if> 16 </select>
如此,jsp名就是标签名,例如这个jsp叫 select.jsp,那么它的用法就是
<sys:select cls="formselect" name="MODULE_TYPE" typeCode="HOME_MODULE_TYPE" defaultValue="${record.MODULE_TYPE }" />
3、通过tld文件和java代码自定义元素标签
引入方式和1相同,写法示例如下:
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN" 3 "http://java.sun.com/dtd/web-jsptaglibrary_1_2.dtd"> 4 <taglib> 5 <tlib-version>1.0</tlib-version> 6 <jsp-version>2.0</jsp-version> 7 <short-name>bgt</short-name> 8 <!-- backGroundTag --> 9 <uri>http://www.sdyy.tag</uri> 10 <tag> 11 <name>hasUrlPerm</name> 12 <tag-class>com.sdyy.common.tags.HasUrlPermissionTag</tag-class> 13 <attribute> 14 <name>link</name> 15 <required>false</required> 16 <rtexprvalue>true</rtexprvalue><!-- 是否支持恶劣表达式 --> 17 <type>java.lang.String</type> 18 <description>示例:acApplication/forMain.do</description> 19 </attribute> 20 21 </tag> 22 </taglib>
A、【判断标签】HasUrlPermissionTag标签是一个判断标签,通过该标签来决定标签包裹的内容是否显示,写法如下:
1 package com.sdyy.common.tags; 2 3 4 5 import javax.servlet.http.HttpServletRequest; 6 import javax.servlet.jsp.JspException; 7 import javax.servlet.jsp.tagext.BodyTagSupport; 8 9 import com.sdyy.common.spring.interceptor.PermissionInterceptor; 10 /** 11 * 12 * @ClassName: HasUrlPermissionTag 13 * @Description: 根据url判断权限标签 14 * @author: liuyx 15 * @date: 2015年12月21日上午11:15:32 16 */ 17 public class HasUrlPermissionTag extends BodyTagSupport { 18 19 private String link;// acApplication/forMain.do 20 21 @Override 22 public int doStartTag() throws JspException { // 在标签开始处出发该方法 23 24 HttpServletRequest request=(HttpServletRequest) pageContext.getRequest(); 25 //获取session中存放的权限 26 27 //判断是否有权限访问 28 if (PermissionInterceptor.isOperCanAccess(request, link)) { 29 //允许访问标签body 30 return BodyTagSupport.EVAL_BODY_INCLUDE;// 返回此则执行标签body中内容,SKIP_BODY则不执行 31 } else { 32 return BodyTagSupport.SKIP_BODY; 33 } 34 35 } 36 37 @Override 38 public int doEndTag() throws JspException { 39 return BodyTagSupport.EVAL_BODY_INCLUDE; 40 } 41 42 public String getLink() { 43 return link; 44 } 45 46 public void setLink(String link) { 47 this.link = link; 48 } 49 50 }
在JSP中的使用方式:
<bgt:hasUrlPerm link="abc.do">
<div>tttttttttttttttttest</div>
</bgt:hasUrlPerm>
B、【控件标签】,这种标签直接返回一个控件,不过是通过java代码生成的控件内容,写法示例:
1 package com.sdyy.common.tags; 2 3 import java.io.IOException; 4 import java.util.ArrayList; 5 import java.util.List; 6 7 import javax.servlet.jsp.JspTagException; 8 import javax.servlet.jsp.JspWriter; 9 import javax.servlet.jsp.tagext.BodyTagSupport; 10 11 /*import com.sdyy.base.ac.ac_permission.model.AcPermission;*/ 12 13 public class ButtonUrlTag extends BodyTagSupport { 14 private static final long serialVersionUID = -7811902545513255473L; 15 16 17 //标签属性用户名 18 private String user = null; 19 //标签属性操作url 20 private String url = null; 21 //标签属性 js方法 22 private String jsmethod = null; 23 //标签属性image 按钮图片 24 private String image = null; 25 //标签属性 alt 提示 26 private String alt = null; 27 //标签属性操作value 按钮文本 28 private String value = null; 29 30 31 32 33 /* 标签初始方法 */ 34 public int doStartTag() throws JspTagException{ 35 return super.EVAL_BODY_INCLUDE; 36 } 37 38 39 /* 标签结束方法 */ 40 public int doEndTag() throws JspTagException{ 41 pageContext.getSession(); 42 Boolean b = false; 43 List list = new ArrayList(); 44 /*AcPermission p = new AcPermission();*/ 45 /*JDBCHibernate jdbca = new JDBCHibernate();*/ 46 try { 47 /*list = jdbca.getHaveURLByUsernameList(user);*/ 48 } catch (Exception e1) { 49 // TODO Auto-generated catch block 50 e1.printStackTrace(); 51 } 52 for(int i = 0;i < list.size(); i++){ 53 /*p = (AcPermission) list.get(i);*/ 54 if(1==1) {//p.getUrl().trim().equals(url.trim())){ 55 b = true; 56 //如果jsmethod属性不为null 则把超链接href改为调用js 57 if(jsmethod!=null){ 58 url = jsmethod; 59 } 60 } 61 } 62 JspWriter out = pageContext.getOut(); 63 if(b){ 64 try { 65 //有权限 显示操作按钮 66 out.println("<a href='" +url+ "' class='regular'><img src='" + image + "' alt='" + alt +"' />" + value + "</a>"); 67 } catch (IOException e) { 68 e.printStackTrace(); 69 } 70 } 71 72 return super.SKIP_BODY; 73 } 74 75 /* 释放资源 */ 76 public void release(){ 77 super.release(); 78 } 79 80 81 public String getUser() { 82 return user; 83 } 84 85 86 public void setUser(String user) { 87 this.user = user; 88 } 89 90 91 public String getUrl() { 92 return url; 93 } 94 95 96 public void setUrl(String url) { 97 this.url = url; 98 } 99 100 101 public String getImage() { 102 return image; 103 } 104 105 106 public void setImage(String image) { 107 this.image = image; 108 } 109 110 111 public String getAlt() { 112 return alt; 113 } 114 115 116 public void setAlt(String alt) { 117 this.alt = alt; 118 } 119 120 121 public String getValue() { 122 return value; 123 } 124 125 126 public void setValue(String value) { 127 this.value = value; 128 } 129 130 public String getJsmethod() { 131 return jsmethod; 132 } 133 134 135 public void setJsmethod(String jsmethod) { 136 this.jsmethod = jsmethod; 137 } 138 }