`
jiangzhen1984
  • 浏览: 98879 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

解决StaleLinkException,StaleSessionException问题跳转到自定义错误页

    博客分类:
  • java
阅读更多
今天抽出了点时间来查看T4的源代码,就为了解决StaleLinkException StaleSessionException 这个问题,这个问题T4回返回一些英文的提示信息这对使用系统的人来说是很不友好的提示。

查看T4的源代码发现 处理这个exception的代码在 AbstractEngine 这个类里面代码如下

 public void service(WebRequest request, WebResponse response) throws IOException
    {
        IRequestCycle cycle = null;
        IMonitor monitor = null;
        IEngineService service = null;

        if (_infrastructure == null){
            _infrastructure = (Infrastructure) request.getAttribute(Constants.INFRASTRUCTURE_KEY);
        }

        // Create the request cycle; if this fails, there's not much that can be done ... everything
        // else in Tapestry relies on the RequestCycle.

        try
        {
            cycle = _infrastructure.getRequestCycleFactory().newRequestCycle(this);
    
        }
        catch (RuntimeException ex)
        {
            throw ex;
        }
        catch (Exception ex)
        {
            throw new IOException(ex.getMessage());
        }

        try
        {
            try
            {
                monitor = cycle.getMonitor();               
                service = cycle.getService();              
                monitor.serviceBegin(service.getName(), _infrastructure.getRequest()
                        .getRequestURI());
              
                // Let the service handle the rest of the request.

                service.service(cycle);
               
                return;
            }
            catch (PageRedirectException ex)
            {
                handlePageRedirectException(cycle, ex);
            }
            catch (RedirectException ex)
            {
                handleRedirectException(cycle, ex);
            }
            catch (StaleLinkException ex)
            {
                handleStaleLinkException(cycle, ex);
            }
            catch (StaleSessionException ex)
            {
                handleStaleSessionException(cycle, ex);
            }
        }
        catch (Exception ex)
        {
            monitor.serviceException(ex);

            // Attempt to switch to the exception page. However, this may itself
            // fail for a number of reasons, in which case an ApplicationRuntimeException is
            // thrown.

            if (LOG.isDebugEnabled())
                LOG.debug("Uncaught exception", ex); 
            activateExceptionPage(cycle, ex);
        }
        finally
        {
            if (service != null){            	
                monitor.serviceEnd(service.getName());
            }

            try
            {
                cycle.cleanup();              
                _infrastructure.getApplicationStateManager().flush();
            }
            catch (Exception ex)
            {System.out.println("%%%-"+"AbstractEngine"+"_20");
                reportException(EngineMessages.exceptionDuringCleanup(ex), ex);
            }
        }
    }




可以看到 处理 exception 就是
   handleRedirectException(cycle, ex),                handleStaleLinkException(cycle, ex);
这两个方法
这两个方法如下:
 protected void handleStaleLinkException(IRequestCycle cycle, StaleLinkException exception)
            throws IOException
    {
        _infrastructure.getStaleLinkExceptionPresenter()
                .presentStaleLinkException(cycle, exception);
    }


 protected void handleStaleSessionException(IRequestCycle cycle, StaleSessionException exception)
            throws IOException
    {
        _infrastructure.getStaleSessionExceptionPresenter().presentStaleSessionException(
                cycle,
                exception);
    }


继续 跟踪方法的执行 :

public StaleSessionExceptionPresenter getStaleSessionExceptionPresenter()
    {
        return (StaleSessionExceptionPresenter) getProperty("staleSessionExceptionPresenter");
    }

 public StaleLinkExceptionPresenter getStaleLinkExceptionPresenter()
    {
        return (StaleLinkExceptionPresenter) getProperty("staleLinkExceptionPresenter");
    }


public class StaleSessionExceptionPresenterImpl implements StaleSessionExceptionPresenter
{
    private ResponseRenderer _responseRenderer;

    private String _pageName;

    public void presentStaleSessionException(IRequestCycle cycle, StaleSessionException cause)
            throws IOException
    {
        IPage exceptionPage = cycle.getPage(_pageName);

        cycle.activate(exceptionPage);

        _responseRenderer.renderResponse(cycle);
    }

    public void setPageName(String pageName)
    {
        _pageName = pageName;
    }

    public void setResponseRenderer(ResponseRenderer responseRenderer)
    {
        _responseRenderer = responseRenderer;
    }

}




public class StaleLinkExceptionPresenterImpl implements StaleLinkExceptionPresenter
{
    private ResponseRenderer _responseRenderer;

    private String _pageName;

    public void presentStaleLinkException(IRequestCycle cycle, StaleLinkException cause)
            throws IOException
    {
        IPage exceptionPage = cycle.getPage(_pageName);

        PropertyUtils.write(exceptionPage, "message", cause.getMessage());

        cycle.activate(exceptionPage);

        _responseRenderer.renderResponse(cycle);
    }

    public void setPageName(String pageName)
    {
        _pageName = pageName;
    }

    public void setResponseRenderer(ResponseRenderer responseRenderer)
    {
        _responseRenderer = responseRenderer;
    }

}




继续跟踪到上面的方法后可以发现_pageName 的名字:StaleLink,StaleSession
经过查看知道这个名字是从 Framework.library 文件里提取的,打开这个文件可以找到
下面的三句话
    <page name="StaleLink" specification-path="pages/StaleLink.page"/>
    <page name="StaleSession" specification-path="pages/StaleSession.page"/>
    <page name="Exception" specification-path="pages/Exception.page"/>
   

将上面的代码修改为
    <page name="StaleLink" specification-path="pub/ErrorPage"/>
    <page name="StaleSession" specification-path="pub/ErrorPage"/>
    <page name="Exception" specification-path="pages/Exception.page"/>
   

搞定,这里说明一下exception 可以通过下面的代码配置,但用同样的配置思想配置其他
两个exception就不行,无奈之下只好修改他的源文件
<contribution configuration-id="tapestry.InfrastructureOverrides">
        <property name="exceptionPageName" value="pub/ErrorPage"/>
    </contribution>



在网上搜索一下--发现有了更简便的方法来处理这个问题---
自你的application配置文件中加上下面的代码 T4会自动替换自己的基础组建
但注意timeout的类需要继承StaleLink类 

 <page name="StaleLink" specification-path="/pub/TimeOut.page"/>

  <page name="StaleSession" specification-path="/pub/TimeOut.page"/>



参考文档:
http://www.blogjava.net/tapestry/archive/2007/01/29/96568.aspx
分享到:
评论

相关推荐

    IIS配置:自定义错误页面自动跳转

    在自定义错误页面配置中,需要设置 `customErrors` 节点,通过 `mode` 属性设置错误模式为 `On`,并设置 `defaultRedirect` 属性将错误跳转到自定义的错误页面。例如: `...

    asp.net 异常处理跳转自定义错误页面Demo

    本示例是关于如何在ASP.NET中实现异常处理,特别是如何在出现错误时跳转到自定义的404(页面未找到)和500(服务器内部错误)错误页面。下面我们将详细探讨这一主题。 首先,我们需要理解ASP.NET中的异常处理机制。...

    显示自定义错误页面,用户自定义

    通过以上步骤,你可以成功地在JSP项目中实现自定义错误页面,提高用户体验并为用户提供更清晰的问题解决方案。在实际开发过程中,务必保持代码的可维护性和遵循最佳实践,以便在未来需要调整或扩展时能快速适应。

    自定义404错误页面

    自定义404错误页面是网站设计中一个重要的细节,它在用户访问不存在的网页时显示,起到用户体验优化和品牌建设的作用。404错误页面通常由服务器返回,表示请求的资源无法找到,可能是URL输入错误、页面已被移除或者...

    360安全卫士如何关闭自定义错误页面.docx

    在使用360安全卫士的过程中,用户可能会遇到一种情况,即当访问的网页出现错误,比如404找不到页面时,360安全卫士会自动跳转到360自家的错误提示页面,而不是浏览器默认的错误页面。对于一些用户来说,这可能不是...

    HTML输入密码自定义跳转页面源码

    如果错误,则可能显示错误消息或阻止跳转。 `style.css`文件则用于定义页面的视觉样式。它可以包括元素的颜色、字体、布局等,使界面看起来更专业且符合设计需求。例如,可能会定义输入框的边框、背景色、提示文本...

    17个经典漂亮的404自定义错误页面下载

    "17个经典漂亮的404自定义错误页面下载"这个主题聚焦于提升用户体验,通过设计吸引人的404页面来缓解用户遇到这种常见问题时的挫败感。 自定义404错误页面是网站设计的重要组成部分,因为它不仅能够传达品牌形象,...

    ThinkPHP中自定义错误页面和提示页面实例

    设置完成后,ThinkPHP将会在出现错误时跳转到我们指定的模板文件来显示错误信息。 接下来,本文提到了两个非常实用的函数:`error_404`和`halt`。这两个函数都可以用来在程序中手动触发错误跳转或提示。 - `error_...

    【ASP.NET编程知识】ASP.NET MVC下自定义错误页和展示错误页的方式.docx

    // 重定向到自定义错误页面 Response.Redirect("~/ErrorPage/GlobalError"); } ``` 5. **错误页面设计**: - 通常,自定义错误页面会放在一个特定的控制器(如`ErrorController`)和视图(如`Error.cshtml`或`...

    Android构建一个通用的WebView(二):自定义的错误页面、缓存数据,离线浏览

    在使用WebView加载网页时,可能会遇到网络错误、404找不到页面等问题。默认情况下,WebView会显示系统提供的错误页面,但这往往不符合我们应用程序的UI风格。为了提供更好的用户体验,我们可以自定义错误页面。首先...

    密码访问单页自定义跳转页面源码

    密码访问单页自定义跳转页面,修改了的密码访问单页,添加了js自定义密码跳转页面。需要正确输入密码才能跳转目标网址。

    jquery实现的支持自定义事件跳转到指定页面的404错误页面源码.zip

    这个“jquery实现的支持自定义事件跳转到指定页面的404错误页面源码.zip”文件显然包含了一个用jQuery编写的404错误处理解决方案,允许用户自定义错误发生时的跳转行为。现在,让我们深入探讨这个主题,了解更多关于...

    很好的404错误跳转页面

    因此,合理的404处理策略包括定期检查并修复死链,使用自定义404页面引导流量,以及设置重定向规则,将旧的、无效的URL重定向到相关的现有页面。 在实际操作中,可以通过各种工具和技术实现404错误页面的定制和监控...

    错误页面跳转解决办法

    本篇将深入探讨如何解决错误页面跳转的问题,以及与之相关的源码分析和工具应用。 首先,了解常见的HTTP状态码对于解决问题至关重要。404 Not Found是用户最常遇到的错误页面,表示服务器无法找到请求的资源。500 ...

    ASP.NET MVC下自定义错误页和展示错误页的方式

    开发者可以在Application_Error方法中编写逻辑来处理异常,比如记录异常信息,并根据异常类型重定向用户到自定义错误页面。 public void Application_Error(object sender, EventArgs e) { Exception exception = ...

    如何设置UCHome注册登陆退出的跳转页自定义

    1. **兼容性问题**:在进行自定义设置时,需要注意不同浏览器之间的兼容性问题,确保在主流浏览器中都能正常显示和跳转。 2. **安全性考虑**:为了保障系统的安全性,在设置跳转页面时,应避免使用可能存在安全隐患...

    PHP JS根据自定义来路域名跳转到指定页面源码

    标题提到的"PHP JS根据自定义来路域名跳转到指定页面源码"正是这种需求的一种解决方案。这个源码集可能是用于帮助开发者构建一个系统,它能够识别用户来自哪个网站,并据此决定将用户重定向到哪个内部页面。 首先,...

Global site tag (gtag.js) - Google Analytics