尝试访问不存在的页面时,我可以重定向到有效的Wicket页面吗?

时间:2012-11-20 16:25:38

标签: java google-app-engine wicket http-status-code-404

简介

如果我的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"如果没有为URL定义servlet映射,则无法自定义404响应页面。

我的问题

在没有为URL定义servlet映射时,有什么方法可以为响应指定Wicket页面吗?或者,是否有一些我可以定义的servlet映射来映射"所有未找到的URL"到我选择的Wicket页面?

我的软件环境是:

  • 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;
  }
}

1 个答案:

答案 0 :(得分:2)

您需要在网络应用中配置404 http错误,看看here

来自上述链接的提问者摘录是:

<error-page>
  <error-code>404</error-code>
  <location>/[URI portion to point to a customer error page]</location>
</error-page>
相关问题