Double data output error using Freemarker and Spring mvc - spring-mvc

I'm working on a website using freemarker and spring mvc to render json data, But I found the double value such as "14.1234" will be rendered as '14,1234' when client's language has bean setted to 'French'.
I hava the value setted in the freemarker configuration files:
<xml>.....
<prop key="locale">zh_CN</prop>
<prop key="number_format">#.#####</prop>
</xml>
What's going on?

In French you use , as the decimal separator. If you are printing not for humans, but for "computer audience", then you have to write ${myDouble?c}.

Related

where is the XSD and semantics of the web.xml defined?

I am trying to find where the XSD and the semantics for the deployment descriptor web.xm file is defined in the Servlet 3.0 specification.
Alternatively, where is an authoritative description of the various supported elements and attributes that can appear inside web.xml and what the default behavior of the container is, in case some elements / attributes are absent.
This started by me wondering what is the default value for the http-only and secure elements inside session-config, e.g. as in:
<session-config>
<session-timeout>60</session-timeout>
<cookie-config>
<http-only>true</http-only>
<secure>false</secure>
</cookie-config>
</session-config>
I am reasonably certain the default values are false for both but I wanted to see where this is authoritatively specified.
Looking at the Java Servlet 3.0 spec there is no XSD. There is a sample XML file (on pg. 169) which has a schemaLocation attribute with value:
http://java.sun.com/xml/ns/j2ee/web-app_2_5.xsd
… which is broken. Googling for web-app_2_5.xsd an XSD file is found but it doesn't contain the definition of <session-config> element (I couldn't locate it even when googling for the other XSDs that that file imports).
The specification does contain graphical depictions of some elements (in the horrible late 90's style when such "visualizations" were in vogue) but this is all it contains for the session-config element:
There's no futher discussion for the cookie-config element.
I find it hard to believe that a specification does not contain the full XSD (or at least a link to it) and a detailed description of the semantics of all elements and attributes.
Am I missing something?
A list of schemas can be found at
http://www.oracle.com/webfolder/technetwork/jsc/xml/ns/javaee/index.html
(at the very beginning it is stated "Latest Version: http://xmlns.jcp.org/xml/ns/javaee/", which in turn redirects to the link I've posted first; I think the latter URL should be used as a permalink)
Then you'll find the schemas grouped by Java EE version. I think servlet 3.0 is JEE 6, so:
http://www.oracle.com/webfolder/technetwork/jsc/xml/ns/javaee/index.html#6
There you'll get the schemas:
http://www.oracle.com/webfolder/technetwork/jsc/xml/ns/javaee/web-app_3_0.xsd
http://www.oracle.com/webfolder/technetwork/jsc/xml/ns/javaee/web-common_3_0.xsd
http://www.oracle.com/webfolder/technetwork/jsc/xml/ns/javaee/web-fragment_3_0.xsd
Had problems with the schemas from oracle. The URLs were just not working, kept throwing an error.
so I switched to the jboss schema (at https://www.jboss.org/j2ee/schema/)
I replaced
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd
with
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee https://www.jboss.org/j2ee/schema/web-app_2_4.xsd"
Also make sure the version attribute on the web-app tag is set correctly (e.g. version="2.4" in this case)

unicode characters in google cloud endpoints

I'm developing a backend that should accept JSON objects and input them in the datastore if they meet certain criteria. My problem is that i get crippled content when i send json that has unicode letters . I have tried it with both the api explorer and rest client plugin for Firefox
my endpoint API method looks like this:
#ApiMethod(name = "addObj", httpMethod = "post" , path = "addObj")
public ArtObj addObj(ArtObj obj)
throws OAuthRequestException, IOException,IllegalArgumentException
{
}
and my Json looks like the following
{
someText:"محتوى عربى",
someEnglish : "English content"
}
According to many articles i did add this in my appengine-web.xml
<system-properties>
<property name="java.util.logging.config.file" value="WEB-INF/logging.properties"/>
<property name="file.encoding" value="UTF-8" />
<property name="DEFAULT_ENCODING" value="UTF-8" />
</system-properties>
but all i receive is a parsed POJO but with garbled unicode letters and perfect english .
I've found an answer to my question . I'm using windows and so the environment variable
JAVA_TOOL_OPTIONS has to be set to -Dfile.encoding=UTF8 to set the default encoding for the JVM . now i get correct JSON .
Thanks
I face the same problem to insert entity in Thai, my solutions is to open project properties window on Eclipse, under "Resource/Text file encoding" click "Other" and select "UTF-8" and it's work.

What should I use as the XLIFF original file in Symfony 2?

In Symfony 2's documentation regarding translations all of the XLIFF examples appear to use file.ext as the original file attribute. From XLIFF 1.2's documentation:
Original file - The original attribute specifies the name of the original file from which the contents of a element has
been extracted.
In Symfony 2's case I don't belief we're actually extracting any contents for translation, but the original attribute is required. The usage of file.ext is never explained in Symfony 2's documentation, whether it is simply a place holder and ignored, or whether it needs to point to an actual file. My best guess is that it is ignored by Symfony 2, but I haven't had a chance to do any tests or dig around in the code.
Second question: Would it be appropriate to specify a default set of translations, e.g. messages.default.yml and use this as the original file from which the XLIFF translations are derived?
XLIFF seems a little bit like overkill when it comes to translations for use with web applications...
Although original attribute of <file> is required in Xliff 1.2 spec and schema, you can ignore it and have a dummy string as its value so that your file passes schema validation.
One of the common uses of this property is when the strings from multiple documents (.properties files, gettext files, etc.) are extracted and reformatted as Xliff trans-units so that the strings from each individual file are placed under a respective individual <file> tag. original attribute can be used to keep the original filenames in an Xliff file with multiple <file> tags:
<?xml version="1.0" encoding="utf-8" ?>
<xliff version="1.1" xml:lang='en'>
<file source-language='en' target-language='de' datatype="plaintext" original="Sample.po">
...
<body>
<trans-unit id="1" restype="button" resname="IDC_TITLE">
<source>Title</source>
</trans-unit>
<trans-unit id="2" restype="label" resname="IDC_STATIC">
<source>&Path:</source>
</trans-unit>
...
</file>
<file source-language='en' target-language='de' datatype="plaintext" original="Sample.properties">
<trans-unit id="1" restype="label" resname="IDC_LABEL">
...
</body>
</file>
</xliff>
I agree that using Xliff for localizing small web applications can often be a bit of an overkill, but this format is arguably the only industry standard file format for localization data interchange at the moment. This makes it very popular with large scale localization implementation either simple source/target-structured files or heavily customized and tailored ones like those used in Translation Management Systems like SDL Idiom Worldserver.
Additionally, there is compatibility between Xliff and other standard file formats of OAXAL family.

Load freemarker template from classpath

I have a Resource in aSpring MVCapplication loaded from the classpath.
<bean id="myController" class="com.MyController">
<property name="myTemplate" value="classpath:myTemplate.txt"/>
</bean>
And I am trying to load it as a freemarker Template using this code:
private Resource myTemplate;
...
Configuration cfg = new Configuration();
cfg.setClassForTemplateLoading(this.getClass(), "/");
Template tpl = cfg.getTemplate(myResource.getFilename());
But I keep ending up in: java.io.FileNotFoundException: Template classpath:myTemplate.txt not found.
I tried implementing what wassuggested here however it doesn't seem to help.
The only hack I could findso far was to remove the "classpath: prefix from the filename String but I prefer not to do it
Any ideas...?
So what you are saying is that you don't want to remove "class:" from the template name, and according to this question you don't want to teach FreeMarker understanding it via a custom TemplateLoader either. I mean, if you bar these, what else could possibly solve this? I can only advice you to do the last; implement a custom TemplateLoader (either one that just removes the "class:" prefix then delegates to ClassTemplateLoader, or, even better, one that just delegates to a Spring ResourceLoader). That's how you configure FreeMarker to do what you want. It's not something extreme to do, implementing your own TemplateLoader.
Update: It might by useful to know that by default there's a mismatch between the FreeMarker template name syntax and Spring's resource name syntax. According the Spring syntax, you can write "classpath:foo.ftl" or "classpath:/foo.ftl". But FreeMarker assumes that the scheme part always ends with ://, and a lonely : or :/ is nothing special. So all these resource paths will be seen as relative paths, and so the current template directory will be prepended before them before the actual template resolution. To solve this, since FreeMarker 2.3.22, you can use Configuration.setTemplateNameFormat(TemplateNameFormat.DEFAULT_2_4_0) (template_name_format=DEFAULT_2_4_0 in Properties), which does consider : as scheme separator.

Character encoding issue with Spring MVC and HTML form

I'm working with spring mvc. I've set up a web form that has two simple text inputs. On controller, I use #ModelAttribute to let spring build the bean from the web form.
The problem comes when user puts on those text fields specials characters, like 酒店 and this kind of stuff, spring doesn't read it as utf-8, and they become the usual bad-encoded string.
I've checked web.xml and there's the utf-8 encoding filter, all pages are marqued as utf-8 and browser is sending right charset headers. Any idea on what's going on?
You may want to check this out:
http://forum.springsource.org/showthread.php?81858-ResponseBody-and-UTF-8
The short of it is that if you are using annotated methods then the messageconverter being used has a default character set. You can change this setting in your web.xml by setting the supported media types.
However, if your service doesn't like that media type, you may get a 406 error. You can create your own string message converter and set the default encoding, but there is no easy way with the built in HttpStringMessageConverter.
Alternately you can re-encode a string to a different character set:
String newresponse = new String(responseString.getBytes("ISO-8859-1"), "UTF-8");
You may also want to check out the related question here: How to get UTF-8 working in Java webapps?
the solution is simple by add produces = "text/plain;charset=UTF-8" to request mapping you can force spring mvc to encode the returned text.

Resources