Pergunta

I need to pass a parameter to my page and I can't find a way to pass parameters that might be null.

If I do:

PageParameters pageParameters = new PageParameters ();
pageParameters.add ("key", null);

This will result in an exception

java.lang.IllegalArgumentException: Argument 'value' may not be null. at org.apache.wicket.util.lang.Args.notNull(Args.java:41)

If I use Google Guava's Optional, I can't find any way to cast the object even if the the Optional object is not holding a null ie not equals to Optional.absent() :

In my landing page's constructor I do

StringValue sv = parameters.get ("key");
sv.to ( Optional.of (MyEnum.SOME_ENUM_CONSTANT).getClass () );

and when I run it I get this error:

org.apache.wicket.util.string.StringValueConversionException: Cannot convert 'Optional.of(SOME_ENUM_CONSTANT)'to type class com.google.common.base.Present.

Am I doing something wrong?

Is there any other way to pass a possibly null object in wicket 6?

I noticed in wicket 1.4 they have PageParameters.NULL which seems to have dissapeared in wicket 6.

Thank you

Foi útil?

Solução

This might be too simple, but what's wrong with

Object value = ?
if (value != null) {
    pageParameters.add ("key", value);
}

and

StringValue sv = pageParameters.get("key");
if (!sv.isNull()) {
    // process string value
}

Outras dicas

All page parameters in wicket will be treated as Strings eventually. The idea is that a page parameter will be on the URL of the request.

From the javadoc:

Suppose we mounted a page on /user and the following url was accessed /user/profile/bob?action=view&redirect=false. In this example profile and bob are indexed parameters with respective indexes 0 and 1. action and redirect are named parameters.

If you add something like x=y&x, the parameter x will appear twice, once with the String y and another with the empty string.

Depending on what you are trying to accomplish I would suggest to either

  • Don't pass the parameter at all when there is a null value required and use the isNull or toOptional methods.
  • Use indexed parameters and test for presence of a certain word
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top