文章目录
JSP自定义标签库
Shiro标签(如 shiro:principal shiro:hasRole shiro:hasPermission)可以在JSP页面中工作,那么自定义标签是如何使用的呢
Shiro标签
shiro标签的shiro.tld文件principal Tag
<tag>
<name>principal</name>
<tag-class>org.apache.shiro.web.tags.PrincipalTag</tag-class>
<body-content>JSP</body-content>
<description>Displays the user's principal or a property of the user's principal.</description>
<attribute>
<name>type</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>property</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>defaultValue</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
Shiro principal Tag实现类
package org.apache.shiro.web.tags;
import java.beans.BeanInfo;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.io.IOException;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspTagException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class PrincipalTag extends SecureTag {
private static final Logger log = LoggerFactory.getLogger(PrincipalTag.class);
private String type;
private String property;
private String defaultValue;
public PrincipalTag() {
}
public String getType() {
return this.type;
}
public void setType(String type) {
this.type = type;
}
public String getProperty() {
return this.property;
}
public void setProperty(String property) {
this.property = property;
}
public String getDefaultValue() {
return this.defaultValue;
}
public void setDefaultValue(String defaultValue) {
this.defaultValue = defaultValue;
}
public int onDoStartTag() throws JspException {
String strValue = null;
if (this.getSubject() != null) {
Object principal;
if (this.type == null) {
principal = this.getSubject().getPrincipal();
} else {
principal = this.getPrincipalFromClassName();
}
if (principal != null) {
if (this.property == null) {
strValue = principal.toString();
} else {
strValue = this.getPrincipalProperty(principal, this.property);
}
}
}
if (strValue != null) {
try {
this.pageContext.getOut().write(strValue);
} catch (IOException var3) {
throw new JspTagException("Error writing [" + strValue + "] to JSP.", var3);
}
}
return 0;
}
private Object getPrincipalFromClassName() {
Object principal = null;
try {
Class cls = Class.forName(this.type);
principal = this.getSubject().getPrincipals().oneByType(cls);
} catch (ClassNotFoundException var3) {
if (log.isErrorEnabled()) {
log.error("Unable to find class for name [" + this.type + "]");
}
}
return principal;
}
private String getPrincipalProperty(Object principal, String property) throws JspTagException {
String strValue = null;
try {
BeanInfo bi = Introspector.getBeanInfo(principal.getClass());
boolean foundProperty = false;
PropertyDescriptor[] var6 = bi.getPropertyDescriptors();
int var7 = var6.length;
for(int var8 = 0; var8 < var7; ++var8) {
PropertyDescriptor pd = var6[var8];
if (pd.getName().equals(property)) {
Object value = pd.getReadMethod().invoke(principal, (Object[])null);
strValue = String.valueOf(value);
foundProperty = true;
break;
}
}
if (!foundProperty) {
String message = "Property [" + property + "] not found in principal of type [" + principal.getClass().getName() + "]";
if (log.isErrorEnabled()) {
log.error(message);
}
throw new JspTagException(message);
} else {
return strValue;
}
} catch (Exception var11) {
String message = "Error reading property [" + property + "] from principal of type [" + principal.getClass().getName() + "]";
if (log.isErrorEnabled()) {
log.error(message, var11);
}
throw new JspTagException(message, var11);
}
}
}
自定义标签
main.jsp
<%@ page contentType="text/html;charset=UTF-8" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="shiro" uri="http://shiro.apache.org/tags" %>
<%@ taglib prefix="mytag" uri="http://org.example/mytag" %>
<%
String name = request.getParameter("name");
%>
<html>
<head>
<title>首页</title>
</head>
<body>
<div style="color: green">
<p>使用Shiro标签输出请求参数:</p>
<p>用户[<shiro:principal defaultValue="游客"/>],您好!</p>
</div>
<div style="color: blue">
<p>使用自定义标签输出请求参数:</p>
<p>用户[<mytag:out paramKey="name" defaultValue="游客"/>],您好!</p>
<p>密码[<mytag:out paramKey="pwd" defaultValue="******"/>]</p>
</div>
<a href="/logout">注销</a><br>
<p><shiro:hasRole name="coder">您拥有[coder]角色</shiro:hasRole></p>
<p><shiro:hasPermission name="code:insert">您拥有代码[提交]权限</shiro:hasPermission></p>
<p><shiro:hasPermission name="code:update">您拥有代码[更新]权限</shiro:hasPermission></p>
<p><shiro:hasPermission name="code:delete">您拥有代码[删除]权限</shiro:hasPermission></p>
</body>
</html>
mytag.tld
<?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.2</tlib-version>
<jsp-version>1.2</jsp-version>
<short-name>Custom Tag</short-name>
<uri>http://org.example/mytag</uri>
<description>Custom Jsp Tag Library</description>
<tag>
<name>out</name>
<tag-class>shirowebdemo.customtag.MyTag</tag-class>
<body-content>JSP</body-content>
<attribute>
<name>paramKey</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>defaultValue</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
</taglib>
MyTag实现类
<!-- Tag所需的jar包--> <dependency> <groupId>javax.servlet.jsp</groupId> <artifactId>javax.servlet.jsp-api</artifactId> <version>2.3.3</version> </dependency>
import org.apache.commons.lang.StringUtils;
import javax.servlet.ServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspTagException;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.Tag;
import java.io.IOException;
/**
* 自定义JSP Tag
*
* @author admin
*/
public class MyTag implements Tag {
private PageContext pageContext;
private String paramKey;
private String defaultValue;
public String getParamKey() {
return paramKey;
}
public void setParamKey(String paramKey) {
this.paramKey = paramKey;
}
public String getDefaultValue() {
return defaultValue;
}
public void setDefaultValue(String defaultValue) {
this.defaultValue = defaultValue;
}
@Override
public void setPageContext(PageContext pageContext) {
System.out.println("called setPageContext()");
this.pageContext = pageContext;
}
@Override
public void setParent(Tag tag) {
System.out.println("called setParent()");
}
@Override
public Tag getParent() {
System.out.println("called getParent()");
return null;
}
@Override
public int doStartTag() throws JspException {
System.out.println("called doStartTag()");
String paramValue = null;
String paramKey = getParamKey();
if (StringUtils.isNotBlank(paramKey)) {
ServletRequest request = this.pageContext.getRequest();
paramValue = request.getParameter(paramKey);
}
if (StringUtils.isBlank(paramValue)) {
paramValue = getDefaultValue();
}
if (StringUtils.isBlank(paramValue)) {
paramValue = "";
}
try {
this.pageContext.getOut().write(paramValue);
} catch (IOException e) {
e.printStackTrace();
throw new JspTagException("Error writing [" + paramValue + "] to JSP.", e);
}
return 0;
}
@Override
public int doEndTag() throws JspException {
System.out.println("called doEndTag()");
return 0;
}
@Override
public void release() {
System.out.println("called release()");
}
}