How to use Spring messages in EJS template? - spring

I consider moving from Thymeleaf templating to EJS templating in my Spring Boot application (there is a need to execute some javascript code on server side). I've successfully configured everything and created my first view using few online examples:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<title>Hello</title>
</head>
<body>
Hello <%= username %>
</body>
</html>
This works OK when I pass String variable named "username" from controller to view. However, I would like my view to use messages from static file "/resources/i18n/messages.properties". In thymeleaf it is widely used, simple and possible by:
th:text="#{messages.hellomessage}"
How to achieve the same result using EJS?

You'll have to create a new ScriptTemplateViewResolver and also create a new ScriptTemplateConfigurer leveraging the Nashorn support as engine.
Take a look at this tutorial for an example.

Related

CSS files not being used when fetched via localhost as resource

I have a local server running on Tomcat.
There is currently a single landing page, which is some HTML with a simple CSS style.
My problem is, when I try to send the CSS file as a resource from my JAX-RS server, it shows up in the browser, however, it is not used by the browser to style the HTML.
Here is how it looks:
As you can see, the CSS is there, it's really simple, but it doesn't change the background.
Now, I tried to do the simplest version possible, where I just put the HTML and CSS in the same folder, and exclude the localhost request, just link it together, and it worked.
I send script files via this function:
#GET
#Path("/{scriptName}")
#Produces(MediaType.TEXT_HTML)
public InputStream getScript(#PathParam("scriptName") String scriptName) {
return ScriptService.getScript(scriptName);
}
In my HTML, the way I put it together in the <head> part is this:
<head>
<title>Home</title>
<link rel="stylesheet" href="http://localhost:8080/WebProject/scripts/mycss.css" type="text/css">
</head>
<body>
<div class="hello">
This is some text
</div>
<div class="hello2">
It is nice to meet you sir.
</div>
</body>
Do you guys have any idea on why this is happening?
Btw, as a side note, I use the same code to fetch my Vue.js scripts, and they work perfectly fine.

Angular 5 together with Spring Thymeleaf

I've been trying to understand how can I use Angular 5 (or 2 or 4) together with Spring thymeleaf template together. My problem is that Angular 5 runs on CLI and it resolves as it's own project (app folder). But that makes it SPA and to be honest I do not like full SPA applications. In my opinion it makes them slow when huge data is there to be processed.
Instead I want to make multi page applications (which means page refresh and server side rendering). And I want to use some Angular 5 features (for example two way data binding). But how exactly I can achieve that? In Angular 1 (AngularJS) all I had to do was include it's source and done. But how about with Angular 5?
Its not much different from AngularJS. You just need to include compilation of the typescript to javascript in your build using something like frontend-maven-plugin. In your thymeleaf templates you would need to point to your generated js files.
Check out the following:
http://justincalleja.com/2016/04/17/serving-a-webpack-bundle-in-spring-boot/
http://blog.gerardin.info/archives/824
I used Thymeleaf to serve a common header and footer
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>My APP</title>
</head>
<body>
<header th:replace="header.html :: headerContent">header content</header>
<app-root></app-root>
<footer th:replace="footer.html :: footerContent">footer content</footer>
<script type="text/javascript" src="inline.bundle.js"></script>
<script type="text/javascript" src="polyfills.bundle.js"></script>
<script type="text/javascript" src="styles.bundle.js"></script>
<script type="text/javascript" src="vendor.bundle.js"></script>
<script type="text/javascript" src="main.bundle.js"></script>
</body>
</html>

In custom adaptor, where is the file's title property I have to set?

I'm making custom file adaptor using adaptor library.
In my adaptor getDocContent(), I retrieve file from URL, and set the content to the Response's OutputStream like sharepoint connector do.
So, I thought "Where is the file's title property I have to set? This is just a content."
As I was thinking, the file does not hit by it's title on the GSA search API. Also by File Extension Filter.
When developing a connector for Google Search Appliance using the Adaptor Framework, you have a few options on how to display metadata. You can send metadata via HTTP headers, but this lacks somehow. For example, when your metadata is too long, it will get truncated (because of it's size).
Since some time we use Handlebars for templating in GSA connectors. You pass your retrieved object to Handlebars, after which you can compose an HTML document such as following:
<!DOCTYPE html>
<html>
<head>
<meta name="description" content="{{object.description}}"/>
<meta name="filetype" content="{{object.filetype}}"/>
<title>{{object.title}}</title>
</head>
<body>
<h1>{{object.title}}</title>
<p>{{object.description}}</p>
<p>{{object.someMoreContent}}</p>
</body>
</html>
So when you getDocContent method is invoked (usually, when GSA crawls you connector), it will crawl the page you design with HTML and HandleBars. Very straightforward and easy to maintain!

how to map Spring controllers to their thymeleaf views(intellij14)

I am trying to configure spring boot thymeleaf and intellij, and for the most part have nailed it, but I cant seem to be able to link controllers with their views, as intellij annoyingly keeps displaying the following message, and the auto-completing the system is not working:
Cannot Resolve 'varName'
my controller looks like this "main/java/..../controller.java"
#Controller
public class CardsController {
#RequestMapping(value="/card/{id}",method = RequestMethod.GET)
public String viewCardAction(#PathVariable("id") Card card,Model model){
model.addAttribute("card",card);
return "cards/view";
}
}
And this is my mockup view "main/resources/cards/view/cards/view":
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head lang="en">
<meta charset="UTF-8"/>
<title th:text="${card.name}"></title> <!-- this line shows the message -->
</head>
<body>
<div th:text="${card.name}"></div> <!-- this line shows the message -->
</body>
</html>
is it that intelij, is still not working for this, or am I missing some configuration
NOTE: it works and compiles, just wanna enable autocomplete
Looks like this is a bug in Intellij.
Here's the youtrack link:
https://youtrack.jetbrains.com/issue/IDEA-132738
It hasn't gotten much attention from the JetBrains developers yet so you may want to add your comments or vote it up there.
Since it is still unresolved and people might come here by search, here is the workaround mentioned by zhao xinjing in the link posted in the accepted answer:
<!--/*--><!--#thymesVar id="loginFormBean" type="sample.system.login.LoginFormBean"--><!--*/-->
<form id="formMain" class="form-horizontal" action="#" data-th-action="#{/login}" data-th-object="${loginFormBean}" method="post"> as thymeleaf comment, nothing be left in html output
You can also use it to specify collections, e.g.
<!--/*--><!--#thymesVar id="names" type="List<String>"--><!--*/-->
<!--/*--><!--#thymesVar id="name" type="String"--><!--*/-->
<li th:each="name: ${names}" ... />

How to get output of RAZOR template engine as a String and then write it to a file?

In my ASp.Net MVC 3 application, I am using the RAZOR engine for generating html with templates. It is working great for viewing in a browser. But I would like to get the output of the razor engine to a string and then write it to a file. For example:
test.cshtml
<html>
<head>
<title>
#websiteTitle
</title>
</head>
<body>
#websiteBody
</body>
</html>
I would like to make a call in my controller code like this:
String output = processTemplate(...)
//code to write 'output' to a file
..where processTemplate() takes the template file name and the required template parameters and produce the String output.
I tried to search in Google but couldn't find any docs on how to do it. Is it possible? If yes, how?
Note: I was able to do it using FreeMarker in Java web applications - we can output to Console, File or StringWriter. It was very convenient. I would like to have similar functionality.
Use "RazorEngine"
for an example of use, have a look at http://www.hanselman.com/blog/NuGetPackagesOfTheWeek12AccessingGoogleSpreadsheetsWithGDataFromCAndHostingRazorTemplatesToGenerateHTMLFromAConsoleApp.aspx

Resources