You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@struts.apache.org by "Roland McIntosh (JIRA)" <ji...@apache.org> on 2012/05/29 16:50:23 UTC
[jira] [Comment Edited] (WW-3828) FreeMarker dynamic attributes
cannot be boolean
[ https://issues.apache.org/jira/browse/WW-3828?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13284854#comment-13284854 ]
Roland McIntosh edited comment on WW-3828 at 5/29/12 2:48 PM:
--------------------------------------------------------------
Build #481 does NOT fix this issue as I'm experiencing it, but it DOES allow booleans in dynamic attributes when used as a "pass-through" attribute (sorry for my inexact terminology). However, the type of the dynamic attribute still appears to be string in the tag's parameters map (whereas in 2.3.1.2 it was not).
For posterity, in the above example, the tag should have read:
{code}
<@s.textfield type="email" key="user.email" autofocus=true required=true />
{code}
(note @s.textfiled instead of @s.text)
The reasoning for supporting dynamic attributes of type boolean or Integer is so that custom themes (like an "html5" theme) can better match the existing API for the "textfield" tag. A theme developer might expect to be able to provide a dynamic attribute "autofocus" boolean to match the existing "required" attribute boolean. This allows themes that rely on dynamic attributes to be "first-class" citizens in the API, and not to be restricted to Strings. Furthermore, allowing booleans (and Integers) in dynamic attributes matches Struts 2.3.1.2 behavior.
I have a theme "mytheme" with the following "text.ftl" contents:
{code:title=mytheme/text.ftl}
<#include "/${parameters.templateDir}/${parameters.theme}/controlheader.ftl" />
<#include "/${parameters.templateDir}/html5/text.ftl" />
<#include "/${parameters.templateDir}/css_xhtml/controlfooter.ftl" />
{code}
Note that it includes "html5/text.ftl". This file contains (partially):
<#if parameters.autofocus?default(false)>
autofocus="autofocus"<#rt/>
</#if>
I copy/pasted the existing implementation of the (documented, supported) "required" attribute. The intention is that if a boolean parameter named "autofocus" exists and has value true, that an attribute/value 'autofocus="autofocus"' will be added to inputs. The actual value of the attribute doesn't matter so much, but is set as "autofocus" for backwards compatibility and conformance to older HTML specifications.
Because my "html5/text.ftl" file checks the parameters map for a boolean "autofocus" attribute, I get the following exception with Build #481 / Struts 2.3.5 SNAPSHOT (same as with 2.3.4):
{noformat}
2012-05-29 09:19:11,568 ERROR [freemarker.runtime] (http-0.0.0.0-8443-6) Template processing error: "Error on line 49, column 6 in themes/html5/text.ftl
Expecting a boolean (true/false) expression here
Expression parameters.autofocus?default(false) does not evaluate to true/false
it is an instance of freemarker.ext.beans.StringModel"
Error on line 49, column 6 in themes/html5/text.ftl
Expecting a boolean (true/false) expression here
Expression parameters.autofocus?default(false) does not evaluate to true/false
it is an instance of freemarker.ext.beans.StringModel
The problematic instruction:
----------
==> if parameters.autofocus?default(false) [on line 49, column 1 in themes/html5/text.ftl]
in include "/${parameters.templateDir}/html5/text.ftl" [on line 25, column 1 in themes/mytheme/text.ftl]
----------
Java backtrace for programmers:
----------
freemarker.core.NonBooleanException: Error on line 49, column 6 in themes/html5/text.ftl
Expecting a boolean (true/false) expression here
Expression parameters.autofocus?default(false) does not evaluate to true/false
it is an instance of freemarker.ext.beans.StringModel
at freemarker.core.Expression.isTrue(Expression.java:150)
at freemarker.core.ConditionalBlock.accept(ConditionalBlock.java:77)
at freemarker.core.Environment.visit(Environment.java:221)
at freemarker.core.MixedContent.accept(MixedContent.java:92)
{noformat}
If I remove my check for the "autofocus" parameter from text.ftl, then the value is passed through as a dynamic attribute autofocus="true". This actually works, but isn't what I intended originally when creating my "html5/text.ftl" template, since I had hoped to query the parameters map directly.
was (Author: rgm):
Build #481 does NOT fix this issue as I'm experiencing it, but it DOES allow booleans in dynamic attributes when used as a "pass-through" attribute (sorry for my inexact terminology). However, the type of the dynamic attribute still appears to be string in the tag's parameters map (whereas in 2.3.1.2 it was not).
For posterity, in the above example, the tag should have read:
<@s.textfield type="email" key="user.email" autofocus=true required=true />
(note @s.textfiled instead of @s.text)
The reasoning for supporting dynamic attributes of type boolean or Integer is so that custom themes (like an "html5" theme) can better match the existing API for the "textfield" tag. A theme developer might expect to be able to provide a dynamic attribute "autofocus" boolean to match the existing "required" attribute boolean. This allows themes that rely on dynamic attributes to be "first-class" citizens in the API, and not to be restricted to Strings. Furthermore, allowing booleans (and Integers) in dynamic attributes matches Struts 2.3.1.2 behavior.
I have a theme "mytheme" with the following "text.ftl" contents:
<#include "/${parameters.templateDir}/${parameters.theme}/controlheader.ftl" />
<#include "/${parameters.templateDir}/html5/text.ftl" />
<#include "/${parameters.templateDir}/css_xhtml/controlfooter.ftl" />
Note that it includes "html5/text.ftl". This file contains (partially):
<#if parameters.autofocus?default(false)>
autofocus="autofocus"<#rt/>
</#if>
I copy/pasted the existing implementation of the (documented, supported) "required" attribute. The intention is that if a boolean parameter named "autofocus" exists and has value true, that an attribute/value 'autofocus="autofocus"' will be added to inputs. The actual value of the attribute doesn't matter so much, but is set as "autofocus" for backwards compatibility and conformance to older HTML specifications.
Because my "html5/text.ftl" file checks the parameters map for a boolean "autofocus" attribute, I get the following exception with Build #481 / Struts 2.3.5 SNAPSHOT (same as with 2.3.4):
2012-05-29 09:19:11,568 ERROR [freemarker.runtime] (http-0.0.0.0-8443-6) Template processing error: "Error on line 49, column 6 in themes/html5/text.ftl
Expecting a boolean (true/false) expression here
Expression parameters.autofocus?default(false) does not evaluate to true/false
it is an instance of freemarker.ext.beans.StringModel"
Error on line 49, column 6 in themes/html5/text.ftl
Expecting a boolean (true/false) expression here
Expression parameters.autofocus?default(false) does not evaluate to true/false
it is an instance of freemarker.ext.beans.StringModel
The problematic instruction:
----------
==> if parameters.autofocus?default(false) [on line 49, column 1 in themes/html5/text.ftl]
in include "/${parameters.templateDir}/html5/text.ftl" [on line 25, column 1 in themes/mytheme/text.ftl]
----------
Java backtrace for programmers:
----------
freemarker.core.NonBooleanException: Error on line 49, column 6 in themes/html5/text.ftl
Expecting a boolean (true/false) expression here
Expression parameters.autofocus?default(false) does not evaluate to true/false
it is an instance of freemarker.ext.beans.StringModel
at freemarker.core.Expression.isTrue(Expression.java:150)
at freemarker.core.ConditionalBlock.accept(ConditionalBlock.java:77)
at freemarker.core.Environment.visit(Environment.java:221)
at freemarker.core.MixedContent.accept(MixedContent.java:92)
If I remove my check for the "autofocus" parameter from text.ftl, then the value is passed through as a dynamic attribute autofocus="true". This actually works, but isn't what I intended originally when creating my "html5/text.ftl" template, since I had hoped to query the parameters map directly.
> FreeMarker dynamic attributes cannot be boolean
> -----------------------------------------------
>
> Key: WW-3828
> URL: https://issues.apache.org/jira/browse/WW-3828
> Project: Struts 2
> Issue Type: Improvement
> Components: Core Actions
> Affects Versions: 2.3.4
> Reporter: Lukasz Lenart
> Assignee: Lukasz Lenart
> Priority: Trivial
> Fix For: 2.3.5
>
>
> The code below will produce an exception
> {code:html}
> <@s.text type="email" key="user.email" required=true autofocus=true />
> {code}
> {code}
> Method public java.lang.String org.apache.struts2.util.StrutsUtil.translateVariables(java.lang.String) threw an exception when invoked on org.apache.struts2.util.StrutsUtil@540948a7
> The problematic instruction:
> ----------
> ==> assignment: value=struts.translateVariables(keyValue)!keyValue [on line 27, column 3 in /themes/simple/dynamic-attributes.ftl]
> in include "/${parameters.templateDir}/simple/dynamic-attributes.ftl" [on line 52, column 1 in /themes/simple/text.ftl]
> in include "/${parameters.templateDir}/simple/text.ftl" [on line 25, column 1 in /themes/my-custom-theme/text.ftl]
> ----------
> {code}
--
This message is automatically generated by JIRA.
If you think it was sent incorrectly, please contact your JIRA administrators: https://issues.apache.org/jira/secure/ContactAdministrators!default.jspa
For more information on JIRA, see: http://www.atlassian.com/software/jira