天天看點

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. public class HasUrlPermissionTag extends BodyTagSupport {

  7. private String link;// acApplication/forMain.do

  8. @Override

  9. public int doStartTag() throws JspException { // 在标簽開始處出發該方法

  10. HttpServletRequest request=(HttpServletRequest) pageContext.getRequest();

  11. //擷取session中存放的權限

  12. //判斷是否有權限通路

  13. if (PermissionInterceptor.isOperCanAccess(request, link)) {

  14. //允許通路标簽body

  15. return BodyTagSupport.EVAL_BODY_INCLUDE;// 傳回此則執行标簽body中内容,SKIP_BODY則不執行

  16. } else {

  17. return BodyTagSupport.SKIP_BODY;

  18. }

  19. }

  20. @Override

  21. public int doEndTag() throws JspException {

  22. return BodyTagSupport.EVAL_BODY_INCLUDE;

  23. }

  24. public String getLink() {

  25. return link;

  26. }

  27. public void setLink(String link) {

  28. this.link = link;

  29. }

  30. }

在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. public class ButtonUrlTag extends BodyTagSupport {

  9. private static final long serialVersionUID = -7811902545513255473L;

  10. //标簽屬性使用者名

  11. private String user = null;

  12. //标簽屬性操作url

  13. private String url = null;

  14. //标簽屬性 js方法

  15. private String jsmethod = null;

  16. //标簽屬性image 按鈕圖檔

  17. private String image = null;

  18. //标簽屬性 alt 提示

  19. private String alt = null;

  20. //标簽屬性操作value 按鈕文本

  21. private String value = null;

  22. public int doStartTag() throws JspTagException{

  23. return super.EVAL_BODY_INCLUDE;

  24. }

  25. public int doEndTag() throws JspTagException{

  26. pageContext.getSession();

  27. Boolean b = false;

  28. List list = new ArrayList();

  29. try {

  30. } catch (Exception e1) {

  31. // TODO Auto-generated catch block

  32. e1.printStackTrace();

  33. }

  34. for(int i = 0;i < list.size(); i++){

  35. if(1==1) {//p.getUrl().trim().equals(url.trim())){

  36. b = true;

  37. //如果jsmethod屬性不為null 則把超連結href改為調用js

  38. if(jsmethod!=null){

  39. url = jsmethod;

  40. }

  41. }

  42. }

  43. JspWriter out = pageContext.getOut();

  44. if(b){

  45. try {

  46. //有權限 顯示操作按鈕

  47. out.println("<a href='" +url+ "' class='regular'><img src='" + image + "' alt='" + alt +"' />" + value + "</a>");

  48. } catch (IOException e) {

  49. e.printStackTrace();

  50. }

  51. }

  52. return super.SKIP_BODY;

  53. }

  54. public void release(){

  55. super.release();

  56. }

  57. public String getUser() {

  58. return user;

  59. }

  60. public void setUser(String user) {

  61. this.user = user;

  62. }

  63. public String getUrl() {

  64. return url;

  65. }

  66. public void setUrl(String url) {

  67. this.url = url;

  68. }

  69. public String getImage() {

  70. return image;

  71. }

  72. public void setImage(String image) {

  73. this.image = image;

  74. }

  75. public String getAlt() {

  76. return alt;

  77. }

  78. public void setAlt(String alt) {

  79. this.alt = alt;

  80. }

  81. public String getValue() {

  82. return value;

  83. }

  84. public void setValue(String value) {

  85. this.value = value;

  86. }

  87. public String getJsmethod() {

  88. return jsmethod;

  89. }

  90. public void setJsmethod(String jsmethod) {

  91. this.jsmethod = jsmethod;

  92. }

  93. }