JSF 2.0 查看过期异常
我一直在使用 JSF 1.2 和此答案中描述的 ViewHandler : IceFaces Session Expiry 导致异常 它非常有用,因为当异常发生时,页面会自动重新生成,这对于公共页面很有用。问题是它与 JSF 2.0 不兼容。有人知道如何让它在 JSF 2.0 或替代品中工作吗?
编辑:
我找到了这个解决方案:无状态JSF ,但仍然想知道是否有任何方法可以通过 ViewHandler
来完成它,就像我在 JSF 1.2 中所做的那样。这是我的 JSF 2.0 当前代码:
public class AutoRegeneratorViewHandler extends GlobalResourcesViewHandler
{
public AutoRegeneratorViewHandler(ViewHandler viewHandler)
{
super(viewHandler);
}
@Override
public UIViewRoot restoreView(FacesContext p_oContext, String p_sViewID)
{
UIViewRoot oViewRoot = super.restoreView(p_oContext,p_sViewID);
try
{
if(oViewRoot == null)
{
initView(p_oContext);
oViewRoot = createView(p_oContext,p_sViewID);
p_oContext.setViewRoot(oViewRoot);
try
{
renderView(p_oContext,oViewRoot);
}
catch(IOException e)
{
e.printStackTrace();
}
System.out.println("Created : " + p_sViewID);
}
}
catch(Exception e)
{
e.printStackTrace();
}
return oViewRoot;
}
}
此代码消除了 ViewExpiredException
但当加载页面时,我似乎未记录。
测试用例:
- 打开网站
- 等待超过当前会话过期时间(来自
web.xml
) - 输入用户名/密码
- 点击登录按钮
- 页面重新加载,登录表单为空
- 重新加载页面
- 页面显示欢迎和登录表单未显示(预期行为)
I've been using the JSF 1.2 with the ViewHandler described in this answer : IceFaces Session Expiry causes an exception it was very useful because when the exception occurs the page is automatically regenerated, good for public pages. The problem is that it is not compatible with JSF 2.0. Does anybody have an idea how to make it work in JSF 2.0 or a replacement?
Edit :
I've found this solution : Stateless JSF, but still wondering if there is any way to do it by a ViewHandler
like I was doing in JSF 1.2. Here is my JSF 2.0 current code :
public class AutoRegeneratorViewHandler extends GlobalResourcesViewHandler
{
public AutoRegeneratorViewHandler(ViewHandler viewHandler)
{
super(viewHandler);
}
@Override
public UIViewRoot restoreView(FacesContext p_oContext, String p_sViewID)
{
UIViewRoot oViewRoot = super.restoreView(p_oContext,p_sViewID);
try
{
if(oViewRoot == null)
{
initView(p_oContext);
oViewRoot = createView(p_oContext,p_sViewID);
p_oContext.setViewRoot(oViewRoot);
try
{
renderView(p_oContext,oViewRoot);
}
catch(IOException e)
{
e.printStackTrace();
}
System.out.println("Created : " + p_sViewID);
}
}
catch(Exception e)
{
e.printStackTrace();
}
return oViewRoot;
}
}
This code get rid of the ViewExpiredException
but when the page is loaded, I'm appearing not logged.
Test case :
- Open the website
- Wait more than the current session expiration time (from
web.xml
) - Enter username/password
- Hit Login button
- Page reload with the login form empty
- Reload the page
- Page show Welcome and login form is not shown (expected behavior)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为 JSF2 的方式是提供您自己的异常处理程序。
在异常处理程序中,可以循环所有未处理的异常,检查 ViewExpiredException 并将其删除。
甚至可以填充请求参数并导航到特定的 Facelet,该 Facelet 可以呈现正确且信息丰富的页面,该页面利用从异常处理程序填充的请求参数。如果需要,甚至可以导航到登录屏幕。
这是一篇这样的文章< /a> 描述如何实现它。
I think the JSF2 way is to provide your own exception handler.
In the exception handler, one can loop all unhandled exceptions, check for the ViewExpiredException, and remove it.
One an even populate the request params and navigate to a specific facelet, which can render the correct and informative page that makes use of those request params that are populated from the exception handler. Can even navigate to the login screen if you want.
Here's a such article describing how to implement it.