JSP Global Variable using Spring - spring

I have a date format at I wish to set globally for my jsp fmt tag to use.
May I know what is right approach to perform this?
I will wish to configure this value in future
pattern = "dd-MM-yyyy kkm"
I have read the following:
How do I create a global JSP variable that I can access across multiple pages or inside frames/iframes?

I think using applicationContext is right approach. You can set your date format at your "welcomepage.jsp",
String pattern = "dd-MM-yyyy kkm";
application.setAttribute("ApplicationPattern",pattern);
get and use the same format wherever required.
String pattern2 =(String)getServletContext().getAttribute("ApplicationPattern");

If you want to set the default data-format pattern for entire app, you could use ServletContext.setAttribute(), but you need a place to do this work, set it in a jsp page or use a initialize filter for your app.
Or you create a Utlitiy class with a static method(field) definition to get the default pattern, then from your fmt
tag use a method call to reference the default pattern

Related

Thymeleaf - i18n get back the template by locale and custom parameter

Circumstances
I'm using Spring Boot with Thymeleaf for populating my HTML template files and get back the result as a String. For that I used SpringTemplateEngine.
The code looks like this
Context context = new Context();
context.setVariables(myProperties);
return templateEngine.process(htmlTemplateName, context);
The problem
For i18n I have a different approach. My thymeleaf HTML template namings are the following (similar to Freemaker templating):
templatename_default_FOO.html
templatename_en_EN_FOO.html
templatename_de_DE_FOO.html
templatename_default_ANOTHER.html
templatename_en_EN_ANOTHER.html
templatename_de_DE_ANOTHER.html
The convention is:
templatename - is the name of the template
default/en_EN/de_DE - is the locale which I get from an API - The locale may not exists in the templates, in this case I want to use the default template with the matching user parameter
FOO/ANOTHER - different parameter values which the users sets - they exists in atleast one template's name (the default should contain it)
Previously I used ResourceBundles to get the templates by locale if existed, otherwise the default value was automatically chosen. However with Thymeleaf implementation I don't know how to implement the same mechanics, because currently I get the template by providing the full name. If I add a locale which not exists I don't get back the default template.
Question
I know that i18n in Thymeleaf is done through language.properties, but in my case I would need a function where I provide the template name, the locale and the user parameter and I get back the specified HTML file if exists and if not the default HTML file with the matching user parameter.
Something similar:
public String getTemplate(Map<String, Object> myProperties, String templateName, Locale locale, String userParameter) {
Context context = new Context();
context.setVariables(myProperties);
return templateEngine.process(templateName, locale, userParameter context);
}
Is that possible somehow or I should use language.properties? Or I should write custom logic to check if the template not exists go with the default one?
Thanks in advance

Accessing the parameter by dynamic name

How to access the parameter #{...} in Process configuration field if its name needs to be changed dynamically (for example, the name of this parameter is contained in some FlowFile attribute OR the name generating using expression language).
Illustrative example – for the LogMessage process, I have prepared several parameters in (msg1, msg2, msg3 etc.) that I would like to output depending on the attribute numofmessage
You can do it using evaluateELString()
${literal('{dynamic_${name}}'):prepend('#'):evaluateELString()}
Currently you cannot use EL inside of a parameter reference. If you put something like #{${abc}} it will look for a parameter named ${abc} and won't find one so it will be invalid.
You can only do the reverse, use parameter inside of EL, for example ${ #{abc}:replace('xxx', 'zzz') }.
In general, parameters are meant to be a better version/replacement for the existing variables functionality, but in this case variables may work better for you since variables are referenced through EL.

Is there a way to replace a string in a flowfile using a regex stored in a variable?

I'm using Nifi, and it looks like ReplaceText doesn't work properly. I want to perform a text search and replace with these criteria:
I want to store a regex of my search string in a variable, not directly in a processor's property.
I want to store a regex of my replacement string in a variable, not directly in a processor's property.
I want to be able to only change the variables in the resulting template, so I can share it with other users via the Nifi registry.
I've tried using ExtractText to search for something, store that in an attribute, and then use the value of that attribute, but Nifi Expression Language scope is listed as unsupported for ExtractText user added properties. ReplaceText works fine if you want to put the regex in the ReplaceText property, but I don't, since I'm sharing the template via the registry and any time someone changes that property for their own search text, there will be a new version in the registry.

Spring MVC RequestMapping with i18n path parts

What are the approaches to map i18n translated url paths?
For example lets say we talk about the follwing url (for locale en):
www.foo.tld/car/manufacturer
In german (de) this url would be
www.foo.tld/auto/hersteller
What i know about Controller RequestMapping i could use severel values to map these url's for one method like
#GetMapping(value={"/car/manufacturer/", "/auto/hersteller/"})
More seo optimized would be probably something with the current locale in the path like
#GetMapping(value={"/en/car/manufacturer/", "/de/auto/hersteller/"})
...but i dont want to start a discussion what would be the best uri seo wise.
This isn't so bad if to use only a few Locales/Languages but i would like to make this somehow dynamic.
Currently im using messages_xx.properties to map url path parts for generating urls in my application, like:
messages.properties
uri.car=car
uri.manufacturer=manufacturer
messages_de.properties
uri.car=auto
uri.manufacturer=hersteller
Im using them already for building links repecting the user locale which works fine.
What im searching now for is a elegant, less error prune way to map these urls in my controller. If i would change for example a value for the key uri.car and would have a static RequestMapping in my controller like in the example above i also need to change it there (if i dont forget!).
Also if i would like to add support for another language i would need to search in all controller and check if i need to add another value mapping.
Is there a smarter way how to map i18n path parts in Spring controllers, ideally respecting a request locale and resolve the path string with the help of messages_xx.properties?
Or would be a filter the way to go extracting path parts according to the requested locale and use internally only one language for mapping urls?
Supose that you have an EN message.properties and a DE message.properties with the following property:
url.car=/car/manufacturer/ in the EN
url.car=/auto/hersteller/ in the DE
In your #Controller you can get easily this properties configuring your messageSource and using it to get the properties:
Inject your configured MessageSource to allow Spring resolve the messages:
#Autowired
private MessageSource messageSource;
Then you can get all the properties from your message.properties file:
String url= messageSource.getMessage("url.car", put_here_your_locale);

Get Thymeleaf #{} expression to append locale

My context path is / and I'm adding locales directly as part of the path: /de/index.html.
Now I'm facing the problem that th:href="#{/login.html}" will resolve to /login.html instead of /de/login.html.
I already tried making a Filter and an Interceptor like they did it here: https://stackoverflow.com/a/23847484/1163457
But it still won't append de/ after the context path.
Writing my own dialect and attribute processors would be a solution, but isn't there any better one?
Why not expose a model attribute for the locale (e.g. curLocale) and redefine all your urls like
th:href="#{/${curLocale}/login.html}"
Thymeleaf allows other expressions inside url expressions themselves.
Locale information is easily accessible either as a method parameter or by calling RequestContext.getLocale()
I found a clean and good solution myself after hours of step debugging:
https://stackoverflow.com/a/60103777/1163457

Resources