How to inject maven properties in Freemarker template? - spring

I need to use the value of "config.enableInstitutions" property in pom.xml to be used in Fremarker template as the value of a hidden input.
Pom.xml
<properties>
<config.enableInstitutions>true</config.enableInstitutions>
</properties>
myfreemarker.ftl
<body>
<input type="hidden" value="<< value of config.enableInstitutions property here >>"/>
How do I use that property in freemarker template?

Related

Building SOA composite application using maven

Using SOA suite 11, trying building source code (composite.xml with configuration file) in SOA composite application into a jar file using maven.
Can anyone help to guide me making POM.xml for the same. i am using "Apache-ANT-Plugin" in my pom file.
http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
This POM relates to this SOA Composite, i.e. the one in this same directory.
There is another POM in the SOA Application directory which handles
the whole SOA Application, which may contain additional projects.
-->
4.0.0
test1
HelloWrold
1.0-SNAPSHOT
sar
<!--
The parent points to the common SOA parent POM. That is a special POM that is
shipped by Oracle as a point of customization (only). You can add default values
for properties like serverUrl, etc. to the SOA common parent POM, so that you
do not have to specify them over and over in every project POM.
-->
<parent>
<groupId>com.oracle.soa</groupId>
<artifactId>sar-common</artifactId>
<version>12.1.3-0-0</version>
</parent>
<properties>
<!-- These parameters are used by the compile goal -->
<scac.input.dir>${project.basedir}\SOA/</scac.input.dir>
<scac.output.dir>${project.basedir}/target</scac.output.dir>
<scac.input>${scac.input.dir}/composite.xml</scac.input>
<scac.output>${scac.output.dir}/out.xml</scac.output>
<scac.error>${scac.output.dir}/error.txt</scac.error>
<scac.displayLevel>1</scac.displayLevel>
<!-- if you are using a config plan, uncomment the following line and update to point
to your config plan -->
<!--<configplan>${scac.input.dir}/configplan.xml</configplan>-->
<!-- These parameters are used by the deploy and undeploy goals -->
<composite.name>${project.artifactId}</composite.name>
<composite.revision>1.0</composite.revision>
<composite.partition>default</composite.partition>
<serverUrl>${oracleServerUrl}</serverUrl>
<user>${oracleUsername}</user>
<password>${oraclePassword}</password>
<overwrite>true</overwrite>
<forceDefault>true</forceDefault>
<regenerateRulebase>false</regenerateRulebase>
<keepInstancesOnRedeploy>false</keepInstancesOnRedeploy>
<!-- These parameters are used by the test goal
if you are using the sca-test (test) goal, you need to uncomment the following
line and point it to your jndi.properties file. -->
<!--<jndi.properties.input>UNDEFINED</jndi.properties.input>-->
<scatest.result>${scac.output.dir}/testResult</scatest.result>
<!-- input is the name of the composite to run test suties against -->
<input>${project.artifactId}</input>
<!--<scac.ant.buildfile>${env.MW_HOME}/soa/bin/ant-sca-compile.xml</scac.ant.buildfile>
<sca.ant.testfile>${env.MW_HOME}/soa/bin/ant-sca-test.xml</sca.ant.testfile>
-->
</properties>
<build>
<plugins>
<plugin>
<groupId>com.oracle.soa.plugin</groupId>
<artifactId>oracle-soa-plugin</artifactId>
<version>12.1.3-0-0</version>
<configuration>
<compositeName>${project.artifactId}</compositeName>
<composite>${scac.input}</composite>
<sarLocation>${scac.output.dir}/sca_${project.artifactId}_rev${composite.revision}.jar</sarLocation>
<serverUrl>${serverUrl}</serverUrl>
<user>${user}</user>
<password>${password}</password>
<!-- Note: compositeRevision is needed to package, revision is needed to undeploy -->
<compositeRevision>${composite.revision}</compositeRevision>
<revision>${composite.revision}</revision>
<scacInputDir>${scac.input.dir}</scacInputDir>
<input>${input}</input>
</configuration>
<!-- extensions=true is needed to use the custom sar packaging type -->
<extensions>true</extensions>
</plugin>
</plugins>
</build>

display application version in title using thymeleaf and springboot

I want to display in my htm page the version of my webapp, using something like this (thymeleaf inside) :
<h4 th:text="${version}">Version</h4>
The data is well set in the pom.xml :
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>fr.test.navig</groupId>
<artifactId>navigo</artifactId>
<version>2.0.3-SNAPSHOT</version>
...
<!-- Package as an executable jar -->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>Application</mainClass>
<addDefaultImplementationEntries>
true
</addDefaultImplementationEntries>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
and I can see it in the MANIFEST.MF (which is in the generated jar under META-INF) :
Implementation-Version: 2.0.3-SNAPSHOT
I've tried to get the appplication version in the controller and set it in a ModuleAttribute :
#ModelAttribute("version")
public String getVersion() {
logger.info("ModelAttribute to get application version");
return getClass().getPackage().getImplementationVersion();
}
But getClass().getPackage().getImplementationVersion() value is null. Indeed the package implementationVersion is not the implementation Version of the application by default.
I know I'm late but Patrick's answer and Spring docs greatly helps in this matter.
1. If your pom.xml use spring-boot-starter-parent as parent, you can use #project.version# to get version (and any other Maven properties) in your application.properties file. According to Spring docs:
You can automatically expand properties from the Maven project using
resource filtering. If you use the spring-boot-starter-parent you
can
then refer to your Maven ‘project properties’ via #..# placeholders
Maven pom.xml:
<groupId>com.foo</groupId>
<artifactId>bar</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>Foo</name>
<description>Bar</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.4.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
Spring application.properties:
foo.app.version=#project.version#
2. Then a class annotated with #ControllerAdvice can be used to inject version as model attribute.
#ControllerAdvice
public class ControllerAdvice {
#Value("${foo.app.version}")
private String applicationVersion;
#ModelAttribute("applicationVersion")
public String getApplicationVersion() {
return applicationVersion;
}
}
3. Finally this model attribute can be accessed by Thymeleaf as any other.
<th:block th:text="${applicationVersion}"></th:block>
Hope this helps!
Here is the simplest way I've found :
In my controller :
#ModelAttribute("version")
public String getVersion() throws IOException {
logger.info("ModelAttribute to get application version");
Manifest manif = new Manifest(
Application.class.getResourceAsStream("/META-INF/MANIFEST.MF"));
String version = (String) manif.getMainAttributes().get(
Attributes.Name.IMPLEMENTATION_VERSION);
return version;
}
In my htm page :
<h4 th:text="${version}">Version</h4>
You need to configure resource plugin to activate filtering on the file that need to be enriched with properties coming from your POM file.
In the generated war, the version (in fact ${project.version}) will be hardcoded to your POM version.

UTF-8 encoding with Spring Boot 1.1.1

How can I "say" Spring Boot to use the UTF-8 encoding, to show and save German umlauts correctly?
We are programming a Java-Webapplication using Sping-Boot 1.1.1 (Release) and as webserver a TomCat7 or Jetty. The database is postgresql or h2 for testing.
Edit:
I tried it with the properties file (thanks for the answer), but no changes are visible.
The database is also UTF-8...
Especially the problem comes, when we send a POST-Request to the Webserver.
The Spring-Request-Handler gets already the broken encoded values.
In the following you can see a part of the code:
(It shows a snippet of the Thymeleaf-Template)
<form accept-charset="utf-8" method="post">
<div class="row">
<fieldset th:object="${model}">
<!-- CSRF token -->
<th:block th:replace="makros :: csrf" />
<div class="col-sm-4 form-group" >
<label for="firstname" th:text="#{edit_user.first_name}">Given Name</label>
<input class="form-control required" type="text" required="required" id="firstname" name="firstname" th:field="*{firstName}" />
</div>
<div class="col-sm-4 form-group">
<label for="firstname" th:text="#{edit_user.last_name}">Family Name</label>
<input class="form-control required" type="text" required="required" id="lastname" name="lastname" th:field="*{lastName}" />
</div>
</fieldset>
</div>
</form>
And this is the request handler for that:
#RequestMapping(method = RequestMethod.POST)
public String handleUserUpdate(#ModelAttribute(MODEL) UpdateUserCommand command) {
//here we cut the broken encoded values
}
Greetings
Stef
This helped:
spring.datasource.url=jdbc:mysql://localhost:3306/securitydb?useUnicode=yes&characterEncoding=UTF-8
What is wrongly encoded? The request or the response? server.tomcat.uri-encoding is switching the URI decoding to UTF-8 (this is already the case for Jetty).
But that does not do anything for the request body. By default, Spring MVC decodes that with ISO-8859-1 (that is the default per the servlet spec). You need to specify a body encoding in your request if you want it to be decoded using UTF-8. Most users are actually using the CharacterEncodingFilter to achieve the same thing (and ensure consistency).
If that fixes your issue, watch out for #1182 that is meant to provide an auto-configuration for that.
The latest should be UTF-8 by default I think? See docs on server.tomcat.uri-encoding: http://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#common-application-properties. OTOH it might depend on where you need the encoding to happen (Spring Boot knows nothing about your database server encoding for instance).
You can set UTF-8 as default encoding for spring-boot-maven-plugin.
I create 2 maven profile to run spring boot in UTF-8 and set active spring profile via maven profile:
<profiles>
<profile>
<id>dev</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<jvmArguments>-Dfile.encoding=UTF8 -Dspring.profiles.active="dev"</jvmArguments>
</configuration>
</plugin>
</plugins>
</build>
</profile>
<profile>
<id>prod</id>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<jvmArguments>-Dfile.encoding=UTF8</jvmArguments>
</configuration>
</plugin>
</plugins>
</build>
</profile>
</profiles>
And run
mvn spring-boot:run for dev profile (application-dev.properties) and
mvn -P prod spring-boot:run for application.properties

Maven properties as environment variables

Is it possible to reference maven properties (artifactId, groupId, etc) outside of the pomfile? I am looking to specify the project's artifactId in my log4j file, and it would be nice to configure log4j.properties as follows:
<appender name="file" class="org.apache.log4j.RollingFileAppender">
<param name="File"
value="${artifactId}.log" />
</appender>
If log4j.properties is a resource which will be filtered during maven build, then this is possible.
To elaborate, if log4j.properties is placed in src/main/resources and filtering is enabled for the resources, then ${project.artifactId} will be replaced by maven during build with the artifact value.
Outside this use case, the property value will not be available automatically.

Spring properties file as xml

I have properties file config.properties where are stored some application wide properties. And I have imported it using property placeholder:
<context:property-placeholder location="classpath:/config.properties" />
I need to store properties in XML file to pass some XML schema validations.
My question is how to import XML file as properties file in spring,?
Thanks,
Arsen
PropertyPlaceholderConfigurer already supports xml property files via the DefaultPropertiesPersister
The xml file format for the properties is as below.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
<entry key="key1">Value 1</entry>
<entry key="key2">Value 2</entry>
</properties>
you can use
<context:property-placeholder
location="classpath:/com/myProject/spring_prop.xml" />
<bean id="bean" class="org.MyBean">
<property name="key1" value="${key1}" />
</bean>
In addition to the other answer here, I have also seen xml properties loaded directly as named properties files:
The spring file contains:
<util:properties id="myXmlProps" location="classpath:/com/myProject/spring_prop.xml" />
This can then be accessed via springs expression language as:
"#{myXmlProps['key1']}"
And injected into Strings in classes with:
#Value("#{myXmlProps['key1']}")
private String aValueForKey1;

Resources