1.First add this to web.xml:
1 2 3 4 | <taglib> <taglib-uri>http://longshine.com/cufj</taglib-uri> <taglib-location>/WEB-INF/tlds/cufjTag.tld</taglib-location> </taglib> |
2.Put the tld file into folder: /WEB-INF/tlds/. And the tld file is like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN" "http://java.sun.com/dtd/web-jsptaglibrary_1_2.dtd"> <taglib> <tlib-version>1.0</tlib-version> <jsp-version>1.2</jsp-version> <short-name>cufjTag</short-name> <uri>http://longshine.com/cufj</uri> <tag> <name>role</name> <tag-class>longshine.telecom.mob.tag.RoleTag</tag-class> <body-content>JSP</body-content> <attribute> <name>roleIds</name> <required>true</required> </attribute> </tag> </taglib> |
3.Now is the java class:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 | package longshine.telecom.mob.tag; import java.io.IOException; import javax.servlet.jsp.JspException; import javax.servlet.jsp.tagext.BodyContent; import javax.servlet.jsp.tagext.BodyTagSupport; import longshine.telecom.mob.common.SessionUserObject; import longshine.telecom.mob.service.BaseService; public class RoleTag extends BodyTagSupport { private static final long serialVersionUID = 1L; private String roleIds; public int doEndTag() throws JspException { BodyContent bc = getBodyContent(); if (roleIds != null) { SessionUserObject sessionUserObject = (SessionUserObject) pageContext.getSession() .getAttribute(BaseService.SESSION_USER_OBJECT); if (sessionUserObject == null || sessionUserObject.getPermissions() == null) { return SKIP_BODY; } String[] roles = roleIds.split(","); for (int i = 0; i < roles.length; i++) { if (sessionUserObject.getPermissions().contains(roles[i])) { try { pageContext.getOut().print(bc.getString()); return SKIP_BODY; } catch (IOException e) { e.printStackTrace(); } } } } return SKIP_BODY; } public String getRoleIds() { return roleIds; } public void setRoleIds(String roleIds) { this.roleIds = roleIds; } } |
5.At last, you can write JSP with this tag, remember to declare this tag in the JSP head:
<%@taglib uri="http://longshine.com/cufj" prefix="c" %>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | <%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@taglib uri="http://longshine.com/cufj" prefix="c" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP TAG</title>
</head>
<body>
<c:role roleIds="002">
<input type="text" name="n"></input>
</c:role>
</body>
</html> |
Enjoy it!