日期:2014-05-20  浏览次数:20787 次

struts中的过滤器怎么写?
我想设计一个过滤器,过滤某个DispachAction。然后根据登陆用户的权限属性(user.getGrade()),是一个INT类型,判断该用户有没有权限访问该DispachAction下的Parameter属性指定的方法。请尽量写详细些。配置文件和FILTER类都写一下。小弟在此谢谢啦!
  ps:我是新手,提的问题可能很SB。请大家手下留情!

------解决方案--------------------
过滤器本来就是一个 servlet和平常写一样只要在web.xml中写就行了 网上找个例子配上 把过滤的地址指定为你的action即可
exp:xxx.do
------解决方案--------------------
建一个baseAction就行啊,
public class BaseAction extends DispatchAction {

public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {

UserInfo user = (UserInfo)request.getSession().getAttribute("login");
if(user == null){
return mapping.findForward("login");
}else if(user.getGrade()){
//你的代码
}

return super.execute(mapping, form, request, response);
}

}
然后别的action在继承这个BaseAction,权限处理就在BaseAction里处理就行。