Created
November 27, 2010 14:46
-
-
Save aldaris/717942 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.apache.wicket.request.Request; | |
import org.apache.wicket.request.Url; | |
import org.apache.wicket.request.Url.QueryParameter; | |
import org.apache.wicket.request.component.IRequestablePage; | |
import org.apache.wicket.request.mapper.MountedMapper; | |
import org.apache.wicket.request.mapper.parameter.PageParameters; | |
import org.apache.wicket.request.mapper.parameter.PageParametersEncoder; | |
import org.apache.wicket.util.string.Strings; | |
/** | |
* | |
* @author aldaris | |
*/ | |
public class MyMapper extends MountedMapper { | |
public MyMapper(final String mountPath, Class<? extends IRequestablePage> pageClass) { | |
super(mountPath, pageClass, new PageParametersEncoder() { | |
@Override | |
public PageParameters decodePageParameters(Request request) { | |
PageParameters parameters = new PageParameters(); | |
String params = request.getUrl().toString(); | |
String[] named = params.split("/"); | |
for (int i = 0; i < named.length; i += 2) { | |
parameters.add(named[i], named[i + 1]); | |
} | |
for (QueryParameter p : request.getUrl().getQueryParameters()) { | |
parameters.add(p.getName(), p.getValue()); | |
} | |
return parameters.isEmpty() ? null : parameters; | |
} | |
@Override | |
public Url encodePageParameters(PageParameters pageParameters) { | |
Url url = new Url(); | |
for (int i = 0; i < pageParameters.getIndexedCount(); ++i) { | |
url.getSegments().add(pageParameters.get(i).toString()); | |
} | |
for (PageParameters.NamedPair pair : pageParameters.getAllNamed()) { | |
if (Strings.isEmpty(pair.getValue())) { | |
QueryParameter param = new QueryParameter(pair.getKey(), pair.getValue()); | |
url.getQueryParameters().add(param); | |
} else { | |
url.getSegments().add(pair.getKey()); | |
url.getSegments().add(pair.getValue()); | |
} | |
} | |
return url; | |
} | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment