I have the code snippet below, and want to give the output from <#spring.message "name"/> as paraneter to the macro (the placeholder parameter).
Providing it directly as I tried doing below didnt't work, anyone knows how I should do it?
<td class="rightCell"><b><#spring.message "name"/>:</b></td>
<td class="leftCell"><#createUserInputItemModifiedv2 "name", "name", "text", #spring.message "name" /></td>
<#macro createUserInputItemModifiedv2 attributeName, errorMessageName, inputType, placeholder>
<input class="edit" type="${inputType}" id="${attributeName}" name="${attributeName}" placeholder="${placeholder}" value="${user[attributeName]!}"/><br/>
<#if validationErrors?? && validationErrors[attributeName]??>
<div class="errorMessage" id="${errorMessageName}Error">
${validationErrors[attributeName]!}
</div>
</#if>
</#macro>
That's because spring.message should also be a FreeMarker function, not just a FreeMarker macro. Macros has no return value (they might directly print to the output writer as a side-effect) so you can't call them where an expression is expected. Anyway... how to work this around right now. Looking at the source code of Spring, maybe this will work:
<#function message code><#return springMacroRequestContext.getMessage(code)></#function>
You could create a utils.ftl or something, (auto-)#import it as u, and then you can do <#createUserInputItemModifiedv2 ..., u.message("name")> in your templates. (Actually, it could be made more convenient, so that you can just write msg.name or like, but let's not go into that here.)
However, I'm not sure if there's any backward compatibility guarantee regarding springMacroRequestContext or its content. So ultimately this should be fixed in Spring.
Related
I concatenate the state and my stateCounter to one String. This is my value for my HashMap which stores all the States but I don't get it to to put this variable in my method.
<span th:with="stateName=${item.state} + ${item.stateCounter}"></span>
<td th:text="${{order.getStateRepository().get(${stateName})}}"></td>
Some notes:
(1) Using a <td> tag suggests you are also using a <table>. Having a <span> tag next to a <td> tag inside a table is not valid HTML (assuming this is what it looks like in your template - maybe this is just a copy/paste thing).
(2) If your order object has a stateRepository field, then you don't need to use a getter order.getStateRepository(). You can just use the field name order.stateRepository. You are already doing this with item.state, for example. Thymeleaf will figure out from the field name how to use the related getter - e.g. item.getState().
(3) The scope (availability/visibility) of a local variable, such as stateName in th:with="stateName=${item.state} is limited to the tag in which it is declared, and also to any sub-tags. You do not have any sub-tags in your <span> - therefore the variable is not visible anywhere outside of your span. So, it is not available inside the <td> tag.
(4) Do you need to use a local variable in your example?
Instead of using get(stateName), you can use the expression referred to by stateName directly:
get(item.state + item.stateCounter)
So overall, this should work (based on the assumptions above):
<td th:text="${order.stateRepository.get(item.state + item.stateCounter)}"></td>
Maybe you do need the local variable, of course. It may depend on the wider context of the Thymeleaf template.
You are trying to use local variable. It will work if you try as follows:
<div th:with="stateName=${item.state} + ${item.stateCounter}">
<td th:text="${order.getStateRepository().get(stateName)}"></td>
</div>
I have Spring bind form with freemarker. But large number show Exponent value. How to show value without Exponent.
For small numeber...
<#spring.formInput 'CaseMaster.year' 'placeholder="e.g. 2013" ' 'number'/>
For large number...
<#spring.formInput 'CaseMaster.suitValue' 'placeholder="e.g. Suit Value" ' />
HTML View:
I wnat to show value like 80000000 or 80000000.00 or like 8,00,00,000.00 in the second field.
I've dug down into the macro and it looks like you have no control over the formatting to the populated value. Perhaps there's a more elegant solution, but at this point, you might try creating your own macro that gives you formatting control. (This is untested.)
<#macro numberInput path attributes="">
<#spring.bind path/>
<input type="text"
id="${status.expression?replace('[','')?replace(']','')}"
name="${status.expression}" value="status.value?c" ${attributes}<#closeTag/>
</#macro>
You would call it like this:
<#numberInput 'CaseMaster.suitValue' 'placeholder="e.g. Suit Value" ' />
Is it possible to write this in freemarker.
<input type="checkbox" value="Available ?" checked="<#if ${status}=='Available'>true<#else>false</#if>"/>
For now it throws exception
I want html checkbox to be checked if status property equals to "Available".
How to do this in freemarker?
<#if ${status}=='Available'> has a syntactical error (that the error message that you haven't included points to, I'm certain): you can't use ${...} inside FreeMarker tags (well, except inside string literals, but whatever). It should be just <#if status == 'Available'>. But, the simples solution for what you want is:
checked="${(status == 'Available')?c}"
or if you have an older FreeMarker then:
checked="${(status == 'Available')?string('true', 'false')}"
When I use the following code in my razor view it renders <label for=""> someText</label> and not <label for="">1. someText</label> but I can't figure out why 1. is removed while rendering.
#Html.Label(String.Format("{0}. someText",1))
Edit:
The following code renders <label for="">1# someText</label> as expected.
#Html.Label(String.Format("{0}# someText",1))
You are misusing the Html.Label method. It is for:
Returns an HTML label element and the property name of the property
that is represented by the specified expression.
That's why it gets confused if you have a point . in the first parameter because it expects a property expression there.
However, you can use the second overload:
#Html.Label("", String.Format("{0}. someText",1))
Or just write out the HTML:
<label>#String.Format("{0}. someText", 1)</label>
You can avoid using the "Html Helper's label" and directly use html "label" and place whatever you want to display correctly. It can also save some time ;)
The syntax which you are using is wrong or We can say that this is not a way to use property with RAZOR syntax.
You ca use this that may be help full for you.
**
#Html.LabelFor(model => model.PropertyName,
String.Format("{0}. " + #Model.PropertyName.ToString() + ",1))
**
I was using this for a data table that contained a double (Lat/Long) and saw this same problem. Thanks for the tips (I am not allowed to comment).
For me, the problem was solved ..
#foreach (var cell in item.ItemArray)
{
<td>
#Html.Label("",cell.ToString().Trim())
</td>
}
Code:
<#spring.formInput 'myForm.spouseEmail' 'id="spouseEmail" class="text"'/>
<#spring.showErrors ', ' 'error'/>
Output:
<span class="error">not a well-formed email address</span>
What I want:
<div class="error">not a well-formed email address</div>
#Mike: it seems you have troubles understanding the nature of macros. They are already-written freemarker script to make your life easier. You can always write a customed one.
Some people think it obvious, but I myself find that it's not easy to know how to view the spring-freemarker macros source code. You can navigate to package org/springframework/spring-webmvc-3.0.5.jar/org/springframework/web/servlet/view/freemarker/spring.ftl in Eclipse's "Referenced Libraries".
Here's the macro "showErrors" gotten from "spring.ftl":
<#macro showErrors separator classOrStyle="">
<#list status.errorMessages as error>
<#if classOrStyle == "">
<b>${error}</b>
<#else>
<#if classOrStyle?index_of(":") == -1><#assign attr="class"><#else><#assign attr="style"></#if>
<span ${attr}="${classOrStyle}">${error}</span>
</#if>
<#if error_has_next>${separator}</#if>
</#list>
</#macro>
To achieve your goal, it's very simple: just write a custom macro which is exactly like the code above, replace span by div
No, but you can easily write your own macro to do whatever you want. Get your inspiration from spring.showErrors itself.