Re: named page context parameters ? - using a Custom ValueEncoder
Hi, I think I found a simple way of passing named context parameters between pages and this is to create a custom ValueEncoder for map objects. I created a MapValueEncoder, and my intention was for it to do the following : Encode a Map of String/Object pairs to a string of the following form: key1/value1/key2/value2 etc... (and of course decode said string back to a map that can be passed to the context) This has the advantage (I thought) of creating nice URLs from my map context e.g. www.mysite.com/key1/value1/key2/value2 The problem is that the context string is URL encoded so that the forward slashes come out as key1%252F%252Fvalue1%252F%252Fkey2%252F%252Fvalue2 Is there anyway to override the default encoding of TapestryInternalUtils.encodeContext(id) ? thx Joel Halbert wrote: Hi Ivan, I'm trying out your extension, thanks, one question though: The following class seems to be missing from the zip you linked to below (http://wfrag.org/files/tapext.zip): ru.nsc.ict.catalogue.annotations.QueryParameter can i get it anywhere? Thx Joel Ivan Dubrov wrote: Joel Halbert wrote: Hi, Is it possible to have named page context parameters? (along the lines of Wicket PageParameters). I would like to be able to have bookmarkable URLs but rather than indexing page context parameters (in onActivate) by ordinal in a list I would rather key them by name. You can use Link#addParameter to add named parameters to Link and Request#getParameter to get parameter value. This is simple way, but requires some extra coding. I’ve wrote my simple extension to Tapestry that allows to work with named parameters the same way as with regular context parameters. You can either implement your event handlers (onStoreParameters/onRestoreParameters, their meaning is similar to onPassivate/onActivate) or use @QueryParameter annotatiton. Code: http://wfrag.org/files/tapext.zip You can use it the following way (snippet from page class): @QueryParameter private Integer page; The @QueryParameter works the similar way to @PageActivationContext annotation Since createPageLink/createEventLink and t:pagelink/t:eventlink does not support overriding named parameters, to generate a link with “page” value other than current one, you can use the following workaround: // We temporarily set page to current one, to generate proper link, // since we don't have a way to override parameter values int saved = page; page = newpage; Link link = resources.createPageLink(resources.getPageName(), false); page = saved; Also, the code does not work well for primitive types (like “int”), so it’s better to use wrappers. -- SU3 Analytics Ltd 61b Oxford Gardens W10 5UJ London Tel: +44 20 8960 2634 Mob: +44 75 2501 0825 www.su3analytics.com SU3 Analytics Ltd is a company registered in England and Wales under company number 06639473 at registered address 61b Oxford Gardens, London W10 5UJ, United Kingdom. - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: named page context parameters ? - using a Custom ValueEncoder
Em Thu, 09 Oct 2008 08:06:09 -0300, Joel Halbert [EMAIL PROTECTED] escreveu: Encode a Map of String/Object pairs to a string of the following form: key1/value1/key2/value2 etc... (and of course decode said string back to a map that can be passed to the context) Instead of encoding to a String, encode it to a List (first key, first value, second key, second value, etc). This way, the slashes won't be URL encoded. -- Thiago H. de Paula Figueiredo Independent Java consultant, developer, and instructor Consultor, desenvolvedor e instrutor em Java http://www.arsmachina.com.br/thiago - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: named page context parameters ? - using a Custom ValueEncoder
Hi Thiago, How would that work? The ValueEncoder.toClient method must return a String...something like this: import org.apache.tapestry5.ValueEncoder; public class MapValueEncoder implements ValueEncoder { private static final String DELIM = /; @Override public String toClient(Object value) { String res = ; MapString,Object map = (MapString, Object) value; for (IteratorString it = map.keySet().iterator(); it.hasNext(); ) { if (res.length() 0) { res += DELIM; } String key = it.next(); res += key + DELIM + map.get(key).toString(); } return res; } @Override public Object toValue(String clientValue) { try { clientValue = URLDecoder.decode(clientValue, UTF-8); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } String[] tokens = clientValue.split(DELIM); MapString, String res = new HashMapString, String(); boolean tokIsKey = true; String currKey = null; for (int i = 0; i tokens.length; i++) { String tok = tokens[i]; if (tokIsKey) { currKey = tok; } else { res.put(currKey, tok); } tokIsKey = !tokIsKey; } return null; } } Thiago H. de Paula Figueiredo wrote: Em Thu, 09 Oct 2008 08:06:09 -0300, Joel Halbert [EMAIL PROTECTED] escreveu: Encode a Map of String/Object pairs to a string of the following form: key1/value1/key2/value2 etc... (and of course decode said string back to a map that can be passed to the context) Instead of encoding to a String, encode it to a List (first key, first value, second key, second value, etc). This way, the slashes won't be URL encoded. -- SU3 Analytics Ltd 61b Oxford Gardens W10 5UJ London Tel: +44 20 8960 2634 Mob: +44 75 2501 0825 www.su3analytics.com SU3 Analytics Ltd is a company registered in England and Wales under company number 06639473 at registered address 61b Oxford Gardens, London W10 5UJ, United Kingdom. - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: named page context parameters ? - using a Custom ValueEncoder
Em Thu, 09 Oct 2008 10:57:31 -0300, Joel Halbert [EMAIL PROTECTED] escreveu: Hi Thiago, Hi, Joel! How would that work? The ValueEncoder.toClient method must return a String...something like this: Oops, of course it wouldn't wok with a ValueEncoder. I'm sorry. :( Instead of using a ValueEncoder, just pass the List (created from your Map) to the context parameter of the PageLink component. By the way, the type of the PageLink's context parameter is List. ;) -- Thiago H. de Paula Figueiredo Independent Java consultant, developer, and instructor Consultor, desenvolvedor e instrutor em Java http://www.arsmachina.com.br/thiago - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]