jsp tld 的tag 自定义标签扩展

引入方式

  1. <%@ taglib prefix="bgt" uri="/WEB-INF/tlds/bgt.tld" %>

写法示例如下:

  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. </tag>
  21. </taglib>


A、【判断标签】HasUrlPermissionTag标签是一个判断标签,通过该标签来决定标签包裹的内容是否显示,写法如下:

  1. package com.sdyy.common.tags;
  2. import javax.servlet.http.HttpServletRequest;
  3. import javax.servlet.jsp.JspException;
  4. import javax.servlet.jsp.tagext.BodyTagSupport;
  5. import com.sdyy.common.spring.interceptor.PermissionInterceptor;
  6. /**
  7. *
  8. * @ClassName: HasUrlPermissionTag
  9. * @Description: 根据url判断权限标签
  10. * @author: liuyx
  11. * @date: 2015年12月21日上午11:15:32
  12. */
  13. public class HasUrlPermissionTag extends BodyTagSupport {
  14. private String link;// acApplication/forMain.do
  15. @Override
  16. public int doStartTag() throws JspException { // 在标签开始处出发该方法
  17. HttpServletRequest request=(HttpServletRequest) pageContext.getRequest();
  18. //获取session中存放的权限
  19. //判断是否有权限访问
  20. if (PermissionInterceptor.isOperCanAccess(request, link)) {
  21. //允许访问标签body
  22. return BodyTagSupport.EVAL_BODY_INCLUDE;// 返回此则执行标签body中内容,SKIP_BODY则不执行
  23. } else {
  24. return BodyTagSupport.SKIP_BODY;
  25. }
  26. }
  27. @Override
  28. public int doEndTag() throws JspException {
  29. return BodyTagSupport.EVAL_BODY_INCLUDE;
  30. }
  31. public String getLink() {
  32. return link;
  33. }
  34. public void setLink(String link) {
  35. this.link = link;
  36. }
  37. }


在JSP中的使用方式:

    <bgt:hasUrlPerm link="abc.do"><div>tttttttttttttttttest</div></bgt:hasUrlPerm>

B、【控件标签】,这种标签直接返回一个控件,不过是通过java代码生成的控件内容,写法示例:


  1. package com.sdyy.common.tags;
  2. import java.io.IOException;
  3. import java.util.ArrayList;
  4. import java.util.List;
  5. import javax.servlet.jsp.JspTagException;
  6. import javax.servlet.jsp.JspWriter;
  7. import javax.servlet.jsp.tagext.BodyTagSupport;
  8. /*import com.sdyy.base.ac.ac_permission.model.AcPermission;*/
  9. public class ButtonUrlTag extends BodyTagSupport {
  10. private static final long serialVersionUID = -7811902545513255473L;
  11. //标签属性用户名
  12. private String user = null;
  13. //标签属性操作url
  14. private String url = null;
  15. //标签属性 js方法
  16. private String jsmethod = null;
  17. //标签属性image 按钮图片
  18. private String image = null;
  19. //标签属性 alt 提示
  20. private String alt = null;
  21. //标签属性操作value 按钮文本
  22. private String value = null;
  23. /* 标签初始方法 */
  24. public int doStartTag() throws JspTagException{
  25. return super.EVAL_BODY_INCLUDE;
  26. }
  27. /* 标签结束方法 */
  28. public int doEndTag() throws JspTagException{
  29. pageContext.getSession();
  30. Boolean b = false;
  31. List list = new ArrayList();
  32. /*AcPermission p = new AcPermission();*/
  33. /*JDBCHibernate jdbca = new JDBCHibernate();*/
  34. try {
  35. /*list = jdbca.getHaveURLByUsernameList(user);*/
  36. } catch (Exception e1) {
  37. // TODO Auto-generated catch block
  38. e1.printStackTrace();
  39. }
  40. for(int i = 0;i < list.size(); i++){
  41. /*p = (AcPermission) list.get(i);*/
  42. if(1==1) {//p.getUrl().trim().equals(url.trim())){
  43. b = true;
  44. //如果jsmethod属性不为null 则把超链接href改为调用js
  45. if(jsmethod!=null){
  46. url = jsmethod;
  47. }
  48. }
  49. }
  50. JspWriter out = pageContext.getOut();
  51. if(b){
  52. try {
  53. //有权限 显示操作按钮
  54. out.println("<a href='" +url+ "' class='regular'><img src='" + image + "' alt='" + alt +"' />" + value + "</a>");
  55. } catch (IOException e) {
  56. e.printStackTrace();
  57. }
  58. }
  59. return super.SKIP_BODY;
  60. }
  61. /* 释放资源 */
  62. public void release(){
  63. super.release();
  64. }
  65. public String getUser() {
  66. return user;
  67. }
  68. public void setUser(String user) {
  69. this.user = user;
  70. }
  71. public String getUrl() {
  72. return url;
  73. }
  74. public void setUrl(String url) {
  75. this.url = url;
  76. }
  77. public String getImage() {
  78. return image;
  79. }
  80. public void setImage(String image) {
  81. this.image = image;
  82. }
  83. public String getAlt() {
  84. return alt;
  85. }
  86. public void setAlt(String alt) {
  87. this.alt = alt;
  88. }
  89. public String getValue() {
  90. return value;
  91. }
  92. public void setValue(String value) {
  93. this.value = value;
  94. }
  95. public String getJsmethod() {
  96. return jsmethod;
  97. }
  98. public void setJsmethod(String jsmethod) {
  99. this.jsmethod = jsmethod;
  100. }
  101. }



posted on 2017-03-22 09:16  signheart  阅读(1352)  评论(0编辑  收藏  举报

导航