Thymeleaf doesn't bind Spring's ModelAndView - spring

I have a Controller like this:
#Controller
public class HelloController {
#Autowired
private SomeService someService;
#RequestMapping("/")
public ModelAndView index() {
ModelAndView mav = new ModelAndView("index");
mav.addObject("title", someService.getTitle());
mav.addObject("text", someService.getText());
return mav;
}
}
Now thymeleaf is supposed to have some knowledge about the mav:
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head lang="de">
<title>${title}</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
</head>
<body>
<h1>${title}</h1>
<h2>${text}</h2>
</body>
</html>
But in my frontend it doesn't work and outputs the template vars:
${title}
${text}
Any ideas? Am I missing some configuration about the ViewResolver in Spring?

Fixed:
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head lang="de">
<title th:text="${title}"></title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
</head>
<body>
<h1 th:text="${title}"></h1>
<h2 th:text="${text}">My Dummy Data</h2>
</body>
</html>

I realize this is a 4 year old question, and Stefano's answer is correct. But this is also correct (inlining):
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head lang="de">
<title>${title}</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
</head>
<body>
<h1>[[${title}]]</h1>
<h2>[[${text}]]</h2>
</body>
</html>

Related

js file not loading giving ERR_ABORTED 404

I have a Laravel/Vue3 project. For some reason I cannot get the right link to public/js/app.js.
My blade file:
<!DOCTYPE html>
<html lang="{{ str_replace('_', '-', app()->getLocale()) }}">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Laravel</title>
</head>
<body>
<div id="app">
<map-app/>
</div>
<script src="{{asset('js/app.js')}}"></script>
</body>
</html>
In my .env file:
APP_URL=https://www.evenuitrusten.nl
ASSET_URL=${APP_URL}/bankjesserver/public
I then get the error:
GET https://www.evenuitrusten.nl/bankjesserver/public/js/app.js net::ERR_ABORTED 404
Where index.html is:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Laravel</title>
</head>
<body>
<div id="app">
<map-app/>
</div>
<script src="https://www.evenuitrusten.nl/bankjesserver/public/js/app.js"></script>
</body>
</html>
On my server, the url to app.js is
/home/blabla/domains/evenuitrusten.nl/bankjesserver/public/js
The URL in the script tag looks correct. Why am I getting a 404?
I tried all kinds of variations of Asset_URL, but non of them worked.
I tried to simplify the problem bij putting a test.js file in the same folder as my maps.blade.php. This file looks like:
<!DOCTYPE html>
<html lang="{{ str_replace('_', '-', app()->getLocale()) }}">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Laravel</title>
</head>
<body>
<div id="app">
<map-app/>
</div>
<script type='text/javascript' src='./test.js'></script>
<script type='text/javascript' src='{{ asset('js/app.js') }}'></script>
</body>
</html>
But also this line leads to a 4040 and teh test.js is not downloaded.

extending fragments in Thymeleaf

I have the following fragment. I'm trying to extend the base fragment "head" with the open graph tags... but the rendered page contains only tags from fragments/head, with the og ones.
How can I add more tags to a fragment?
<head th:include="fragments/head :: head">
<!-- You can use Open Graph tags -->
<meta property="og:url" th:content="${url}" />
<meta property="og:type" content="website" />
<meta property="og:title" content="GUApp" />
<meta property="og:description" th:content="${description}" />
<!--<meta property="og:image" content="http://www.your-domain.com/path/image.jpg" />-->
</head>
<head th:fragment="head" xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">
....
</head>
The easiest option is to pass additional tags as in Flexible layouts documentation demo.
Thanks to fragment expressions, we can specify parameters for
fragments that are not texts, numbers, bean objects… but instead
fragments of markup.
This allows us to create our fragments in a way such that they can be
enriched with markup coming from the calling templates, resulting in a
very flexible template layout mechanism.
index.html
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.w3.org/1999/xhtml">
<head th:include="header :: head(~{::meta})">
<!-- You can use Open Graph tags -->
<meta property="og:url" th:content="${url}"/>
<meta property="og:type" content="website"/>
<meta property="og:title" content="GUApp"/>
<meta property="og:description" th:content="${description}"/>
</head>
...
header.html
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.w3.org/1999/xhtml">
<head th:fragment="head(meta)">
<!-- some default styles -->
<link href="base.css" rel="stylesheet" />
<!--/* Per-page placeholder for additional meta tags */-->
<th:block th:replace="${meta}" />
</head>
...
Result html:
<!DOCTYPE html>
<html lang="en">
<head>
<link href="base.css" rel="stylesheet" />
<meta property="og:url"/>
<meta property="og:type" content="website"/>
<meta property="og:title" content="GUApp"/>
<meta property="og:description"/>
</head>
...

Unable to call modelMap in JSP

I have a Spring MVC controller which maps test to test.jsp. It has ModelMap object which has to be accessed by JSP page.
Here is controller
#RequestMapping(value = "/test")
public String test(ModelMap map)
{
map.addAttribute("data","the string which should bbe printed");
return "test";
}
Here is test.jsp
<%# page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Hello</title>
</head>
<body>
<c:out value="${data}" />
</body>
</html>
I am getting nothing on the screen.
Please help me out.

Basic highlightjs document won't work

Highlightjs has very simple usage instructions, which I can't seem to get to work in the document below. I would expect the html within the <pre> and <code> to be displayed, but all I see is the text. What am I doing wrong?
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<link rel="stylesheet" href="http://cdnjs.cloudflare.com/ajax/libs/highlight.js/8.5/styles/default.min.css">
<script src="http://cdnjs.cloudflare.com/ajax/libs/highlight.js/8.5/highlight.min.js"></script>
<script>hljs.initHighlightingOnLoad();</script>
</head>
<body>
<pre>
<code class="html">
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<p>This should be escaped, highlighted html. </p>
</body>
</html>
</code>
</pre>
</body>
</html>
The answer is highlightjs does not escape code for you as was suggested to me. Good link here.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<link rel="stylesheet" href="http://cdnjs.cloudflare.com/ajax/libs/highlight.js/8.5/styles/default.min.css">
<script src="http://cdnjs.cloudflare.com/ajax/libs/highlight.js/8.5/highlight.min.js"></script>
<script>hljs.initHighlightingOnLoad();</script>
</head>
<body>
<pre>
<code class="html">
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<p>This should is highlighted html. </p>
</body>
</html>
</code>
</pre>
</body>
</html>

Code Igniter lose charset

I'm using codeigniter but it loses charset every time.
What can cause this?
For example:
At my address bar
www.abc.com/def
www.abc.com/def/
My db is UTF-8
My config file - $config['charset'] = 'ISO-8859-1';
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="description" content="">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title></title>
<link rel="stylesheet" href="css/main.css">
</head>
<body>
</body>
</html>
take a look at meta charset part

Resources