【问题标题】:Css contents are not applying in a page when Iam redirecting the page using requestdispatcher in filter当我在过滤器中使用 requestdispatcher 重定向页面时,Css 内容未应用于页面
【发布时间】:2012-09-04 07:52:56
【问题描述】:

在检查条件后,如果条件失败,然后使用请求调度程序将其重定向到其他页面。它正在重定向到该页面,但 css 未应用于该页面。为什么会这样?

public class RolePrivilegeFilter implements Filter {
List<String> privilege = null;
private static final String notAuthorizedPath = "/home/notAuthorized.jsf";
@Override
public void destroy() {
log.info("filter finish");
}

@Override
public void doFilter(ServletRequest sRequest, ServletResponse sResponse, FilterChain filterChain) throws IOException, ServletException {
String smUser = null;
 try {
    HttpServletRequest request = (HttpServletRequest) sRequest;
    HttpServletResponse response = (HttpServletResponse) sResponse;
    HttpSession session = null;
    try {
        request = (HttpServletRequest) sRequest;
        smUser =  request.getHeader(EnvironmentBean.SESSION_ATTR_SM_USER);
        } catch (Exception e) {
        // TODO: handle exception
        e.printStackTrace();
        }
    if (rolePrivilege == null || rolePrivilege.isEmpty()) {
    rolePrivilege = new RolePrivilegeBuilderImpl().rolePrivilege("Smtest2");
    privilege filter bean is\t"+rolePrivilege);
    log.debug("After rolePrivilege: " + rolePrivilege);
    if (rolePrivilege != null && rolePrivilege.size() > 0 ) {
    if (session == null) {
        session = request.getSession(true);
    }
    session.setAttribute("privilege", rolePrivilege);

    } else {
             //This is the page iam  redirecting to. 
    System.out.println("role privilege not authorized page");
    RequestDispatcher rd = request.getRequestDispatcher(notAuthorizedPath);
                    rd.forward(request, response);
                    return;
           }
    } catch (Exception e) {
        e.printStackTrace();
        log.debug("Error in setting session attribute: " + e.getMessage());
    }
    log.debug("doFilter() finish");
    filterChain.doFilter(sRequest, sResponse);
}

@Override
public void init(FilterConfig arg0) throws ServletException {
    log.info("filter start");

}
}

问题是 iam 重定向到该页面,但该页面的 css 没有被应用,就像没有显示背景图像一样。为什么会这样?

【问题讨论】:

    标签: html css jsf richfaces servlet-filters


    【解决方案1】:

    显然过滤器的 URL 模式确实匹配了浏览器对该 CSS 文件的请求,因此它也被过滤器阻止了。显然,您已将过滤器映射到过于宽泛的 URL 模式,例如 /* 或其他东西。

    您需要确保过滤器绕过所有这些 CSS 文件请求。如果无法相应地更改过滤器的 URL 模式以绕过 CSS 文件请求,例如将 /* 更改为 /app/* 以便它仅在所有受限资源所在的 /app 文件夹中的请求上运行,然后您需要重写过滤器的代码以对请求 URI 添加额外的检查。

    类似这样的:

    if (request.getRequestURI().endsWith(".css")) {
        chain.doFilter(request, response);
        return;
    }
    

    这将使所有*.css 请求都被传递而不是被阻止。

    【讨论】:

    • 感谢 balusC 但上面代码中的资源处理程序是什么。
    • JSF 标准来自javax.faces.application 包,RichFaces 来自org.richfaces.resource 包。只需让您的 IDE 自动完成导入即可。
    • IDE 未显示导入语句,它显示创建类资源处理程序。
    • 如果您真的使用 JSF 2.0 和 RichFaces 4.0,请再次验证。你的问题被标记为[jsf-2.0],所以我假设 JSF 2.0。这些类在 JSF 1.x 和 RichFaces 3.x 中不存在,需要使用不同的解决方案。但具体问题仍然归结为您的 CSS 文件也被该过滤器阻止,您需要重写过滤器以让它们通过。
    • 我完全不明白你的具体问题。您是否了解基本的 HTTP、网络服务器、网络浏览器的一般工作原理? JSF 在 webserver 中执行,HTML 在 webbrowser 中执行。 HTML 中的任何&lt;link&gt;(以及&lt;script&gt;&lt;img&gt;)引用都由网络浏览器依次通过完全独立的HTTP 请求从网络服务器下载,这些HTTP 请求又会调用/* 上的任何过滤器。您是否以任何方式理解并尝试过我的回答中提出的解决方案?
    猜你喜欢
    • 1970-01-01
    • 2019-08-18
    • 2010-11-10
    • 2012-05-10
    • 1970-01-01
    • 1970-01-01
    • 2017-07-08
    • 2010-12-04
    • 2013-10-17
    相关资源
    最近更新 更多