2012-11-20 16 views
2

介紹嘗試訪問不存在的頁面時,我可以重定向到有效的Wicket頁面嗎?

如果我的Apache Wicket的Web應用程序(運行在GAE/J)的用戶試圖訪問一個不存在的頁面,例如:

http://[MyURL]/admin/PageSubscribeX 

Web框架將記錄以下警告:

org.apache.wicket.core.util.lang.WicketObjects resolveClass: Could not resolve class [[...].admin.PageSubscribeX] 
java.lang.ClassNotFoundException: [...].admin.PageSubscribeX 
    at com.google.appengine.runtime.Request.process-78915baf06af5f31(Request.java) 
    at java.lang.Class.forName(Class.java:133) 
    at org.apache.wicket.application.AbstractClassResolver.resolveClass(AbstractClassResolver.java:108) 
    at org.apache.wicket.core.util.lang.WicketObjects.resolveClass(WicketObjects.java:71) 
    at org.apache.wicket.core.request.mapper.AbstractComponentMapper.getPageClass(AbstractComponentMapper.java:139) 
    at org.apache.wicket.core.request.mapper.PackageMapper.parseRequest(PackageMapper.java:148) 
    ... 
    at com.google.tracing.TraceContext$TraceContextRunnable.run(TraceContext.java:458) 
    at java.lang.Thread.run(Thread.java:679) 

和GAE/J有錯誤的404頁和文本Error: NOT_FOUND響應。

我知道,目前與GAE/J I「不能在沒有servlet映射爲一個URL定義自定義404響應頁面」。

我的問題

有什麼辦法在沒有servlet映射爲一個URL定義我可以指定響應檢票頁?或者,是否有一些servlet映射可以定義,將「找不到的所有URL」映射到我選擇的Wicket頁面?

我的軟件環境是:

  • 檢票:6.2.0
  • GAE/J:1.7.3
  • 的Java:1.6.0_37。

一個失敗的嘗試

繼@biziclop的意見,我曾嘗試以下,但失敗了。我得到的是我的錯誤頁面PageError顯示每次....

我在我的WebApplication#init()代碼爲:

ICompoundRequestMapper crmRoot = getRootRequestMapperAsCompound(); 
URLNotFoundMapper mapperURLNotFound = new URLNotFoundMapper(null, 
PageError.class); 
crmRoot.add(mapperURLNotFound); 
setRootRequestMapper(crmRoot); 

我的新映射URLNotFoundMapper

/** 
* This mapper class is intended to be the mapper of last resort, to be used 
* if all other mappers cannot handle the URL of the current request. 
* <br/> 
* This mapper will cause a defined (error) page to be shown. 
*/ 
public class URLNotFoundMapper extends BookmarkableMapper 
{ 
    private IRequestMapper m_rmRoot = null; 
    private Class<? extends IRequestablePage> m_classPage = null; 

    /** 
    * Constructor. 
    * @param rmRoot 
    * The application's previous root request mapper. 
    * If this is <code>null</code> then it is ignored. 
    * @param clError 
    * The class of the page which should handle erroneous requests. 
    * This must not be <code>null</code>. 
    */ 
    public URLNotFoundMapper(IRequestMapper rmRoot, 
    final Class<? extends IRequestablePage> clError) 
    { 
    m_rmRoot = rmRoot; 
    m_classPage = clError; 
    } 

    /** 
    * Use this mapper as the last option. 
    * So let all other mappers try to handle the request first. 
    * @param request 
    * The request. 
    * @return 
    * The score of the application's previous root request mapper. 
    */ 
    @Override 
    public int getCompatibilityScore(Request request) 
    { 
    return Integer.MIN_VALUE; 
    } 

    /** 
    * This method returns an <code>IRequestHandler</code> for the bookmarkable 
    * error page. 
    * @param request 
    * @return 
    * An <code>IRequestHandler</code> capable of processing a bookmarkable 
    * request. 
    * 
    */ 
    @Override 
    public IRequestHandler mapRequest(Request request) 
    { 
    IRequestHandler rhResult = null; 
    if (m_rmRoot != null) 
     rhResult = m_rmRoot.mapRequest(request); 

    if (rhResult != null) 
     rhResult = null; // Another mapper can handle this 
    else 
    { 
     rhResult = processBookmarkable(m_classPage, null); 
    } 

    return rhResult; 
    } 

    @Override 
    public Url mapHandler(IRequestHandler requestHandler) 
    { 
    Url urlResult = null; 
    if (m_rmRoot != null) 
     urlResult = m_rmRoot.mapHandler(requestHandler); 

    if (urlResult != null) 
     urlResult = null; // Another mapper can handle this 
    else 
    { 
     PageInfo info = new PageInfo(); 
     UrlInfo urlInfo = new UrlInfo(new PageComponentInfo(info, null), 
     m_classPage, null); 
     urlResult = buildUrl(urlInfo); 
    } 

    return urlResult; 
    } 

    /** 
    * @return 
    * The URL info for the bookmarkable error page. 
    */ 
    @Override 
    protected UrlInfo parseRequest(Request request) 
    { 
    UrlInfo uiResult = null; 
    uiResult = new UrlInfo(null, m_classPage, null); 
    return uiResult; 
    } 
} 
+0

它當然應該是可能的,並且看着堆棧跟蹤,我認爲最好在mapper級別處理這個問題。 – biziclop

+0

@biziclop您的意思是修改我的web.xml文件以包含「錯誤頁面」元素或其他內容? –

+0

我的意思是編程,因爲它已經完成[這裏](https://cwiki.apache.org/WICKET/request-mapping.html)。在最後的代碼片段中,他們向您展示瞭如何用自定義實現替換根請求映射器。從那裏你可以完全控制你要加載的頁面類和時間。 – biziclop

回答

2

您需要配置在你的webapp 404 HTTP錯誤,看看here

從上面的鏈接提問者的提取物爲:

<error-page> 
    <error-code>404</error-code> 
    <location>/[URI portion to point to a customer error page]</location> 
</error-page> 
+0

可悲的是,谷歌應用程序引擎的Java(GAE/J)不使用錯誤代碼404 web.xml文件「的錯誤頁面」元素讓這個解決方案(在實踐中,這一配置被忽略。)你能或其他任何人想想另一種解決方案? –

+0

O.o,你可以覆蓋wicket的錯誤頁面,但這只是爲服務器internel錯誤(運行時異常),爲404 http錯誤工作據我所知總是服務器配置 – osdamv

相關問題