Using Spring's theme resolvers and themes with a Thymeleaf template - spring

I would like to use Spring theme resolvers' feature (see: here) in my Thymeleaf template.
What is the Thymeleaf's equivalent to the spring:theme JSP tag?
See JSP code sample below:
<%# taglib prefix="spring" uri="http://www.springframework.org/tags"%>
<html>
<head>
<link rel="stylesheet" href="<spring:theme code='styleSheet'/>" type="text/css"/>
</head>
<body style="background=<spring:theme code='background'/>">
...
</body>
</html>

I don't think this was the case at the time that you asked your question, but the current version of thymeleaf-spring3 (2.0.18 as of this writing) has as part of the SpringStandard dialect a themes function, which can be used like this:
<body th:style="'background-color: ' + ${#themes.code('backgroundColor')}">

Related

how to add static file such as css and js in html file in spring boot?

I am new spring boot and thymeleaf.
i have try adding css file to my html file in spring boot. but nothing work
it is not working for me
<link rel="stylesheet" th:href="#{/main.css}">
my css file in under
static
---| css
---| main.css
The static directory is served from /.
For example, src/main/resources/static/main.css will be served from /main.css whereas src/main/resources/static/css/main.css will be served from /css/main.css.
try out this it will work for you:
<link rel="stylesheet" th:href="#{/css/main.css}"/>
Anywhere beneath src/main/resources/static is an appropriate place for static content such as CSS, JavaScript.The src/main/resources/templates folder is intended for view templates that will be turned into HTML by a templating engine such as Thymeleaf.
This will surely work.
<link rel="stylesheet" th:href="#{/css/main.css}"/>
Try out this:
<link rel="stylesheet" href="../static/css/main.css" type="text/css" th:href="#{/main.css}">
Please let me know if it works or not !!
Be sure that your HTML file is in the templates folder if not please drag it to the templates folder then add
<link rel="stylesheet" th:href="#{/css/main.css}"/>
in your HTML file.

send arabic data in spring mvc form

i have a spring form where i need to send some data in arabic so
i set the page encoding and the content type to UTF-8 like this :
<%# page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
...
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
when i display the data in the controller i get something like this ?¨???¦?³?±???§
i'm using spring 4 along with spring security, i don't know if this can be be the cause, but can it be the csrf added by spring security ?
thanks in advance

How to implement layout template with spring boot and mustache

I have a Sprint Boot Web Maven project created from Spring Initlializer with Mustache templating engine.
So far, its working and I can do the basic things but I want to set a layout (template) that will include the main body of html e.g. <html>...</html> whilst my view template will only include the page content e.g. <h1>Hello World</h1>
I can get partials to work so I could do {{>header}}<h1>Hello World</h1>{{>footer}}
What I want to be able to do is:
index.html
{{>header}}{{>content}}{{>footer}}
home.html
<h1>Hello World</h1>
I can't find any tutorial or documentation how do this.
If it helps, I found some reference documentation here:
https://spring.io/blog/2016/11/21/the-joy-of-mustache-server-side-templates-for-the-jvm
See the section on Natural Templates.
{{!
<!doctype html>
<html lang="en">
<body>
}}
{{>header}}
<h1>Demo</h1>
<div>Hello World</div>
{{>footer}}
{{!
</body>
</html>
}}

How do you include twitter bootstrap with spring mvc?

I have spring mvc supplied text boxes and buttons in form page how to include twitter bootstrap in those tags. Where do I place bootstrap related files in the folder structure and which jar do I required to use bootstrap?
Example text boxes are:
<%#taglib uri="http://www.springframework.org/tags/form" prefix="form" %>
<form:form method="post" commandName="stdcmd">
username:<form:form path="txtUname">
password:<form:form path="txtPwd">
<input type="submit" value="register"/>
</form:form>
To use the bootstrap, you need to place the bootstrap files in the web folder. For example, resources/bootstrap folder. Then add the link to those files in jsp like:
<link rel='stylesheet' href='resources/bootstrap/bootstrap.min.css'>
<script type="text/javascript" src="resources/bootstrap/bootstrap.min.js"></script>
Also on your spring config file include this one:
<mvc:resources mapping="/resources/**" location="/resources/bootstrap
Check this link for more info
You should be able to access the classes from bootstrap and apply in your jsp.

One or more resources has the target of 'head' but not 'head' component has been defined within the view

I created a JSF page with PrimeFaces components. The project runs fine, but the PrimeFaces UI look and feel is completely missing. I'm only noticing below message in server log:
One or more resources has the target of 'head' but not 'head' component has been defined within the view
What does this mean and how can I fix the PrimeFaces UI styling?
This means that you're using plain HTML <head> instead of JSF <h:head> in your XHTML template. The JSF <h:head> allows automatic inclusion of CSS/JS resources in the generated HTML <head> via #ResourceDependency annotations. PrimeFaces as being a jQuery based JSF component library needs to auto-include some jQuery/UI JS/CSS files and this really requires a <h:head>.
So, search for a
<head>
<title>Some title</title>
...
</head>
in your templates and replace it by
<h:head>
<title>Some title</title>
...
</h:head>
See also:
What's the difference between <h:head> and <head> in Java Facelets?
Unable to understand <h:head> behaviour
How to programmatically add JS and CSS resources to <h:head>
How to include another XHTML in XHTML using JSF 2.0 Facelets?

Resources