当未找到 FacesContext 会话属性时如何重定向到 JSF 中的错误页面
我正在使用 JSF 和 Apache myFaces 1.2.9 开发移动 Web 应用程序。
在身份验证期间,我调用一个 servlet,它将执行身份验证,然后在会话中设置一些属性。
在每个托管 bean 构造函数中,我都会检查此会话属性,并相应地设置一个布尔值。我将该布尔值设置为 tr:document 的呈现属性的值。类似这样的内容
<tr:document title="someid" rendered="#{controller.render}">
//SOME PAGE CONTENTS
</tr:document>
基于会话属性,页面可能会渲染也可能不会。我想更优雅地处理这个问题。
我通过 这个问题在SO中,它告诉我们过滤器的使用。我使用了过滤器,如下所示这里< /a>.
我将 webFilter urlPattern 更改为 /faces/jsp/*
因为我想检查每个 jsp 的会话属性。
在 doFilter() 方法中,我正在检查这样的会话属性 如果它是 null
我将重定向到无效的用户页面。
System.out.println("In doFilter");
HttpServletRequest request = (HttpServletRequest) req;
HttpServletResponse response = (HttpServletResponse) res;
HttpSession session = request.getSession(false);
if (session.getAttribute("myattribute") == null) {
response.sendRedirect(properties.getProperty(INVALID_USER_REDIRECTURL, true)); // No attribute found, so redirect to Invalid user page.
} else {
chain.doFilter(req, res); // attribute found, so just continue request.
}
但我没有看到我的过滤器被触发,因为我没有看到我在过滤器 init(),doFilter()
方法中保留的任何调试语句。
有什么帮助吗?
编辑:我刚刚在我的 web.xml 中完成了这个过滤器映射
<filter>
<filter-name>trinidad</filter-name>
<filter-class>org.apache.myfaces.trinidad.webapp.TrinidadFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>trinidad</filter-name>
<servlet-name>faces</servlet-name>
</filter-mapping>
,我想 myfaces 有一些过滤器。我可以再拥有一个吗?
I am developing a mobile webapp using JSF and Apache myFaces 1.2.9.
During authentication i am calling a servlet which will do the authentication and then set some attribute in the session.
And in every managed bean constructor i am checking for this session attribute and accordingly i am setting a boolean value.That boolean value i am setting as a value for rendered attribute of tr:document.Something like this
<tr:document title="someid" rendered="#{controller.render}">
//SOME PAGE CONTENTS
</tr:document>
Based on the session attribute,the page may render or not.I want to handle this issue more gracefully.
I came through this question in SO which tells the usage of filters.I used the filter as shown here.
I changed the webFilter urlPattern to /faces/jsp/*
as i want to check the session attribute for each jsp.
In doFilter() method i am checking for session attribute like this
and if it is null
i am redirecting to invalid user page.
System.out.println("In doFilter");
HttpServletRequest request = (HttpServletRequest) req;
HttpServletResponse response = (HttpServletResponse) res;
HttpSession session = request.getSession(false);
if (session.getAttribute("myattribute") == null) {
response.sendRedirect(properties.getProperty(INVALID_USER_REDIRECTURL, true)); // No attribute found, so redirect to Invalid user page.
} else {
chain.doFilter(req, res); // attribute found, so just continue request.
}
But i don't see my filter getting fired as i don't see any debug statements which i kept in my filter init(),doFilter()
methods.
Any help?
EDIT:I just came through this filter mapping in my web.xml
<filter>
<filter-name>trinidad</filter-name>
<filter-class>org.apache.myfaces.trinidad.webapp.TrinidadFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>trinidad</filter-name>
<servlet-name>faces</servlet-name>
</filter-mapping>
I suppose myfaces has some filter.Can i have another one of mine?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将过滤器映射到 FacesServlet 所映射的 URL 模式。
Map the filter to URL pattern where your FacesServlet is mapped.