首页 > 代码库 > java中结合struts2自定义标签的使用

java中结合struts2自定义标签的使用

java中结合struts2自定义标签的使用

一、建立一个继承于SimpleTagSupport类实现它的doTag方法

 

 1 package com.xiangshang.tag;
 2 
 3 import java.io.IOException;
 4 import java.util.List;
 5 
 6 import javax.servlet.jsp.JspException;
 7 import javax.servlet.jsp.PageContext;
 8 import javax.servlet.jsp.tagext.SimpleTagSupport;
 9 
10 import com.opensymphony.xwork2.ActionContext;
11 import com.opensymphony.xwork2.util.ValueStack;
12 
13 public class FunctionTag extends SimpleTagSupport {
14     
15     private String value;
16 
17     private String name;
18 
19     public String getName() {
20         return name;
21     }
22 
23     public void setName(String name) {
24         this.name = name;
25     }
26 
27     public String getValue() {
28         return value;
29     }
30 
31     public void setValue(String value) {
32         this.value =http://www.mamicode.com/ value;
33     }
34 
35     @SuppressWarnings("unchecked")
36     @Override
37     public void doTag() throws JspException, IOException {
38         StringBuilder sb = new StringBuilder();
39         PageContext pageContext = (PageContext) this.getJspContext();
40         List<String> functions = (List<String>) pageContext.getSession()
41                 .getAttribute("functions");
42         ValueStack vs = ActionContext.getContext().getValueStack();
43         String str = (String) vs.findValue(name);
44         for (String s : functions) {
45             if (s.equals(str)) {
46                 sb.append("<a href=http://www.mamicode.com/"javascript:alert(‘有权限‘)\">")
47                         .append(value).append("</a>");
48                 break;
49             }
50         }
51         if (sb.length()==0) {
52             sb.append("<a href=http://www.mamicode.com/"javascript:alert(‘没权限!!‘)\">")
53                     .append(value).append("</a>");
54         }
55         pageContext.getOut().print(sb);
56     }
57 }

  为了测试方便本人在建立了session监听器来存储function:

   

 1 package com.xiangshang.listener;
 2 
 3 import java.util.ArrayList;
 4 import java.util.List;
 5 
 6 import javax.servlet.ServletContext;
 7 import javax.servlet.ServletContextEvent;
 8 import javax.servlet.ServletContextListener;
 9 import javax.servlet.http.HttpSessionEvent;
10 import javax.servlet.http.HttpSessionListener;
11 
12 import org.springframework.web.context.WebApplicationContext;
13 import org.springframework.web.context.support.WebApplicationContextUtils;
14 
15 import com.xiangshang.service.UserService;
16 
17 public class MySessionListener implements HttpSessionListener{
18     @Override
19     public void sessionCreated(HttpSessionEvent hse) {
20         System.out.println("Zhangqy Session Created");
21         List<String> functions= new ArrayList<String>();
22         functions.add("添加人信息");
23         functions.add("删除人信息");
24         functions.add("查看人信息");
25         functions.add("修改人信息");
26         hse.getSession().setAttribute("functions",functions);
27     }
28 
29     @Override
30     public void sessionDestroyed(HttpSessionEvent hse) {
31     }
32 
33 
34 }

 

二、在WEB-INF下创建tld 文件:xiangshang.tld

 

 1  <?xml version="1.0" encoding="UTF-8"?>
 2 <!DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN" "http://java.sun.com/dtd/web-jsptaglibrary_1_2.dtd" >
 3 <taglib>
 4  <tlib-version>1.0</tlib-version>
 5  <jsp-version>2.0</jsp-version>
 6  <short-name>x</short-name>
 7  <uri>/xiangshang-tags</uri>
 8  <display-name>xiangshang tags</display-name>
 9  <tag>
10   <name>a</name>
11   <tag-class>com.xiangshang.tag.FunctionTag</tag-class>
12   <body-content>empty</body-content>  
13   <attribute>
14    <name>name</name>
15    <required>true</required>
16    <rtexprvalue>true</rtexprvalue>
17    <type>java.lang.String</type> 
18   </attribute>  
19    <attribute>
20    <name>value</name>
21    <required>true</required>
22    <rtexprvalue>true</rtexprvalue>
23    <type>java.lang.String</type> 
24   </attribute> 
25  </tag>
26  </taglib>

三、在jsp界面中首先倒入自定义的标签库 :

<%@ taglib uri="/xiangshang-tags" prefix="x" %>

四、接下来就是开始使用自定义标签了:

<x:a name="添加人信息" value="添加人信息"/>