利用公共的Action实现用户合法性的校验
在这里,我们可以通过实现一个公共的Action并增加相应的权限验证功能来实现用户权限的校验工作.
这样,在进行业务功能开发的时候,所有需要执行权限校验的Action都需要继承自此公共的Action.
下面是一个简单的例子
具有校验功能的Action(SecureAction.java)
/*
* Generated by MyEclipse Struts
* Template path: templates/java/JavaClass.vtl
*/
package dgut.ke.struts.secure;
import javax.servlet.RequestDispatcher;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
/**
* MyEclipse Struts
* Creation date: 08-28-2007
*
* XDoclet definition:
* @struts.action validate="true"
*/
public abstract class SecureAction extends Action {
/*
* Generated Methods
*/
/**
* Method execute
* @param mapping
* @param form
* @param request
* @param response
* @return ActionForward
*/
public final ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)throws Exception {
HttpSession session = request.getSession();
String userId = (String)session.getAttribute("SESSION.USER");
if(userId==null)
return (mapping.findForward("isNullSession"));
else
return doExecute(mapping,form,request,response);
}
public abstract ActionForward doExecute(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws Exception;
}
下面编写一个普通的Action(isNullSessionAction.java)继承自此公共Action
/*
* Generated by MyEclipse Struts
* Template path: templates/java/JavaClass.vtl
*/
package dgut.ke.struts.secure;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
public class IsNullSessionAction extends SecureAction {
@Override
/*重写类中的方法*/
public ActionForward doExecute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
//通过父类的权限验证则该方法会被执行
return mapping.findForward("success");
}
}
struts-config.xml中的相关配置
<action path="/isNullSession" type="dgut.ke.struts.secure.IsNullSessionAction">
<forward name="success" path="/success.jsp"></forward>
<forward name="isNullSession" path="/index.jsp"></forward>
</action>
<action path="/secureAction" type="dgut.ke.struts.secure.SecureActionAction"/>
posted on 2007-08-28 20:23
Ke 阅读(420)
评论(0) 编辑 收藏 所属分类:
struts