2011-02-27 99 views
0

当前我正在更新一个jsf项目,并实现了一个关于项目的奇怪的事情,当一个jsf页面获取请求和页面返回给客户端时,我在浏览器中完全看到页面之后,对同一页面的新请求到达,尽管事实我没有点击任何我正在使用导航处理navigation.I在我的项目中使用jsf(myfaces),richfaces。JSF导航问题

我在这些类上设置了两个断点,并且我看到大部分页面,并非全部,发送的请求和请求经过menufilter - > myfacesservletwrapper(在此时浏览器完全显示页面)之后,此菜单过滤器中断了另一个请求同一页。

package com.endersys.itap.ui; 

import java.io.IOException; 

import javax.servlet.Filter; 
import javax.servlet.FilterChain; 
import javax.servlet.FilterConfig; 
import javax.servlet.ServletException; 
import javax.servlet.ServletRequest; 
import javax.servlet.ServletResponse; 
import javax.servlet.http.HttpServletRequest; 
import javax.servlet.http.HttpServletResponse; 

import com.endersys.itap.ui.module.user.User; 
import com.endersys.itap.ui.module.user.UserManager; 
import java.io.FileInputStream; 
import java.util.Properties; 
import java.util.logging.Level; 
import java.util.logging.Logger; 


public class MenuFilter implements Filter { 

    private String ALLOWED = "login.xhtml"; 
    private String ALLOWED_FOLDER = "a4j"; 


    private static boolean searchEnabled = false; 
    private static boolean syslogServiceEnabled = false; 
    private static String SYSLOG_PAGE= "syslogsettings.xhtml"; 
    private Properties conf; 
    private String SEARCH_PAGE= "search.xhtml"; 

    private static final String BASE_PATH = "/opt/itap/logmonitor/"; 
    private static final String CONF_PATH = BASE_PATH + "etc/logmonitor.properties"; 

    private static Logger logger = Logger.getLogger(MenuFilter.class.getName()); 

    public void destroy() { 
    } 

    public void init(FilterConfig arg0) throws ServletException { 
     if(loadConfiguration()) 
     { 
      if(conf.getProperty("search_enabled").equalsIgnoreCase("true")) 
      { 
       searchEnabled = true; 
      } 

      try 
      { 
       if(conf.getProperty("syslog_enabled").equalsIgnoreCase("true")) 
       { 
        syslogServiceEnabled = true; 
       } 
      }catch(Exception exc) 
      { 
       exc.printStackTrace(); 
      } 
     } 
    } 

    private boolean loadConfiguration() 
    { 
     conf = new Properties(); 
     FileInputStream fis = null; 
     try { 
      fis = new FileInputStream(CONF_PATH); 
      conf.load(fis); 
     } catch (Exception e) { 
      logger.log(Level.SEVERE, e.getMessage(), e); 
      return false; 
     } finally { 
      try { 
       if (fis != null) { 
        fis.close(); 
       } 
      } catch (IOException e) { 
       logger.log(Level.SEVERE, e.getMessage(), e); 
      } 
     } 
     return true; 
    } 

    /** 
    * TODO Unit test this function extensively. 
    */ 
    public void doFilter(ServletRequest req, ServletResponse res, 
      FilterChain chain) throws IOException, ServletException { 
     try 
     { 
      UserManager userManager = (UserManager) ((HttpServletRequest) req) 
        .getSession(true).getAttribute("userManager"); 

      // http://localhost:8080/itapgui-v2*/index.xhtml* 
      String relativePath = ((HttpServletRequest) req).getServletPath(); 
      // Servlet path has a leading "/" but our menu items do not. 
      relativePath = relativePath.substring(1); 

      // http://localhost:8080*/itapgui-v2*/index.xhtml 
      String contextPath = ((HttpServletRequest) req).getContextPath(); 

      if(!searchEnabled && relativePath.endsWith(SEARCH_PAGE)) 
      { 
       ((HttpServletResponse) res).sendRedirect(contextPath 
          + "/index.xhtml"); 
       return; 
      } 

      if(!syslogServiceEnabled && relativePath.endsWith(SYSLOG_PAGE)) 
      { 
       ((HttpServletResponse) res).sendRedirect(contextPath 
          + "/index.xhtml"); 
       return; 
      } 

      if (!relativePath.endsWith(ALLOWED) 
        && !relativePath.startsWith(ALLOWED_FOLDER)) { 
       // Permission required. 
       // if (relativePath.endsWith("logout.xhtml")) { 
       // ((HttpServletRequest) req).getSession(true).invalidate(); 
       // ((HttpServletResponse) res).sendRedirect(contextPath 
       // + "/login.xhtml"); 
       // return; // Required. 
       // } 

       if (userManager == null) { 
        // Not authorized. 
        if(relativePath != null && relativePath.endsWith("index.xhtml")) 
        { 
         ((HttpServletResponse) res).sendRedirect(contextPath 
          + "/login.xhtml"); 
        }else 
        { 
        ((HttpServletResponse) res).sendRedirect(contextPath 
          + "/login.xhtml?session=expired"); 
        } 
        return; // Required. 
       } 
       User user = userManager.getUser(); 
       if (user.getId() == null) { 
        // Not authorized. 
        ((HttpServletResponse) res).sendRedirect(contextPath 
          + "/login.xhtml"); 
        return; // Required. 
       } else if (user.getId() != 1) { 
        Menu menu = (Menu) ((HttpServletRequest) req).getSession(true) 
          .getAttribute("menu"); 
        MenuItem item = menu.getItemByPath(relativePath); 
        if(item != null) 
        { 
         if (!userManager.access(item.getPerms())) { 
          ((HttpServletResponse) res).sendRedirect(contextPath 
            + "/error.xhtml"); 
          return; // Required. 
         } 
        } 
       } 
      } 
      chain.doFilter((HttpServletRequest) req, (HttpServletResponse) res); 

     }catch(Exception exc) 
     { 
      exc.printStackTrace(); 

      if(exc instanceof IOException) 
      { 
       throw (IOException) exc; 
      } 
      else if(exc instanceof ServletException) 
      { 
       throw (ServletException) exc; 
      } 

     } 
    } 



} 





public class MyFacesServletWrapper extends MyFacesServlet { 

    private static final String CONN_ERROR_URI = "/dberror.xhtml"; 
    private static final String OTHER_ERROR_URI = "/errors.xhtml"; 

    @Override 
    public void service(ServletRequest request, ServletResponse response) throws IOException, ServletException { 
     try { 
      super.service(request, response); 

     } catch (ServletException e) { 
      HttpServletRequest req = (HttpServletRequest) request; 
      HttpServletResponse res = (HttpServletResponse) response; 

      //if an database exception has occured 
      if (ExceptionUtils.indexOfType(e, javax.persistence.PersistenceException.class) != -1) { 
       res.sendRedirect(req.getContextPath() + CONN_ERROR_URI); 
      } 
      else { 
       // add the exception to the session scope attribute 
       // to show stack trace 
       req.getSession().setAttribute("exception", e); 
       res.sendRedirect(req.getContextPath() + OTHER_ERROR_URI); 
      } 
     } 
    } 

} 
+0

我还没有解决的问题,但我认为它从RichFaces的结果。但我是不确定,任何人都对richfaces有深入的了解,而ajax可以提供很大的帮助。 – ayengin 2011-03-11 22:51:21

+0

这很难说。 Richfaces可能提出两个请求的原因有很多。我猜想有多个来自客户端的请求,它不是您的servlet或过滤器中的问题。虽然我可能会误解你的英语。 – Adam 2011-03-21 21:27:55

+0

是否有可能两个发送了两个请求,当我点击某个源动作时。例如,如果有一个页面在另一个表格内有一个表格 – ayengin 2011-03-24 19:45:32

回答

0

RichFaces组件发送请求到服务器,我跟踪他们A4J的servlet