Merging maps in Freemarker - dictionary

I'm unable to find any documentation on how to merge two hash maps. This is what I am trying to acheive
<select
<#render_attrs commonattrs.merge({"class":"select"}) /> > ....
<#macro render_attrs attrs>
<#list attrs?keys as key>
<#if attrs[key]!="">
${key}="${attrs[key]}"
</#if>
</#list>

commonattrs + {"class": "select"}
It's documented here: http://freemarker.org/docs/dgui_template_exp.html#dgui_template_exp_hashop

Related

Pass loop variable from Freemarker template to Spring controller

I am listing objects in a table in my view. I want to be able to edit an object using a button in the table.
<#list products as product>
<tr>
<td>${product.productName}</td>
<td>${product.price}</td>
<td>${product.quantity}</td>
<td>
<form name="product" method="post" action="/product/edit">
<input type="submit" name="submit" value="Edit this product"/>
</form>
</td>
</tr>
</#list>
The object then should be passed to a controller method:
#RequestMapping(value="/edit", method = RequestMethod.POST)
public ModelAndView edit(#ModelAttribute("product") Product product){
ModelAndView mav = new ModelAndView("product/edit");
mav.addObject("product", product);
return mav;
}
However, the product obtained by the edit method is null. How do I fix this? I tried to bind the product inside form using the code below, but that did not work either.
<form name="product" method="post" action="/product/edit">
<#spring.bind "product" />
<input type="hidden" name="${spring.status.expression}" value="${spring.status.value}"/>
<input type="submit" name="submit" value="Edit this product"/>
</form>
I want to use the POST method.
I would like to suggest a different approach. If I'm not mistaken you just want to pick an object for later editing - you don't really edit it in that very view.
If so, all you have to do is to pass an identifier of your object to your controller, but not the selected object itself.
If not, you should give us the hole story and provide the rest of the view as well.
Assuming I'm right the next question is why you need to use a form submission at all. Passing an id is best done by links - either as parameter or, if you follow REST-style, as part of the URI itself:
<!-- Link parameter -->
<#list products as product>
<tr>
<td>${product.productName}</td>
<td>${product.price}</td>
<td>${product.quantity}</td>
<td>
Edit ${product.productName}
</td>
</tr>
</#list>
<!-- REST-style -->
...
Edit ${product.productName}
...
productName isn't a good id of course. If products is a list (meaning, java.util.List) the index of the list is handy. Even in a HashMap or Set I'd create a unique id instead of using the product name.
Now that you can identify your object, select it in the backing code for later editing, but not in the view.
You'll find loads of examples of how to get link parameters in a controller. So, no need to go into detail here.
If however you insist on using a form and a POST-method then do it like this:
<form method="post" action="/product/edit">
<#list products as product>
<tr>
<td>${product.productName}</td>
<td>${product.price}</td>
<td>${product.quantity}</td>
<td>
<button value="${product.productName}" name="product" type="submit">Edit ${product.productName}</button>
</td>
</tr>
</#list>
</form>
Note that this won't work for older IE browsers (below Ver. 10), because they don't return the value, but everything that is inside the button tag.
Hidden inputs and a single submit button won't help at all, because all inputs are submitted and using different forms is not the way either.

RactiveJS dynamic variable name

I'm curious if I can somehow use dynamic variable names in templates. For example, I'm having a loop, though a type of units in my game:
{{# config.units:unit }}
<input type="text" id="unit_{{unit}}" class="toTrain selectable" value="" placeholder="0" />
{{/ config }}
Where the value of the input should return the value of {{units_1}} for example, which represents the amount of units (type 1).
I could easily do some external object and store the amount of units for each of them but, I was wondering if I can keep the data binded because somewhere in the template there will be a total needed resources which is calculated whith these values.
The only "solution" which came into my head was to get rid of the loop and manually write the units in the template. But, when units change, I also need to change the template, and.. the real template structure for one unit is a bit bigger than this snippet.
Example:
<input value="{{units_1}}" />
<input value="{{units_2}}" />
And I was looking for something like:
<input value="{{'units_'+unit}}" />
Which is obviously not working and not even supposed to work this way. But, thats why I'm here right ? To raise questions.
Regards !
Try to use write getUnit function:
{{# config.units:unit }}
<input type="text" id="{{ getUnit(unit) }}" class="toTrain selectable" value="" placeholder="0" />
{{/ config }}
Component:
Ractive.extend({
init:function(){
self.set("getUnit", function (id) {
return self.get("config.units.unit_"+id);
});
}
})

struts2 s:radio cssClass and cssStyle add attribute twice

I would put my JSP file in HTML5 format.
I have a problem with the tag struts radio button;
when I put cssclass"disabled" ( or other like cssClass"red" ) , when I look the source I get twice attribute class="disabled".
But It works nice for the other struts tag.
See example below:
JSP file:
<s:radio cssClass="disabled" name="mirror.swiBlo" list="Y"/>
source:
<input type="radio" name="mirror.swiBlo" id="consultation_mirror_swiBloY" value="Y" class="disabled" class="disabled"/>
If someone has any idea to solve that.
Thanks
Actually it's a bug in struts.
In simple/radiomap.ftl setting of class and style occurs twice.
source code:
<#if parameters.cssClass??>
class="${parameters.cssClass?html}"<#rt/>
</#if>
<#if parameters.cssStyle??>
style="${parameters.cssStyle?html}"<#rt/>
</#if>
<#include "/${parameters.templateDir}/simple/css.ftl" />
And css.ftl handle class and style again.

Toggle language on the current page with JSTL or Spring MVC tag

I'm using Spring MVC LocaleChangeInterceptor to handle change of locale. I need 2 simple links to switch the current page between English and French while keeping any existing parameter of the current URL.
The solution I've came up with looks rather ugly. A c:url tag with an empty value to link to the current page and a loop to append any existing parameter except for the language parameter that I don't want to see twice. So the code is as follow.
<c:url value="" var="englishURL">
<c:forEach items="${param}" var="currentParam">
<c:if test="${currentParam.key != 'siteLanguage'}">
<c:param name="${currentParam.key}" value="${currentParam.value}"/>
</c:if>
</c:forEach>
<c:param name="siteLanguage" value="en"/>
</c:url>
<c:url value="" var="frenchURL">
<c:forEach items="${param}" var="currentParam">
<c:if test="${currentParam.key != 'siteLanguage'}">
<c:param name="${currentParam.key}" value="${currentParam.value}"/>
</c:if>
</c:forEach>
<c:param name="siteLanguage" value="fr"/>
</c:url>
English / Français
Does someone have a less ugly way to create these links? I know I can create a custom tag to avoid the duplication but I feel that's a very small improvement since I need to declare the tag and all.
Not really less ugly, but atleast some less code duplication
<c:forEach var="tuple" items="${fn:split('en,English|fr,Français|nl,Nederlands', '|')}" varStatus="status">
<c:set var="locale" value="${fn:split(tuple, ',')[0]}"/>
<c:set var="name" value="${fn:split(tuple, ',')[1]}"/>
<c:url value="" var="url">
<c:forEach items="${param}" var="currentParam">
<c:if test="${currentParam.key != 'changeLocale'}">
<c:param name="${currentParam.key}" value="${currentParam.value}"/>
</c:if>
</c:forEach>
<c:param name="changeLocale" value="${locale}"/>
</c:url>
${name}
<c:if test="${not status.last}"> / </c:if>
</c:forEach>
From your question it seems like you are appending the siteLanguage param in your url everytime. Is it correct? Why you need to do this? You can save this language change in session or cookie and then everytime you don't need to specify the siteLanguage parameter. Once the language is selected then the language parameter is stored in the session or cookie. And spring will manage the rest of the things itself.
Just specify either SessionLocaleResolver or CookieLocaleResolver along with your LocaleChangeInterceptor. It will do this for you.
Hope this helps you. Cheers.

#spring.formInput in #list iterator

<#list flowList as flow>
<#spring.formInput "flow.createDatetime" />
</#list>
flowList is arrayList.
freemarker.template.TemplateModelException: Method public org.springframework.web.servlet.support.BindStatus org.springframework.web.servlet.support.RequestContext.getBindStatus(java.lang.String) throws java.lang.IllegalStateException threw an exception when invoked on org.springframework.web.servlet.support.RequestContext#8bc713e with arguments of types [java.lang.String,]
at freemarker.ext.beans.OverloadedMethodModel.exec(OverloadedMethodModel.java:134)
at freemarker.core.MethodCall._getAsTemplateModel(MethodCall.java:93)
How can I resolve #spring.formInput in #list.
Have you tried doing an intermediate assign? I saw this problem on other StackOverflow pages, like Freemarker syntax for a form for a collection of objects (Spring 3 MVC):
<#list flowList as flow>
<#assign flowDate = flow.createDatetime />
<#spring.formInput "flowDate" />
<\#list>
The following workaround works for me, but is pretty ugly:
<#list flowList as flow>
<#assign index=flowList?seq_index_of(flow)>
<#spring.formInput "flowList[${index}].createDatetime" />
</#list>
When the above form is posted, you'll need to ensure that the flow-list is pre-populated with empty flows. Alternatively, just using Spring's AutoPopulatingList as the flow-list implementation.
For spring to bind the object, the exact reference must be provided. Hence you need to add the index in the tag. This is needed when you post the form back and want the flowlist object as request body in a controller method.
<#list flowList as flow>
<#spring.formInput "flowList[${flow_index}].createDatetime" />
</#list>
After rendering if you look at the HTML it would be like
<input type="text" id="flowList0.createDatetime" name="flowList[0].createDatetime" value="..." />

Resources