I have a need to create a url to be put into an email.  The code is in a service class without direct access to a Page or Component.  At first glance, I thought that WebRequestCycle.urlFor would do the trick.   I soon found out that it only gives me the relative path.  After some work, I came up with the following code:

import javax.servlet.http.HttpServletRequest;
import wicket.Page;
import wicket.PageParameters;
import wicket.RequestCycle;
import wicket.protocol.http.WebRequest;
import wicket.protocol.http.WebRequestCycle;

* Builds a fully qualified path to the indicated page with the given
* parameters.
* @param pageClass The class of the Wicket page to link to.
* @param pageParameters The parameters to send to the page.  Send an empty
*                       PageParameters instance if there are no parameters to send.
* @return The url in the form of a String.
public String buildFullyQualifiedPath(
Class<? extends Page>pageClass, PageParameters pageParameters
StringBuilder sb = new StringBuilder();
WebRequestCycle cycle = (WebRequestCycle)RequestCycle.get();
HttpServletRequest req = ((WebRequest)cycle.getRequest()).getHttpServletRequest();
if (req.getServerPort() != 80) {
sb.append(cycle.urlFor(null, pageClass, pageParameters));
return sb.toString();

This works well enough, but seems a bit hackish.  I was wondering if anyone else had come up with a better solution.


Philip A. Chapman

Desktop and Web Application Development:
Java, .NET, PostgreSQL, MySQL, MSSQL
Linux, Windows 2000, Windows XP

Attachment: signature.asc
Description: This is a digitally signed message part

Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
Wicket-user mailing list

Reply via email to