how to add parameterized message in freemarker / spring messages - spring

I'm using freemarker as view renderer in a Spring application. Is there any way to parameterise messages from the spring.ftl library?
In java we'd use the message source getMessageSource("code.key", new String[]{"param1", "param2"}, null, null) and it interpolates the string.
But there doesn't appear to be an option for this with spring.messageText or spring.message.
Am I missing something? Is there a way to simulate it? I know I can add in the controller, but all the other messages use the macros, and it seems like a bit of a workaround for something that should be easy...

Freemarker can be used together with any taglibrary.
Have you tried using the Spring taglib with the "message" tag?
(<%# taglib prefix="spring" uri="http://www.springframework.org/tags" %>)
Please see following links for additional details:
http://freemarker.org/docs/pgui_misc_servlet.html#autoid_56
http://www.mkyong.com/spring-mvc/spring-mvc-internationalization-example/
Please let us know if this works fine..

Related

How to validate tags in xhtml against a facelet-taglib

What I'm trying to do is write a maven plugin that will validate xhtml files so if any of the tags have attributes that don't belong, are misspelled or if there are any other kind of validation errors, the build will fail.
We are using Primefaces 6.0 and we'd like to include validation of those tags. Problem is that within the Primefaces jar file I can only find a facelet-taglib file (for Primefaces 6.0 in folder META-INF/primefaces-p.taglib.xml) and I don't know how to validate an xhtml file against a facelet-taglib file.
I know how to write Java code to validate using schema files but I haven't had any luck finding any Java examples of validating tags in my xhtml file against a facelet-taglib, specifically the Primefaces taglib xml taglib file.
I have tried loading the taglib file like below...
SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
InputStream stream = ..[file primefaces-p.taglib.xml]..;
Schema schema = schemaFactory.newSchema(new StreamSource(stream));
But I just get the following error:
org.xml.sax.SAXParseException; lineNumber: 7; columnNumber: 39; s4s-elt-character: Non-whitespace characters are not allowed in schema elements other than 'xs:appinfo' and 'xs:documentation'. Saw 'http://primefaces.org/ui'.
I figured maybe I'm using the wrong XMLConstants variable, but I'm not sure which one to use, or if this would even work.
One other note, I am aware of the existing XML Maven Plugin, but I could not get it to work with the primefaces facelet-taglib. If someone knows how to get it to work with the taglib, please let us know!
Any help would be greatly appreciated!
Taglibs are not schema's so that just won't work. But a descent IDE will use the taglibs for you to do similar validations but in a different way

Questions about spring reference statement and application context

I have two primary doubts about spring:
When we get a bean from context, should we must use ways like :
context = new ClassPathXmlApplicationContext("applicationContext.xml");
Should it be called only via this explicit way?
I was thinking about that when the website start, this spring config file should also be read.
When we use tags of spring or other packages like jstl, etc. We should give the reference statement first like :
<%# taglib prefix="sf" uri="http://www.springframework.org/tags/form" %>
I was so curious about that, when the page is parsing, the web server do download this uri and use it in this page? But without internet, it seems also work. How it works, or why we must add this in the head part of a file.
It is indeed possible to get a bean from the Spring context that way, but it might be considered to be defeating the purpose of having a bean container with dependency injection. The goal of the container would be to provide the beans to your classes, so they don't need to instantiate or grab them directly from the context. See this short tutorial for a simple example of (xml-based) DI.
The uri attribute in a <%taglib> tag can be resolved locally if the tld is in your classpath under a certain specific path. See With JSP, does the taglib URI mean that my site is reliant on the URI resolving?.

Where does fmt:message get the locale from?

I need to know how the fmt:message tag of the JSTL gets the locale from. i dont want to use fmt:setLocale. I want to customize the locale resolver that fmt:message uses. I know implementing the Spring's LocaleResolver works for spring message source. Is there anything similar for the fmt:message tag?
Thanks in Advance.

Spring taglib in uiBinder xml-file

How can I put my taglib prefix correctly in my name.ui.xml file, is it even possible ?
In an html/jsp file it normally would work with
<%#taglib uri="http://www.springframework.org/tags" prefix="spring"%>
But thats not the case with uiBinder xml files. Is there a way or an alternative ? I'm trying to ensure multilingualism in my application. Thx for any help :)
You do understand that JSP is processed by your server, and UiBinder is processed by the GWT compiler to produce JS for the browser (client), right?
If you want to internationalize your GWT application, there are tools for that: http://www.gwtproject.org/doc/latest/doc/latest/DevGuideI18n.html

Jasper in Jetty 6 throws exception for JSTL tag

I'm trying to run an application in jetty that runs fine in Tomcat 5.5. The app uses servlet 2.4 and JSP 2.0.
Jetty/Jasper is throwing this exception:
org.apache.jasper.JasperException: /WEB-INF/tiles/layout/main.jsp(85,55) PWC6340: According to the TLD, rtexprvalue is true, and deferred-value is specified for the attribute items of the tag handler org.apache.taglibs.standard.tag.rt.core.ForTokensTag, but the argument for the setter method is not a java.lang.Object
One odd thing, I can't find the TLD anywhere. It seems to be obtaining it by magic that I don't understand. Is it possibly getting a wrong TLD?
It's also hard to tell from where it's loading org.apache.taglibs.standard.tag.rt.core.ForTokensTag. Eclipse doesn't find it in the load path of the project.
Any hints welcome ...
Jetty includes their own JSTL library and there is no need to include jakrta taglib's standard and core jars.
If you do put jakartat taglib's jars into your web application then there is a conflict in the forTokens tag that causes this error while other tags work well. I suggest either remove the jakarta taglib implementation from your web app and rely on Jetty's, or stop using forTokens.
#Guss is correct, the only way out seems to be to avoid the use of c:forTokens.
example alternative to c:forTokens using c:forEach:
<%#taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%#taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%>
<c:forTokens items="${input}" delims="," var="i">
<!-- do stuff with ${i} -->
</c:forTokens>
<c:forEach items="${fn:split(input,',')}" var="i">
<!-- do stuff with ${i} -->
</c:forEach>

Resources