I have the .ftl page where i put values from locale property files like so :
<div id="hiddenDiv">
<#spring.message "${MyKey}.Popup"/>
</div>
The problem is that in some property files, i have the key MyKeyValue.Popup, but in others i don't. (namely the en.properties file)
And when spring tries to generate the model in english, it doesn't find that key in en.properties file.
Is there a way to handle that exception ? I have to handle it. I can't just put all of the possible keys into that property file. There are too many of them.
Please help.
The solution is to use this macro instead of the #spring.message :
<#spring.messageText "code", "Default message"/>
This doesn't throw an exception because it has a default value.
Hope this helps someone.
Related
I want to use custom error messages for validation constraints on dozens of fields in my project.
I do not want to set the message on every one of these, because that would be a blatant violation of DRY. Repeating the same string in every declaration like: #NotNull(message="custom msg") would mean that if I decide to change the message in the future I'd have to hunt them all down replace them, but even worse I might use the wrong string on some of them and be inconsistent.
How do you deal with this?
Is the best option really to extend every symfony stock constraint and set my default there, and use my own custom annotation class?
Please note that using the translator is not an option for me, so I am looking for a solution that does not include the symfony translation component.
Thanks a lot for the help in advance.
Assuming you are working with English as the app's language, and you've configured translator: { fallback: en }, you can override these constraint messages universally. Start by creating this file: app/Resources/translations/validators.en.yml
And use the following translation format:
This value should not be blank.: Your custom message here
Whatever the standard message is.: Another custom message
This also works for any other language setting, assuming you've made a validators.lang.yml for it!
You can also place this file in your bundle directory under Resources/translations, and a few other places.
You can read more about this here!
In my JSP page, I am iterating over a list of objects using the c:forEach tag. Each object in this iteration has a name field, and I am trying to use that name field to reference other objects that I have added to my Spring model. However, when I do this, I am getting an exception saying the property of the second object cannot be found on a String. Let me illustrate with a simplified example.
<c:forEach items="${companyList}" var="company" varStatus="num">
<c:set var="monthStats" value="${company.name}_month" />
<tr>
<td>Properties This Month</td>
<td>${monthStats.properties}</td>
</tr>
....
</c:forEach>
All of the objects are located inside a Spring Model object and have been set like this
model.addAttribute(company.getName() +"_month", currentMonthStats);
I have also tried throwing out the <c:set tag and trying
${(company.name + "_month").properties}
as well as
${${company.name + "_month"}.properties}.
Those havent worked either. Also, I wasn't quite sure how to word my title, so if it could worded better let me know so that other people can find this if they have the same issue.
You're basically asking for double-evaluation, and it doesn't work like that.
I'd modify the data delivered to the view layer to be something a little less odd, so you can access it in a non-pathological manner, like by putting the month stats into a map accessed by the company name (or id), or directly associated them in a DTO, etc.
A related problem that is specific to single field has been solved here. But how to customize a collection field's conversion error message?
Here is an example:
On a jsp page, I have a field in Collection type:
...
<s:iterator value="items" status="m">
<s:hidden name="selitmems[%{#m.index}].id" value="%{id}"/>
<s:textfield name="selitmems[%{#m.index}].quant" size="10"/>
</s:iterator>
The items' type is: List<Item>; the selitems' type is List<SelItem>.
I want selitmems[].quant property to be an integer type. If a string like "abc" is filled in for the first item by an end user, the default error message is:
Invalid field value for field "selitmems[0].quant".
The above message is not what i want. In my case, I would prefer to generalize the error message as follows regardless of the specific selected item:
Please input integers for the items.
Of course it would be great if the error message can vary according to the specific item:
Please input an integer for the first item.
I have tried to add some keys like "selitmems[0].quant" or "selitmems" in the properties file, but can't get the result. Is there way to customize the error message for a collection field in struts2 when I can still reuse the built-in type conversion functions?
Try using the "label" attribute. You can play some... interesting games with this, such as (untested, but close):
label="%{getText('selitem.quantity', { #m.index })}"
The property file would contain:
selitem.quantity=Item #{0}
You can change the conversion error message as described here, although this may not be precisely what you want to do.
(I've been known to remove the conversion interceptor altogether and let either the default converters or custom converters handle conversion errors when a bad conversion also fails the field's "real" validation.)
Mm hmm... you can play some crazy games with OGNL and substituion.
Put something like this in your properties file.
invalid.fieldvalue.selitmems.quant = Please input integers for the items.
Then selitmems[0].quant, selitmems[1].quant, selitmems[2].quant conversion errors all return the same message.
I have a need to do something like this:
bob.common=goat
bob.have=I have a {bob.common}!
bob.want=I want a {bob.common}!
bob.need=I need a {bob.common}!
Is this sort of thing possible? I know this seems silly, but being able to re-use a common piece is a need here, and we really can't (don't want to) do it programmatically.
We're already using numbered arguments in our properties, but we would like to be able to pass in a reference to another property.
I suggest to do this :
bob.common=goat
bob.have=I have a {0}!
bob.want=I want a {0}!
bob.need=I need a {0}!
Then in your page :
<spring:message code="bob.common" var="animal"/>
<spring:message code="bob.have" arguments="${animal}"/>
<spring:message code="bob.want" arguments="${animal}"/>
<spring:message code="bob.need" arguments="${animal}"/>
The way you want to do would be too strict, if you want to change your animal for example.
According to the Spring changelog, this has been supported since 2.5.3:
PropertyPlaceholderConfigurer supports nested keys in placeholder keys as well (e.g. "${db.${environment}}")
So for your example case, you should be able to use:
bob.have=I have a ${bob.common}!
and the PropertyPlaceholderConfigurer should recognise the "nested key" and resolve that correctly.
I am trying to add two localization resource files into my MVC3 App_GlobalResources.
I add Global.resx and then Gloabl.fr.resx. However, only the first one generates code in .designer.cs. The second one just generate an empty file.
Can anybody help me out?
Thanks a lot.
That is how its supposed to work. The .designer.cs class is a strongly typed class so that you can type.
#Global.mystring and it will return a localised (depending on the UICulture) string.
The designer file doesn't actually contain the localised strings, it just contains a bunch of properties which (in turn) return the localised string.. this is why you wouldn't need more than one class.
Perhaps you are trying to find a way of retrieving the resources for different cultures e.g. fr?
You need to set the UICulture to "fr". Either manually or by setting the following element in the web config:
<globalization culture="auto" uiCulture="auto"/>
This would do it automatically based on your browser settings