Struts拦截器
struts拦截器只能拦截action,但如果我直接访问的是页面,那么这个拦截器是不会进行拦截的,请问一般用什么方法来解决
------解决方案--------------------那你只有写filter了,struts没法
------解决方案--------------------页面一般都放在web-inf下,tomcat保护,这样你就直接访问不到了。
------解决方案--------------------在sturts2中,先编写一个过滤器,然后在web.xml 文件中配置如下:
<filter>
<filter-name>login(可任意命名)</filter-name>
<filter-class>com.LoginNotFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>login</filter-name>
<url-patten>/ht/* (jsp路劲,表示/WebRoot/ht/目录下的所有jsp)</url-patten>
</filter-mapping>
过滤器代码如下:
package com;
import
java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.FilterConfig;
import javax.servlet.FilterChain;
import
javax.servlet.ServletException;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpServletRequest;
public class LoginNotFilter implements Filter
{
public void init(FilterConfig fc){}
@SuppressWarnings("unchecked")
public void doFilter(ServletRequest request,ServletResponse response,FilterChain chain)throws
IOException,
ServletException {
HttpServletRequest req = (HttpServletRequest)request;
String path = req.getContextPath();
HttpSession session = req.getSession();
Object users = session.getAttribute("userLogin");
if(users!=null){
chain.doFilter(request,response);
}else{
session.setAttribute("result","您没有登录或登陆超时");
response.getWriter().println("<script>location.href='"+ path +"/login.jsp';</script>");
}
}
public void destroy(){}
}
以上代码适用于用户登录才能进入后台的jsp。