JSF导航问题

时间:2011-02-27 22:05:01

标签: java jsf richfaces

目前我正在更新一个jsf项目并且当一个jsf页面获得请求并且页面返回到客户端时我意识到项目的一个奇怪的事情我在浏览器中完全看到页面之后对同一页面的新请求到达尽管我没有点击我正在使用导航处理程序进行导航。我在我的项目中使用jsf(myfaces),richfaces。

我在这些课程上设置了两个断点,我看到大多数页面,而不是全部,发送请求和请求通过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);
            }
        }
    }

}

2 个答案:

答案 0 :(得分:0)

根据评论中的对话,我建议您查看JSF生命周期的详细信息。请注意,任何reRenderajaxRendred="true"都可能会启动生命周期。这个网站过去对我很有帮助:http://www.ibm.com/developerworks/library/j-jsf2/

答案 1 :(得分:0)

Richfaces组件向服务器发送请求,我将它们跟踪到a4j servlet