Freemarker: null value displayed - freemarker

I have this code:
<input type="text" name="zipCode" maxlength="5" value="${zipCode!}">
When zipCode = null (I use Java), the input field displayes null. Why?
Thanks!

#ddekany: you were rignt. zipCode has a String value that equals "null".

Related

Thymeleaf + Spring: get rid of the default element id

Is there any way to suppress auto-generating ID attribute for elements while using th:field in Thymeleaf (2.1.4.RELEASE)? For example, given code:
<input type="text" th:field="*{year}" />
will produce the following HTML:
<input type="text" id="year" name="year" value="" />
What I want to achieve is (no id attribute):
<input type="text" name="year" value="" />
In JSP it was as easy as setting empty id:
<form:input path="year" id="" />
but Thymeleaf just replaces this empty attribute with the default-generated one.
Ok, I have looked inside the source code of Thymeleaf (2.1.4.RELEASE) and the method responsible for setting element id in Spring dialect is org.thymeleaf.spring4.processor.attr.SpringInputGeneralFieldAttrProcessor.doProcess(...) (source on Github) that calls org.thymeleaf.spring4.processor.attr.AbstractSpringFieldAttrProcessor.computeId(...) (source on Github). If you look at computeId(...), you will see that there is no simple way to set empty id.
So we need to do it in not a simple way :) Here it is:
I created a custom dialect and defined a custom attribute noid. The markup looks like this:
<input type="text" th:field="*{year}" thex:noid="true" />
There is a great tutorial explaining how to create and use custom dialects in Thymeleaf and below is the most important part: attribute processor responsible for removing id attribute from given element.
Important things to note:
high precedence value (9999) guarantees that this processor will be executed as the last one (so no other processors will modify id after this one is executed)
modification type is set to substitution so we are completely replacing value of id element
removeAttributeIfEmpty(...) returns true, rather self-explanatory, remove attribute if empty
getModifiedAttributeValues(...) sets id to empty value and because above-mentioned method returns true, id attribute is removed
Code:
public class NoIdAttrProcessor extends AbstractAttributeModifierAttrProcessor {
public NoIdAttrProcessor() {
super("noid");
}
#Override
public int getPrecedence() {
return 9999;
}
#Override
protected ModificationType getModificationType(Arguments arguments, Element element, String attributeName, String newAttributeName) {
return ModificationType.SUBSTITUTION;
}
#Override
protected boolean removeAttributeIfEmpty(Arguments arguments, Element element, String attributeName, String newAttributeName) {
return true;
}
#Override
protected boolean recomputeProcessorsAfterExecution(Arguments arguments, Element element, String attributeName) {
return false;
}
#Override
protected Map<String, String> getModifiedAttributeValues(Arguments arguments, Element element, String attributeName) {
Map<String, String> values = new HashMap<>(1);
values.put("id", "");
return values;
}
}
If you dont want to use this in id of you input field just assign the value to only the th:name field,
<input type="text" th:name="*{year}" />
will give you output like,
<input type="text" name="2015" />
Or You can use a string at the end to make the id generate different from the name attribute like this
<input type="text" th:name="*{year}" th:id="*{year} + '-year' " />
will give you the output,
<input type="text" name="2015" id="2015-year"/>

Textfield validation in Grails framework

I am new to grails. I want to validate a text field and throw error message on the screen. Can anyone help me on this. for eg.lastName is not entered .
here is my code
Person.groovy
class Person {
String firstName
String lastName
int age
static constraints = {
lastName(nullable:false, blank:false)
}
gsp
LastName
<label for="firstName">FirstName</label>
<g:textField name="firstName" value="${person.firstName}"></g:textField><br/>
<label for="age">Age</label>
<g:textField name="age" value="${person.age}"/><br/>
<g:actionSubmit value ="save" action="save"/>
i tried the above but it is not working. do i need to make any setting for validation. I am trying this in intelliJ
your code works as it should
you are not handling any error, you should at least use something to flash a message on errors.
you might wanna try something like this:
<g:textField name="firsname" value="${person.firstname}"/>
<g:hasErrors bean="${person}" field="firstname">
<g:eachError bean="${person}" field="firstname">
<p style="color: red;"><g:message error="${it}"/></p>
</g:eachError>
</g:hasErrors>

Changing the id of an element using Prototype?

Lets say I have an element like this:
<input id="options_14_text" type="text" onchange="something()" name="options[14]" value="">
How could I change its id to options_15_text for example using Prototype?
Thanks!
You can select the element and then change its id property:
​$("options_14_text").id = "options_15_text";​​​​​​​​​​​​​​​​​​​
Or you could use writeAttribute:
​$("options_14_text").writeAttribute("id", "options_15_text");​​​​​​​​​​​​​​​​​​​​​
Here's a working example. Inspect the DOM to see that the id value has changed.

How to get data from jquery input field?

I use JQuery Datepicker and cannot get data from it.
In .html:
<p>Date: <input type="text" id="datepicker"/> </p>
In models.py:
class RecordModel(models.Model):
...
Date = models.DateField(blank = False)
...
In forms.py:
class RecordForm(forms.ModelForm):
...
#There is no Date field
...
In views.py:
def doc(request, DocName):
S = request.POST.get("Date") # error
Value of S in doc() function in views.py is "" (empty string), even if I choose date in a widget. How to get real data?
You need a name attribute to submit data.
You are looking for a POST data keyed by Date but I don't see a name=Date anywhere.
It should be:
<input type="text" id="datepicker" name="Date"/> </p>

Dynamic fielderror struts2 validation

i'm dynamically generating fields (from a hashmap):
<s:iterator value="app.fields" status="field">
<s:set name="findex" value="%{'app.fields[' + #field.index + '].id'}"/>
<s:fielderror value="%{#findex}"/>
<s:textfield name="%{#findex}" />
</s:iterator>
This method sets up the textfield ok:
<input type="text" id="saveapp_app_fields_1__id" value="[DE]Enter an ID" name="app.fields[1].id">
but not the fielderror.
I add the fielderrors manually in the validate method. but all field errors get displayed n times for each fielderror tag. wich implies that what is actually happening is that the #findex seems to evaluate to null and i'm adding n <fielderror/> tags.
I could extract the field errors manulally in the jsp but was hoping for a more elegant solution.
Thanks in advance. Michael.
I've never seen a fielderror declared in that way. Perhaps try:
<s:fielderror>
<s:param value="%{#findex}" />
</s:fielderror>

Resources