功能 :判断当前用户是否有权限访问当页面
思路:读取当前用户的权限(在登陆验证是设置的)
和标签在jsp页面取到的值进行比较,不匹配时跳转到指定页面
总结:标签类一般存放在web-inf/lib
xxxx.tld 配置 标签类的参数定义和指定类名
web-inf.xml 指定xxxx.tld的位置和定义名字
<mytag:CheckPower power="manager" gotoPage="login.jsp"/>
错误总结:写标签类的时候get/set方法漏了 ,报错。
写jsp页面时ctrl c ctrl v时 uri没改,页面不认。
下步目标:标签类进行验证时能将验证错误信息写入session或者request
然后能通过jsp页面读取到该错误信息
===================CheckPower.class(标签类程序)==================================
package com.mytld;
import java.io.IOException;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;
public class CheckPower extends TagSupport {
private String power;
private String gotoPage;
public int doEndTag() throws JspException {
String userPower = (String) pageContext.getSession().getAttribute(
"power");
try {
if (!power.equals(userPower)) {
pageContext.forward(gotoPage);
return super.EVAL_PAGE;
}
} catch (Exception e) {
e.printStackTrace();
}
return super.EVAL_PAGE;
// return super.SKIP_BODY ;
// return super.SKIP_PAGE;
}
public String getGotoPage() {
return gotoPage;
}
public void setGotoPage(String gotoPage) {
this.gotoPage = gotoPage;
}
public String getPower() {
return power;
}
public void setPower(String power) {
this.power = power;
}
}
================manager.jsp==================================
<%@ page pageEncoding="GBK" contentType="text/html; charset=GBK" %>
<%@ taglib uri="/tags/struts-bean" prefix="bean" %>
<%@ taglib uri="/tags/struts-logic" prefix="logic" %>
<%@ taglib uri="mytag" prefix="mytag" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GBK"/>
<title>manager</title>
</head>
<body>
<center>manager</center>
<mytag:CheckPower power="manager" gotoPage="login.jsp"/>
<logic:present name="LoginBean" scope="request">
<bean:write name="LoginBean" property="passWord"/>
</logic:present>
</body>
</html>
================mytag.tld===================
<?xml version="1.0" encoding="Shift_JIS"?>
<taglib>
<tlibversion>1.2</tlibversion>
<jspversion>1.1</jspversion>
<shortname>mytag</shortname>
<tag>
<name>CheckPower</name>
<tagclass>com.mytld.CheckPower</tagclass>
<attribute>
<name>power</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>gotoPage</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
</taglib>
==================web.xml(片断)===========================
<taglib>
<taglib-uri>mytag</taglib-uri>
<taglib-location>/WEB-INF/mytag.tld</taglib-location>
</taglib>
思路:读取当前用户的权限(在登陆验证是设置的)
和标签在jsp页面取到的值进行比较,不匹配时跳转到指定页面
总结:标签类一般存放在web-inf/lib
xxxx.tld 配置 标签类的参数定义和指定类名
web-inf.xml 指定xxxx.tld的位置和定义名字
<mytag:CheckPower power="manager" gotoPage="login.jsp"/>
错误总结:写标签类的时候get/set方法漏了 ,报错。
写jsp页面时ctrl c ctrl v时 uri没改,页面不认。
下步目标:标签类进行验证时能将验证错误信息写入session或者request
然后能通过jsp页面读取到该错误信息
===================CheckPower.class(标签类程序)==================================
package com.mytld;
import java.io.IOException;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;
public class CheckPower extends TagSupport {
private String power;
private String gotoPage;
public int doEndTag() throws JspException {
String userPower = (String) pageContext.getSession().getAttribute(
"power");
try {
if (!power.equals(userPower)) {
pageContext.forward(gotoPage);
return super.EVAL_PAGE;
}
} catch (Exception e) {
e.printStackTrace();
}
return super.EVAL_PAGE;
// return super.SKIP_BODY ;
// return super.SKIP_PAGE;
}
public String getGotoPage() {
return gotoPage;
}
public void setGotoPage(String gotoPage) {
this.gotoPage = gotoPage;
}
public String getPower() {
return power;
}
public void setPower(String power) {
this.power = power;
}
}
================manager.jsp==================================
<%@ page pageEncoding="GBK" contentType="text/html; charset=GBK" %>
<%@ taglib uri="/tags/struts-bean" prefix="bean" %>
<%@ taglib uri="/tags/struts-logic" prefix="logic" %>
<%@ taglib uri="mytag" prefix="mytag" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GBK"/>
<title>manager</title>
</head>
<body>
<center>manager</center>
<mytag:CheckPower power="manager" gotoPage="login.jsp"/>
<logic:present name="LoginBean" scope="request">
<bean:write name="LoginBean" property="passWord"/>
</logic:present>
</body>
</html>
================mytag.tld===================
<?xml version="1.0" encoding="Shift_JIS"?>
<taglib>
<tlibversion>1.2</tlibversion>
<jspversion>1.1</jspversion>
<shortname>mytag</shortname>
<tag>
<name>CheckPower</name>
<tagclass>com.mytld.CheckPower</tagclass>
<attribute>
<name>power</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>gotoPage</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
</taglib>
==================web.xml(片断)===========================
<taglib>
<taglib-uri>mytag</taglib-uri>
<taglib-location>/WEB-INF/mytag.tld</taglib-location>
</taglib>