One of the first things I found as I was getting familiar with Wicket is how do you point to resources that are part of a page. Well I did some digging around to find out so I’m documenting it here for others who might be facing the same challenges as they discover Apache Wicket.
Well, the above code works when you have a JSP page that gets compiled at run-time before being served. But in the case of Wicket, it does not. So how do you handle such situations?
wicket:head /wicket:head …
Step-2: Get the ServletContext and in turn the Context-Path for the web application in your Java/Scala code.
For application servers implementing Servlet 2.5 and above: (Tomcat 6.x implements Servlet 2.5) [sourcecode language=“java”]
ServletContext servletContext = WebApplication.get().getServletContext(); String contextPath = servletContext.getContextPath();
For application servers implementing Servlet 2.4: (Tomcat 5.5x implements Servlet 2.4) [sourcecode language=“java”]
String contextPath = getWebRequestCycle().getWebRequest().getHttpServletRequest().getContextPath();
What's the difference? Well, in Servlet 2.5, a new method was added String javax.servlet.ServletContext.getContextPath() But if you are deploying your code in an earlier Tomcat or other application servers that do not implement Servlet 2.5, you will be at a disadvantage. So in those cases, use the other approach.
Step-3: In your Java/Scala Wicket code, just as you add labels or other components to your page you can add the following:
So here’s what happens. In step-2, we created a variable called “contextPath” with the Servlet Context Path as it’s value. And when you add a “StringHeaderContributor”, it adds an entry into the pages header. It’s that simple!
So now after your browser goes through the request you now have the correct “ContextPath” inserted into your application:
After I posted the article I noticed I had left out a few additional details on this topic. If you are adding resources such as images on your page, Wicket provides you with a easy way to do this. There is a org.apache.wicket.markup.html.image.ContextImage class that you can use to create image html tags in your page.
In your Java or Scala class you can do the following: [sourcecode language=“html”]
add(new ContextImage(“logo”, contextPath + “/i/logo.jpg”))
and in your html (Wicket) page you can add a html-wicket element like this: [sourcecode language=“html”]
Wicket matches the wicket:id “logo” with the element added in the code and displays the image with the correct context-path.
If you find this article useful, consider signing up for my RSS feed or Email Newsletter. See links on the right side.