I would avoid the use of static maps for tracking the radio groups. In addition, static will be shared between threads, so you have code that will likely break in production.
In my book, static should always be stateless. You have two approaches that work better (and are more testable): - Define a service with scope "perthread" as a wrapper around this map - Define your own Environmental object. You can make a contribution to the PageRenderInitializer service to get your environmental object set up. Generally when you think "I wish PageRenderSupport had such and such a method" you can accomplish the same thing by defining your own environmental. On 10/14/07, Michael Gerzabek <[EMAIL PROTECTED]> wrote: > > Sorry, you're right: The component is basically a compilation of Radio > and RadioGroup. I use a static Map to secure the unique names of > [EMAIL PROTECTED]'s. In setupRender() I prepare the environment in > cleanupRender() I wrap it up. > > Here's the code ... > > > package com.michaelgerzabek.services.web.components; > > import java.util.HashMap; > import java.util.Map; > > import org.apache.tapestry.Binding; > import org.apache.tapestry.ComponentAction; > import org.apache.tapestry.ComponentResources; > import org.apache.tapestry.Field; > import org.apache.tapestry.MarkupWriter; > import org.apache.tapestry.PageRenderSupport; > import org.apache.tapestry.RadioContainer; > import org.apache.tapestry.ValueEncoder; > import org.apache.tapestry.annotations.Environmental; > import org.apache.tapestry.annotations.Parameter; > import org.apache.tapestry.internal.TapestryInternalUtils; > import org.apache.tapestry.ioc.annotations.Inject; > import org.apache.tapestry.services.ComponentDefaultProvider; > import org.apache.tapestry.services.Environment; > import org.apache.tapestry.services.FormSupport; > import org.apache.tapestry.services.Request; > import org.apache.tapestry.services.ValueEncoderSource; > > public class Radio implements Field { > > @Parameter(required = false, defaultPrefix = "literal") > private String _id; > private String _clientId; > > @Parameter(defaultPrefix = "literal") > private String _label; > > @Parameter("false") > private boolean _disabled; > > @Parameter(required = false) > private ValueEncoder _encoder; > > @Parameter(required = true, principal = true, defaultPrefix="literal") > private Object _value; > > @Parameter(required = true, defaultPrefix="literal") > private String _name; > > public String getElementName() { > > return _name; > } > > @Inject > private ComponentDefaultProvider _defaultProvider; > > @Inject > private ValueEncoderSource _valueEncoderSource; > > String defaultLabel() { > > return _defaultProvider.defaultLabel( _resources ); > } > > Binding defaultValue() { > > return _defaultProvider.defaultBinding( "value", _resources ); > } > > final ValueEncoder defaultEncoder() { > > return _valueEncoderSource.createEncoder( "value", _resources ); > } > > public String getLabel() { > > return _label; > } > > public boolean isDisabled() { > > return _disabled; > } > > public String getClientId() { > > return _clientId; > } > > @Inject > private PageRenderSupport _pageRenderSupport; > > /** ------------------------------------------------------------ > * Markup generieren für <[EMAIL PROTECTED]/> > */ > > void beginRender( MarkupWriter writer ) { > > RadioContainer _container = _environment.peek( > RadioContainer.class ); > String value = _container.toClient( _value ); > > _clientId = _pageRenderSupport.allocateClientId( _id ); > _name = _container.getElementName(); > > writer.element( > "input", > "type", > "radio", > "id", > _clientId, > "name", > _name, > "value", > value); > > if ( _container.isSelected( _value ) ) writer.attributes( > "checked", "checked" ); > } > > void afterRender( MarkupWriter writer ) { > > writer.end(); > } > > /** ------------------------------------------------------------ > * Die Container-Organisation > */ > private static Map<String, RadioContainer> radioGroups = new > HashMap<String, RadioContainer>(); > > @Environmental > private FormSupport _formSupport; > > @Inject > private Environment _environment; > > @Inject > private ComponentResources _resources; > > @Inject > private Request _request; > > private Object _clientValue; > > private void setup( String elementName ) { > > _name = elementName; > } > > private void processSubmission() { > > String clientValue = _request.getParameter( _name ); > > Object value = _encoder.toValue( clientValue ); > > _clientValue = value; > } > > private static class Setup implements ComponentAction<Radio> { > > private static final long serialVersionUID = > -7984673040135949374L; > > private final String _elementName; > > Setup( String elementName ) > { > _elementName = elementName; > } > > public void execute( Radio component ) > { > component.setup( _elementName ); > } > }; > > private static final ComponentAction<Radio> PROCESS_SUBMISSION = new > ComponentAction<Radio>() > { > private static final long serialVersionUID = > -3857110108918776386L; > > public void execute( Radio component ) > { > component.processSubmission(); > } > }; > > /** > * Environment herstellen > */ > final void setupRender() { > > String name = _formSupport.allocateElementName( > _resources.getElementName() ); > > RadioContainer radioContainer = radioGroups.get( name ); > > if ( null == radioContainer ) { > > ComponentAction<Radio> action = new Setup( name ); > _formSupport.storeAndExecute( this, action ); > > radioGroups.put( name, new RadioContainer() { > > public String getElementName() { > > return _name; > } > > public boolean isDisabled() { > > return _disabled; > } > > @SuppressWarnings("unchecked") > public String toClient( Object value ) { > > if ( null == _encoder ) > return value.toString(); > return _encoder.toClient( value ); > } > > public boolean isSelected( Object value ) { > > return TapestryInternalUtils.isEqual( value, > _clientValue ); > } > > }); > > _formSupport.store( this, PROCESS_SUBMISSION ); > } > _environment.push( RadioContainer.class, radioContainer ); > } > > /** > * Environment aufräumen > */ > final void cleanupRender() { > > _environment.pop( RadioContainer.class ); > } > > } > > > Nick Westgate schrieb: > > Hard to say without seeing the source and how you are using the > > component. > > > > Cheers, > > Nick. > > > > > > Michael Gerzabek wrote: > >> I implemented an alternative radio component. When I debug my > >> setupRender() the parameters from the template are all null. When I > >> look into the ComponentResources in _bindings there are all my > >> Parameters but the annotated values in my own component seem never be > >> initialized. > >> > >> What am am I missing? > > > --------------------------------------------------------------------- > To unsubscribe, e-mail: [EMAIL PROTECTED] > For additional commands, e-mail: [EMAIL PROTECTED] > > -- Howard M. Lewis Ship Partner and Senior Architect at Feature50 Creator Apache Tapestry and Apache HiveMind