[ https://issues.apache.org/jira/browse/VELOCITY-881?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16091033#comment-16091033 ]
pixiang.lq commented on VELOCITY-881: ------------------------------------- At first, thank you for your answers Claude Brisson. I have try to force a rendering with code like: #if( "$some.reference" == 'some value') but it still doesn't work, and the output was: -------------------------- <bean class="MY_CLASS2"> <property name="MY_PROPERTY_NAME2"> <value>${provider.version}</value> </property> </bean> -------------------------- I know "${deploy.domain} will always be null, since Velocity will first search the context for a deploy property". But in our team property naming conventions is "property.name", like "provider.version", rather than "property_name". So "reference insertion handler" mechanism is a good way for us to replace the value when velocity got value null. "$foo.bar.baz would call the handler three times, which is not at all desirable." In fact $foo.bar.baz would call the handler only one times and i have debug to verify that. ----------------------------------- public Object execute(Object o, InternalContextAdapter context) throws MethodInvocationException { if(this.referenceType == 4) { return null; } else { {color:red}Object result = this.getVariableValue(context, this.rootString);{color} this.log.error(String.format("rootString:%s, result:%s", this.rootString, result)); if(result == null && !this.strictRef) { return EventHandlerUtil.invalidGetMethod(this.rsvc, context, this.getDollarBang() + this.rootString, (Object)null, (String)null, this.uberInfo); } else { ........ At last, may be my suggestion is not good for performance in other scenes and my be have another way to solute this. I fill it very strange that "reference insertion handler" mechanism has give a way to replace the inserted value in the template, but it not support to replace the reference in if statement. > Even handler didn't work when the reference appear in #if statement > ------------------------------------------------------------------- > > Key: VELOCITY-881 > URL: https://issues.apache.org/jira/browse/VELOCITY-881 > Project: Velocity > Issue Type: Bug > Components: Engine > Affects Versions: 1.7 > Environment: it a common case. It nothing to do with a specified > environment > Reporter: pixiang.lq > Assignee: Claude Brisson > Labels: easyfix > Original Estimate: 2h > Remaining Estimate: 2h > > My code like: > ---------------------------------------------------------------- > public static void main(String[] args) { > VelocityEngine ve = new VelocityEngine(); > ve.setProperty(Velocity.RESOURCE_LOADER, "class"); > ve.setProperty("class.resource.loader.class", > > "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader"); > try { > ve.init(); > Template tp = ve.getTemplate("my-template.xml"); > Context context = new VelocityContext(); > // add my event handler > Map map = new HashMap<>(); > map.put("deploy.domain", "offline"); > map.put("deploy_domain", "offline"); > map.put("provider.version", "1.0.0"); > map.put("provider_version", "1.0.0"); > EventCartridge eventCartridge = new EventCartridge(); > eventCartridge.addEventHandler(new > MyReferenceInsertionHandler(map)); > eventCartridge.attachToContext(context); > StringWriter writer = new StringWriter(); > tp.merge(context, writer); > System.out.println(writer.toString()); > } catch (Exception e) { > e.printStackTrace(); > } > } > ---------------------------------------------------------------- > My template file to be converted by velocity like: > ---------------------------------------------------------------- > #if(${deploy.domain}=='testString') > <bean class="MY_CLASS1"> > <property name="MY_PROPERTY_NAME1"> > <value>aaa</value> > </property> > </bean> > #end > <bean class="MY_CLASS2"> > <property name="MY_PROPERTY_NAME2"> > <value>${provider.version}</value> > </property> > </bean> > ---------------------------------------------- > My got the output in fact: > ---------------------------------------------- > <bean class="MY_CLASS2"> > <property name="MY_PROPERTY_NAME2"> > <value>1.0.0</value> > </property> > </bean> > ---------------------------------------------- > My expected output: > ---------------------------------------------- > <bean class="MY_CLASS1"> > <property name="MY_PROPERTY1"> > <value>aaa</value> > </property> > </bean> > <bean class="MY_CLASS2"> > <property name="MY_PROPERTY_NAME2"> > <value>1.0.0</value> > </property> > </bean> > ---------------------------------------------- > My suggestion to fix this bug: > ASTReference.java > ---------------------------------------------- > The method before change: > public Object value(InternalContextAdapter context) > throws MethodInvocationException > { > return (computableReference ? execute(null, context) : null); > } > After change: > public Object value(InternalContextAdapter context) > throws MethodInvocationException > { > Object value = this.computableReference?this.execute((Object)null, > context):null; > if(value == null && !this.escaped) { > // process the value by EventHandlerUtil > value = EventHandlerUtil.referenceInsert(this.rsvc, context, > this.literal, value); > } > return value; > } > ---------------------------------------------- -- This message was sent by Atlassian JIRA (v6.4.14#64029) --------------------------------------------------------------------- To unsubscribe, e-mail: dev-unsubscr...@velocity.apache.org For additional commands, e-mail: dev-h...@velocity.apache.org