Equivalent to c:set in thymeleaf [duplicate] - spring

I am new to Thymeleaf and converting my Web page from JSP to Thymeleaf. I have a strut tag like this:
<c:set var="someVariable" value="${someValue}"/>
That variable can be used anywhere in JSP. Is there any such alternatives for this in Thymeleaf?

You can use local variables.
Declare an HTML element with a th:with attribute. For example
<div th:with="someVariable=${someValue}">
The documentation states
When th:with is processed, that [someVariable] variable is created as a
local variable and added to the variables map coming from the context,
so that it is as available for evaluation as any other variables
declared in the context from the beginning, but only within the bounds
of the containing tag.

Just a note, if you wish to assign more than one variable, separate them with a comma :
<div th:with="someVariable=${someValue},anotherVariable=${anotherValue}">
See the third example : Local Variable section of Thymeleaf documentation

declare with th:with="varName=${'str'}
ref with in src th:src="#{${varName}}"
in more detail:
<head th:with="component =${'/static/component'}, bizJs = ${'/static/js/biz'}">
<span th:text="${component}"></span>
<script th:src="#{(${component})}"></script>
<script th:src="#{${bizJs} + '/static/js'}"></script>
</head>

Related

can i define global variables for an email in eloqua

I am using eloqua for email marketing and i do not see any way to define global variables in the email. Is there any script that eloqua uses that i can use to define some global variables at the top of the email so i do not have to define values several times in the html?
I am looking to define variables like this:
var mylink = "www.google.com";
<!DOCTYPE html>
<html>
<head> </head>
<body>
Visit Website
</body>
</html>
Eloqua actively blocks java scripting in emails[1].
What you are attempting is bypassing eloqua built in functions.
Either utilize the field merge or dynamic content modules to achieve your desired outcome.
If you have a range of values, picklists can be very powerful.
1 https://docs.oracle.com/en/cloud/saas/marketing/eloqua-user/Help/Emails/HTMLemailCodeRequirements.ht

Use of spring security jstl tag <sec:authority access="hasPermission(#domain, 'permission')> inside a jstl core loop

I'm using facelets (JSF2.1) and I'm trying to do something like:
<c:forEach var="domainObject" items="#{MB.listOfDomainObjects}">
<sec:authorize access="hasPermission(#domainObject,'PERMISSION_X')">
hello world
</sec:authorize>
</c:forEach>
I've tried changing #domainObject by #domainObject, #{domainObject}, $domainObject and a lot of other combinations with the same result: domainObject is not processed correctly. Sometimes I get an error saying the page cannot be constructed, others domainObject is null.
It's like the scope where the tag c:forEach puts the variable domainObject is not scanned by sec:authorize to find it.
I've tried also to force the use of a scope using the tag <c:set ... scope="view"/>. I've also tried to use <ui:repeat> with the same results, but considering the tag sec:authorize is a jstl one, I suppose is executed in build time (like c:forEach) and not in render time (like ui:repeat), so I think I must use c:foreach and not ui:repeat.
Any chance to solve my problem?

Spring JSP variable not assigned

I have in my JSP page code like this:
<spring:url value="" var="url"/>
EN
And issue is that parameter url in link is always set to empty String.I would expect that if I type url like localhost:8080/test the url variable will hold this value and it will be replaced in link so it would look like /change_locale?locale=EN&current=test. However it is always generated like /change_locale?locale=EN&current=.What I am doing wrong? Best regards
In
<spring:url value="" var="url"/>
Your value value is the empty String. Because of this, the URL is relative.
Spring uses UrlTag to construct the value from a <url> tag. You'll want to take a look at its createUrl method in the source code if you're curious.
In this case, it will generate a value that is the empty String and store it in a page scope attribute named url. That's what you get when rendering
${url}

How to process freemarker template from variable inside template?

Simple example:
<div>${item.content}</div>
But inside of item.content variable may exist another template code, such as
<#if otherItem.image??><div class ...>...</#if>
Is that possible to tell freemarker compile this code inside variable?
?interpret creates a directive from a string. Note that it doesn't call the directive. Expressions that evaluate to directive can be called with <#someExpression />. Putting these together you get:
<div><#item.content?interpret /></div>

Cannot access variable inside the included JSP

I am using Spring 2.5 in my application. In the view I have main jsp in which I have included another jsp. I have declared a variable using c:set tag in main jsp which I can not access inside the jsp. Below is code
main.jsp
<c:set var="hPediquestStudy"><spring:message code="study.hpediquest.mapping" /></c:set>
<c:set var="currentStudy" value='<%=(String)session.getAttribute("currentStudy")%>'/>
<html>
<head>
</head>
<body>
<c:if test="${currentStudy eq hPediquestStudy}">
Variables are equal
</c:if>
<c:if test="${currentStudy ne hPediquestStudy}">
Variables are not equal
</c:if>
<jsp:include page="/WEB-INF/jsp/included.jsp"></jsp:include>
</body
</html>
included.jsp
<c:if test="${currentStudy eq hPediquestStudy}">
hPediquestStudy Variable is accessible
</c:if>
<br/>currentStudy : ${currentStudy}
<br/>hPediquestStudy : ${hPediquestStudy}
I am getting output
Variables are equal
currentStudy : hPediquest
hPediquestStudy :
Why on main jsp both values are equal while in included jsp I dont see its value?
Why currentStudy displays its value inside the included jsp?
is there any solution which helps me to access the variable set in the parent jsp ans can be accessed in the included jsps?
I can see the value of hPediquestStudy value if I set that variable in included jsp like in main jsp. But I dont want to set it every time when I include the jsp. Please help
Why on main jsp both values are equal while in included jsp I dont see its value?
Because <c:set> by default stores them in the page scope.
Why currentStudy displays its value inside the included jsp?
Because it's available as session attribute as well.
is there any solution which helps me to access the variable set in the parent jsp ans can be accessed in the included jsps?
You need to set the scope attribute of <c:set> to request or above. The (default) page scope is exposed to current JSP only, not any included JSPs.
Note that the line
<c:set var="currentStudy" value='<%=(String)session.getAttribute("currentStudy")%>'/>
is unnecessary. The ${currentStudy} will already scan for variables in page, request, session and application scopes. As you've apparently already set it in the session scope, you don't need to copy it into the page scope. So, just remove that line. All with all, your top 2 <c:set> lines should be replaced with this single line:
<c:set var="hPediquestStudy" scope="request"><spring:message code="study.hpediquest.mapping" /></c:set>
and then it'll work the way you intended.
See also:
Our EL wiki page

Resources