这是我写的一个判断是否事管理员的taglib类:package com.robusoft.wantong.tag;import java.io.IOException;
import javax.servlet.http.HttpSession;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.TagSupport;
import org.apache.struts.action.Action;
import org.apache.struts.util.MessageResources;
import com.robusoft.wantong.*;
import com.robusoft.wantong.database.*;public final class CheckAdminTag extends TagSupport {
// --------------------------------------------------- Instance Variables
/**
* The key of the session-scope bean we look for.
*/
private String name = "user";
/**
* The page to which we should forward for the user to log on.
*/
private String page = "/error.jsp";
// ----------------------------------------------------------- Properties
/**
* Return the bean name.
*/
public String getName() { return (this.name); }
/**
* Set the bean name.
*
* @param name The new bean name
*/
public void setName(String name) { this.name = name; }
/**
* Return the forward page.
*/
public String getPage() { return (this.page); }
/**
* Set the forward page.
*
* @param page The new forward page
*/
public void setPage(String page) { this.page = page; }
// ------------------------------------------------------- Public Methods
/**
* Defer our checking until the end of this tag is encountered.
*
* @exception JspException if a JSP exception has occurred
*/
public int doStartTag() throws JspException { return (SKIP_BODY); }
/**
* Perform our logged-in user check by looking for the existence of
* a session scope bean under the specified name. If this bean is not
* present, control is forwarded to the specified logon page.
*
* @exception JspException if a JSP exception has occurred
*/
public int doEndTag() throws JspException { // Is there a valid user logged on?
boolean valid = false;
HttpSession session = pageContext.getSession();
if ((session != null) && (session.getAttribute(name) != null)){
User _user = (User)session.getAttribute(name);
valid = _user.isAdmin();
} // Forward control based on the results
if (valid)
return (EVAL_PAGE);
else {
try {
pageContext.forward(page);
} catch (Exception e) {
throw new JspException(e.toString());
}
return (SKIP_PAGE);
} }
/**
* Release any acquired resources.
*/
public void release() { super.release();
this.name = "user";
this.page = "/error.jsp"; }
}
import javax.servlet.http.HttpSession;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.TagSupport;
import org.apache.struts.action.Action;
import org.apache.struts.util.MessageResources;
import com.robusoft.wantong.*;
import com.robusoft.wantong.database.*;public final class CheckAdminTag extends TagSupport {
// --------------------------------------------------- Instance Variables
/**
* The key of the session-scope bean we look for.
*/
private String name = "user";
/**
* The page to which we should forward for the user to log on.
*/
private String page = "/error.jsp";
// ----------------------------------------------------------- Properties
/**
* Return the bean name.
*/
public String getName() { return (this.name); }
/**
* Set the bean name.
*
* @param name The new bean name
*/
public void setName(String name) { this.name = name; }
/**
* Return the forward page.
*/
public String getPage() { return (this.page); }
/**
* Set the forward page.
*
* @param page The new forward page
*/
public void setPage(String page) { this.page = page; }
// ------------------------------------------------------- Public Methods
/**
* Defer our checking until the end of this tag is encountered.
*
* @exception JspException if a JSP exception has occurred
*/
public int doStartTag() throws JspException { return (SKIP_BODY); }
/**
* Perform our logged-in user check by looking for the existence of
* a session scope bean under the specified name. If this bean is not
* present, control is forwarded to the specified logon page.
*
* @exception JspException if a JSP exception has occurred
*/
public int doEndTag() throws JspException { // Is there a valid user logged on?
boolean valid = false;
HttpSession session = pageContext.getSession();
if ((session != null) && (session.getAttribute(name) != null)){
User _user = (User)session.getAttribute(name);
valid = _user.isAdmin();
} // Forward control based on the results
if (valid)
return (EVAL_PAGE);
else {
try {
pageContext.forward(page);
} catch (Exception e) {
throw new JspException(e.toString());
}
return (SKIP_PAGE);
} }
/**
* Release any acquired resources.
*/
public void release() { super.release();
this.name = "user";
this.page = "/error.jsp"; }
}
解决方案 »
- 问下关于jquery赋值问题
- struts2自定义struts.xml的位置,结果找不到action
- [求助]Struts2+Spring2.5+Hibernate3出现的问题
- 如何增加在删除记录时弹出对话框提示是否确认删除此信息的代码
- struts 跳转问题
- jsp 项目安装求助和外包
- 关于JDOM的应用
- 菜鸟问题,javax.servlet.ServletException: No data found?怎么回事?
- 我在JB中编写Servlet,为什么在第一次请求时,其会再调用init()?
- jsp论坛,大家快去看看,解决树形目录http://52jsp.126.com
- Tomcat4.1.12+Apache2.0.43的配置
- 如何在JSP中做树型目录,数据来自数据库?
如果不是管理员就跳到error.jsp页面了。