Note that there are some explanatory texts on larger screens.

plurals
  1. POCan I redirect to a valid Wicket page when attempting to access a non-existent page?
    text
    copied!<p><strong>Introduction</strong></p> <p>If a user of my Apache Wicket web application (which runs on GAE/J) attempts to visit a non-existent page, for example:</p> <pre><code>http://[MyURL]/admin/PageSubscribeX </code></pre> <p>the web framework logs the following warning:</p> <pre><code>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) </code></pre> <p>and GAE/J responds with an error 404 page and the text <code>Error: NOT_FOUND</code>.</p> <p>I know that currently with GAE/J I "cannot customize the 404 response page when no servlet mapping is defined for a URL".</p> <p><strong>My question</strong></p> <p>Is there any way I can specify a Wicket page for the response when no servlet mapping is defined for a URL? Alternatively, is there some servlet mapping I can define to map "all URLs not found" to a Wicket page of my choice?</p> <p>My software environment is:</p> <ul> <li>Wicket: 6.2.0</li> <li>GAE/J: 1.7.3</li> <li>Java: 1.6.0_37.</li> </ul> <p><strong>A failed attempt</strong></p> <p>Following the comments of @biziclop, I have tried the following, but this failed. All I got was my error page PageError showing every time....</p> <p>My code in my <code>WebApplication#init()</code> was:</p> <pre><code>ICompoundRequestMapper crmRoot = getRootRequestMapperAsCompound(); URLNotFoundMapper mapperURLNotFound = new URLNotFoundMapper(null, PageError.class); crmRoot.add(mapperURLNotFound); setRootRequestMapper(crmRoot); </code></pre> <p>My new mapper <code>URLNotFoundMapper</code> was</p> <pre><code>/** * 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. * &lt;br/&gt; * This mapper will cause a defined (error) page to be shown. */ public class URLNotFoundMapper extends BookmarkableMapper { private IRequestMapper m_rmRoot = null; private Class&lt;? extends IRequestablePage&gt; m_classPage = null; /** * Constructor. * @param rmRoot * The application's previous root request mapper. * If this is &lt;code&gt;null&lt;/code&gt; then it is ignored. * @param clError * The class of the page which should handle erroneous requests. * This must not be &lt;code&gt;null&lt;/code&gt;. */ public URLNotFoundMapper(IRequestMapper rmRoot, final Class&lt;? extends IRequestablePage&gt; 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 &lt;code&gt;IRequestHandler&lt;/code&gt; for the bookmarkable * error page. * @param request * @return * An &lt;code&gt;IRequestHandler&lt;/code&gt; 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; } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload