PersonTag.java
,須要繼承 BodyTagSupport
類,並重寫 doStartTag
方法,在該方法內部實現渲染咱們業務上須要展現的html對應的邏輯。package com.terwergreen; import com.terwergreen.util.StringUtil; import org.apache.commons.beanutils.PropertyUtils; import javax.servlet.http.HttpServletRequest; import javax.servlet.jsp.JspException; import javax.servlet.jsp.tagext.BodyTagSupport; import java.io.IOException; import java.util.ArrayList; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; /** * PersonTag * * @author Terwer * @version 1.0 * 2019/10/16 14:24 **/ public class PersonTag extends BodyTagSupport { protected String personName; protected String loginName; protected String personId; protected Boolean layer = Boolean.TRUE; protected Object bean; public String getPersonName() { return personName; } public void setPersonName(String personName) { this.personName = personName; } public String getLoginName() { return loginName; } public void setLoginName(String loginName) { this.loginName = loginName; } public String getPersonId() { return personId; } public void setPersonId(String personId) { this.personId = personId; } public Boolean getLayer() { return layer; } public void setLayer(Boolean layer) { this.layer = layer; } public Object getBean() { return bean; } public void setBean(Object bean) { this.bean = bean; } public int doStartTag() throws JspException { try { String cxpath = ((HttpServletRequest) pageContext.getRequest()) .getContextPath(); StringBuffer sb = new StringBuffer(); List<Map<String, String>> list = this.parseToOrgList(); for (int i = 0; i < list.size(); i++) { Map<String, String> tmpPerson = list.get(i); if (list.size() > 1 && i != list.size() - 1) { tmpPerson.put("fdName", tmpPerson.get("fdName") + ";"); } sb.append(render(cxpath, tmpPerson, layer != null ? layer.booleanValue() : true)); } pageContext.getOut().print(sb.toString()); } catch (Exception e) { e.printStackTrace(); } return SKIP_BODY; } public static StringBuffer render(String cxpath, Map<String, String> tmpPerson, boolean layer) { StringBuffer sb = new StringBuffer(); sb.append("<a class=\"com_author\" " + " href=\"javascript:void(0) \""); // sb.append("<a class=\"com_author\" " // + "href=\"" // + cxpath // + "/sys/person/sys_person_zone/sysPersonZone.do?method=view&fdId=" // + tmpPerson.get("fdId") + "\" target=\"_blank\""); if (layer) { sb.append(" ajax-href=\"" + cxpath + "/sys/zone/resource/zoneInfo.jsp?fdId=" + tmpPerson.get("fdId") + "\""); sb.append(" onmouseover=\"if(window.LUI && window.LUI.person)window.LUI.person(event,this);\""); } sb.append(">"); sb.append(org.apache.commons.lang.StringEscapeUtils.escapeHtml(tmpPerson.get("fdName"))); sb.append("</a>"); return sb; } private List<Map<String, String>> parseToOrgList() throws Exception { List<Map<String, String>> rtnList = new ArrayList<Map<String, String>>(); if (StringUtil.isNotNull(personId)) { if (personId.indexOf(";") > -1) { String[] persons = personId.split(";"); String[] names = null; if (StringUtil.isNotNull(personName)) { names = personName.split(";"); } for (int i = 0; i < persons.length; i++) { rtnList.add(parseToMap(persons[i], names == null ? null : (names[i] == null ? null : names[i]), loginName)); } } else { rtnList.add(parseToMap(personId, personName, loginName)); } } else if (bean != null) { Object tmpBean = null; if (bean instanceof String) { String beanCfg = (String) bean; if (beanCfg.indexOf(";") > -1) { String[] beans = beanCfg.split(";"); for (int i = 0; i < beans.length; i++) { rtnList.add(parseToMap(pageContext .getAttribute(beans[i]))); } } else { tmpBean = pageContext.getAttribute(beanCfg); } } else { tmpBean = bean; } if (tmpBean != null) { if (tmpBean instanceof Object[]) { Object[] beans = (Object[]) tmpBean; for (int i = 0; i < beans.length; i++) { rtnList.add(parseToMap(beans[i])); } } else if (tmpBean instanceof List) { List<?> beans = (List<?>) tmpBean; for (int i = 0; i < beans.size(); i++) { rtnList.add(parseToMap(beans.get(i))); } } else if (tmpBean instanceof Map) { Map beans = (Map) tmpBean; for (Iterator iterator = beans.values().iterator(); iterator .hasNext(); ) { Object tmpObject = (Object) iterator.next(); rtnList.add(parseToMap(tmpObject)); } } else { rtnList.add(parseToMap(tmpBean)); } } } return rtnList; } private Map<String, String> parseToMap(String fdId, String fdName, String fdLoginName) throws Exception { Map<String, String> map = new HashMap<String, String>(); map.put("fdId", fdId); if (StringUtil.isNotNull(fdName)) { map.put("fdName", fdName); map.put("fdLoginName", fdLoginName); } else { try { map.put("fdName", "唐有煒"); } catch (Exception e) { map.put("fdName", ""); } } return map; } private Map<String, String> parseToMap(Object beanObj) throws Exception { Map<String, String> map = new HashMap<String, String>(); if (beanObj != null) { map.put("fdId", (String) PropertyUtils.getProperty(beanObj, "fdId")); map.put("fdName", (String) PropertyUtils.getProperty(beanObj, "fdName")); map.put("fdLoginName", (String) PropertyUtils.getProperty(beanObj, "fdLoginName")); } return map; } }
二、建立tld描述文件,位置放在 WEB-INF/tld/ui.tld
下javascript
<?xml version="1.0" encoding="UTF-8"?> <taglib xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee web-jsptaglibrary_2_0.xsd" version="2.0"> <tlib-version>2.0</tlib-version> <jsp-version>2.0</jsp-version> <short-name>ui</short-name> <uri>http://www.landray.com.cn/tags/widget</uri> <tag> <name>person</name> <tag-class>com.terwergreen.PersonTag</tag-class> <body-content>JSP</body-content> <attribute> <description>personId</description> <name>personId</name> <required>false</required> <rtexprvalue>true</rtexprvalue> </attribute> <attribute> <description>personName</description> <name>personName</name> <required>false</required> <rtexprvalue>true</rtexprvalue> </attribute> <attribute> <description>bean</description> <name>bean</name> <required>false</required> <rtexprvalue>true</rtexprvalue> <type>java.lang.Object</type> </attribute> <attribute> <description>layer</description> <name>layer</name> <required>false</required> <rtexprvalue>true</rtexprvalue> <type>java.lang.Boolean</type> </attribute> <!-- <attribute> <description>loginName</description> <name>loginName</name> <required>false</required> <rtexprvalue>true</rtexprvalue> </attribute> --> </tag> </taglib>
三、web.xml
加入 taglibhtml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" version="4.0"> <jsp-config> <taglib> <taglib-uri>ui</taglib-uri> <taglib-location>/WEB-INF/tld/ui.tld</taglib-location> </taglib> </jsp-config> </web-app>
四、建立測試頁面java
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <%@ taglib uri="/WEB-INF/tld/ui.tld" prefix="ui" %> <html> <head> <title>Title</title> </head> <body> <div>Hello World</div> <ui:person personId="111111111"></ui:person> </body> </html>