Request mappingPage edited by Thomas GötzChanges (21)
Full ContentHow it was in Wicket 1.4To mount a page in Wicket 1.4 the developer had to use org.apache.wicket.protocol.http.WebApplication's:
The new way in Wicket 1.5org.apache.wicket.request.target.coding.IRequestTargetUrlCodingStrategy interface and all its implementations are replaced with the org.apache.wicket.request.IRequestMapper and its respective implementations. To add a mapper into the list of mappers which Wicket will use to process a request use org.apache.wicket.Application.getRootRequestMapperAsCompound().add(mapperInstance). Sometimes you may want a specific IRequestMapper to be process all incoming requests. To do this you should use org.apache.wicket.Application.setRootRequestMapper(IRequestMapper). This mapper may manipulate the Request's Url and then pass it for further processing to the registered non-root mappers. For examples of this idea see the source code of org.apache.wicket.request.mapper.CryptoMapper and org.apache.wicket.protocol.https.HttpsMapper Default mapper implementationsHomePageMapperThis mapper is pre-configured by Wicket and there is no need to register it. It is used to create IRequestHandler for requests to the root ('/') of the application context. BookmarkableMapperThis mapper decodes and encodes bookmarkable URLs like:
This mapper is also pre-configured and there is no need to register it. To change 'wicket' and 'bookmarkable' segments in the Url to something else see org.apache.wicket.request.mapper.IMapperContext.getNamespace() (the default implementation can be replaced with org.apache.wicket.Application.newMapperContext()). MountedMapperThis mapper is similar to BookmarkableMapper but the difference is that the user application defines the mount point where this mapper matches.
Usage: MyApp.java: public void init() { super.init(); getRootRequestMapperAsCompound().add(new MountedMapper("/mount/point", MyPage.class)); mountPage("/mount/point", MyPage.class); // convenient method doing the same as above } This mapper is a combination of all IRequestTargetUrlCodingStrategy implementations from Wicket 1.4. It supports:
The mapper can handle a mix of the supported parameters - indexed + named + query. PackageMapperThis mapper can mount a whole package. That is you mount a single page with a mount path prefix and then the mapper knows how to map all Page implementations in that package. Usage: MyApp.java: public void init() { super.init(); getRootRequestMapperAsCompound().add( new MountMapper("/mount/point", new PackageMapper( PackageName.forClass(Page3.class)))); mountPackage("/mount/point", Page3.class); } Assuming that PageA package is "com.example.pages" a request to "/mount/point/PageB" will use com.example.pages.PageB if it exists and is an instance of Page. ResourceMapperA mapper which mounts ResourceReference implementations. Usage: MyApp.java: public void init() { super.init(); getRootRequestMapperAsCompound().add(new ResourceMapper("/company/logo", new PackageResourceReference(MyPage.class, "res/logo.gif"))); mountResource("/company/logo", new PackageResourceReference(MyPage.class, "res/logo.gif"))); // convenient method doing the same as above } CryptoMapperA wrapper around another mapper which will encrypt/decrypt the URLs generated by the inner one. Usage: public void init() { super.init(); IRequestMapper cryptoMapper = new CryptoMapper(getRootRequestMapper(), this); setRootRequestMapper(cryptoMapper); } HttpsMapperA mapper which makes a redirect to the same URL with HTTPS protocol if the requested page is annotated with @RequireHttps or to HTTP protocol if the last processed page had @RequireHttps and the one going to be processed has no such annotation. Usage: public class MyApplication extends WebApplication { public void init() { super.init(); getRootRequestMapperAsCompound().add(new MountedMapper("secured", HttpsPage.class)); setRootRequestMapper(new HttpsMapper(getRootRequestMapper(), new HttpsConfig(80, 443))); } } Making all urls absoluteYou can override RequestCycle#newUrlRenderer() with a different UrlRenderer to force all links to be absolute. import org.apache.wicket.request.Request; import org.apache.wicket.request.Url; import org.apache.wicket.request.UrlRenderer; import org.apache.wicket.util.lang.Args; import org.apache.wicket.util.string.Strings; import org.slf4j.Logger; import org.slf4j.LoggerFactory; /** * {@link UrlRenderer} which generates absolute urls * * @author Bas * */ public class AbsoluteUrlRenderer extends UrlRenderer { private static final Logger log = LoggerFactory.getLogger( AbsoluteUrlRenderer.class ); private final Url contextUrl, filterUrl; public AbsoluteUrlRenderer( Request request, String prefix ) { super( request ); this.contextUrl = buildContextUrl( request, prefix ); this.filterUrl = buildFilterUrl( request, prefix ); log.debug( "Prefix for absolute urls: {}", filterUrl ); } @Override public String renderRelativeUrl( Url url ) { Args.notNull( url, "url" ); if( url.isAbsolute() ) { return url.toString(); } else { Url absolute = fromFilterRelativeToAbsolute( url ); log.debug( "renderRelativeUrl: {} => {}", url, absolute ); String renderedUrl = absolute.toString(); return Strings.isEmpty( renderedUrl ) ? "." : renderedUrl; } } @Override public String renderContextRelativeUrl( String url ) { Args.notNull( url, "url" ); // Prevent prefixing a url twice if( url.startsWith( contextUrl.toString() ) ) { return url; } if( url.startsWith( "/" ) ) { url = "" 1 ); } Url relativeUrl = Url.parse( url ); Url absoluteUrl = fromContextRelativeToAbsolute( relativeUrl ); log.debug( "renderContextRelativeUrl: {} -> {}", relativeUrl, absoluteUrl ); return absoluteUrl.toString(); } private Url buildContextUrl( Request request, String prefix ) { Url url = "" class="code-keyword">new Url(); if( prefix != null && prefix.length() > 0 ) { url.getSegments().addAll( Url.parse( prefix ).getSegments() ); } String contextPath = request.getContextPath(); if( contextPath.length() > 0 ) { url.getSegments().addAll( Url.parse( contextPath.substring( 1 ) ).getSegments() ); } if( !url.isAbsolute() ) { url.getSegments().add( 0, "" ); } return url; } private Url buildFilterUrl( Request request, String prefix ) { Url url = "" request, prefix ); String filterPath = request.getFilterPath(); if( filterPath.length() > 0 ) { url.getSegments().addAll( Url.parse( filterPath.substring( 1 ) ).getSegments() ); } return url; } private Url fromContextRelativeToAbsolute( Url url ) { Url absolute = new Url( url ); absolute.prependLeadingSegments( contextUrl.getSegments() ); return absolute; } private Url fromFilterRelativeToAbsolute( Url url ) { Url absolute = new Url( url ); absolute.prependLeadingSegments( filterUrl.getSegments() ); return absolute; } } There is an optional prefix parameter which you can supply to the constructor to set a fixed prefix. The sole purpose of this prefix is to handle the case where the app server is behind a reverse proxy, and the reverse proxy maps a folder to a root-mapped application. E.g. the reverse proxy maps "/app" to an app running at "/" with the WicketFilter handling "/*". All urls need to be prefixed with "/app", but AbsoluteUrlRenderer cannot detect this. Installing this custom UrlRenderer can be done by installing a custom IRequestCycleProvider: import org.apache.wicket.IRequestCycleProvider; import org.apache.wicket.request.UrlRenderer; import org.apache.wicket.request.cycle.RequestCycle; import org.apache.wicket.request.cycle.RequestCycleContext; /** * {@link RequestCycle} provider which overrides the {@link UrlRenderer} to generate absolute links. * * @see {@link AbsoluteUrlRenderer} * @see {@link RequestCycle#getUrlRenderer()} * * @author Bas * */ public class AbsoluteUrlRequestCycleProvider implements IRequestCycleProvider { private final String prefix; public AbsoluteUrlRequestCycleProvider() { this( null ); } public AbsoluteUrlRequestCycleProvider( String prefix ) { this.prefix = prefix; } @Override public RequestCycle get( RequestCycleContext context ) { return new RequestCycle( context ) { @Override protected UrlRenderer newUrlRenderer() { return new AbsoluteUrlRenderer( getRequest(), prefix ); } }; } } In your Application#init() method, call setRequestCycleProvider( new AbsoluteUrlRequestCycleProvider() );
Change Notification Preferences
View Online
|
View Changes
|
Add Comment
|
- [CONF] Apache Wicket > Request mapping confluence
- [CONF] Apache Wicket > Request mapping confluence
- [CONF] Apache Wicket > Request mapping confluence
- [CONF] Apache Wicket > Request mapping confluence
