Thymeleaf How to check current URL? - spring-boot

i was familiar with JSTL before and thymeleaf is something new to me.
What i want is to check if the current URL is the index page (/index), if so then make visible a div.
Here is a JSTL equivalent example
<c:set var="url" value="${ pageContext.request.requestURI }" />
<c:if test="${url=='/example/WEB-INF/views/inbox.jsp'}">
...
</c:if>

Try this:
<div th:if="${#httpServletRequest.requestURI == '/example/WEB-INF/views/inbox.jsp'}">
some content
</div>

Related

Output not visible jstl c:out not showing on browser

Problem: When I open page view source this data is shown on browser
Hello there jsp
<c:forEach var="student" items="2abcnullmca">
<c:out value= ""/>
</c:forEach>
<c:out value= "abc" />
And on browser it does not show c:out value in spring I have used model.addAttribute() to show data
Actually I forgot to include jstl tag thanks for looking into it but.
Here is the Tag -
<%# taglib prefix = "c" uri = "http://java.sun.com/jsp/jstl/core" %>

Checked radion button based on model variable + Spring forms + JSP

I have model called cuntry.java with variable lang .I want to check the radio button option in JSP based on the lang. It could be 'EN' or 'FR'. when i wrote below code, throwing error in JSP page.
Code:
<form:radiobutton path="lang" id="language" value="FR" name="radios" <c:if ${cuntry.lang == 'FR' ? 'checked="checked"' : '' } /> ></form:radiobutton>
Error : Unterminated <form:radiobutton tag
There are multiple errors in the form tag. When using Spring tags, you may not specify id and name attributes. They will be generated automatically from path variable. Tag <c:if ... /> inside another tag <form:radiobutton ... /> is not allowed. Remove end tag as well. This is the right way
<form:radiobutton path="lang" value="FR" checked="${cuntry.lang == 'FR' ? 'checked' : '' }" />
If the value of cuntry.lang is FR, the Spring tag will result the following HTML (after compiling)
<input id="lang" name="lang" checked="checked" type="radio" value="FR" />
otherwise
<input id="lang" name="lang" type="radio" value="FR" />
If you absolutely need attributes id = language and name = radios, you can not use Spring tag .
You will find a good example here.
<c:if> tag works on tags rather than attributes. As a result, your html syntax becomes invalid.
I haven't done jsp for quite a while. this should more or less work:
<form:radiobutton checked="${cuntry.lang == 'FR' ? 'checked':''}" path="lang" id="language" value="FR" name="radios">
</form:radiobutton>
If you insist on using <c:if> tag for this, you can do this:
<c:if test="${cuntry.lang == 'FR'}">
<form:radiobutton checked="checked" path="lang" id="language" value="FR" name="radios">
</form:radiobutton>
</c:if>
<c:if test="${cuntry.lang != 'FR'}">
<form:radiobutton path="lang" id="language" value="FR" name="radios">
</form:radiobutton>
</c:if>
You can also use <c:choose>,<c:when>,<c:otherwise> tags to solve this problem.
Obviously the first solution looks better.

Need to Combine Spring Binding Errors and Custom Exceptions into Same DIV

I need to display SpringMVC's Form errors, as well as any custom "Exception" messages that I store in the request, in the same DIV on my page.
Right now they're in separate DIV's:
<!-- Validation Errors -->
<spring:hasBindErrors htmlEscape="true" name="model">
<c:if test="${errors.errorCount gt 0}">
<div class="errors_div clsErrorTblBorder">
<c:forEach items="${errors.allErrors}" var="error">
<div class="errors clsWhiteBack blue">
<myForm:message messageLinkClass="errorLink"
error="${error}" />
</div>
</c:forEach>
</div>
</c:if>
</spring:hasBindErrors>
<!-- Exceptions -->
<c:if test="${fn:length(requestScope.exceptions) > 0}">
<div class="errors_div clsMsgTblBorder">
<c:forEach items="${requestScope.exceptions}" var="exception">
<div class="messages clsWhiteBack">
${exception}
</div>
</c:forEach>
</div>
</c:if>
The shared condition for an Error DIV should be,
${fn:length(requestScope.exceptions) || errors.errorCount gt 0}
But it's impossible to test this condition, because:
You don't get the errors var. until you do <spring:hasBindErrors>
Anything inside <spring:hasBindErrors> only applies if you have
binding errors. If you don't (e.g. if you only have a custom
Exception message) its logic won't execute, so you can't move the
custom "exceptions" check inside it.
I got some clues from this thread: How to access Spring 3 MVC validator results in JSP without using form taglib , and here's the working result:
<c:if test="${fn:length(requestScope.exceptions) > 0 ||
requestScope['org.springframework.validation.BindingResult.model'].hasFieldErrors()}">
The 2nd part of this condition checks for Binding Errors even prior to the tag Spring:hasBindErrors.

passing jslt value to javascript

I trying to a pass a jslt value to javascript but the value is not getting rendered.
<c:forEach items="${requestScope.P.Releases}" var="pr" varStatus="status"> <a href=javascript:popPR('${pr.url}') class="linkPR">
<c:out value="${pr.title}" escapeXml='false' /></a>
<c:foreach>
if directly type the pr.url value the popup window gets opened but if i pass the through jstl it does not call the popup.
Can anyone please suggest how to fix it.
Thanks
Try using <c:out> to output the url :
<c:forEach items="${requestScope.P.Releases}" var="pr" varStatus="status">
<a href=javascript:popPR('<c:out value="${pr.url}"/>') class="linkPR">
<c:out value="${pr.title}" escapeXml='false' /></a>
<c:foreach>

How can I display error in JSP without a <form:form>

I have a jsp page with a List<Object> as the #ModelAttribute. However, there are no <form:form> tags in the page. All I'm doing is print the contents of the List.
In my Controller.java, I'm binding an error by doing:
result.rejectValue("", "NOT_LOGGED_IN", "You should Login first") ;
But since I dont have a form in my jsp, I'm not able to access the error with:
<form:errors path="" /> <br/>
Please tell me how to access the error (or what I'm doing wrong).
In your controller:
model.addAttribute("errors", result.getAllErrors());
In your JSP:
<c:forEach items="${errors}" var="error">
<%-- do want you want with ${error} --%>
<c:out value="${error.defaultMessage}" />
</c:forEach>
Associate global errors this way:
result.reject("NOT_LOGGED_IN", "You should Login first") ;
You can show the global errors in the jsp :
<form:errors cssClass="error" delimiter="<p/>" />
for any specific error
set in your code like-
model.addObject("errorMsg","username/password failed");
And show this error on jsp in this way:
<c:out value="${errorMsg}"/>
This way you would get your error on jsp.
there's a way to reference the error like
instead of
<form:form commandName="object">
<form:errors path="field"/>
</form:form>
best
Check if Expression Language evaluation is enabled in JSP(By default it is disabled).If not add below code to enable it.
<%# page isELIgnored="false" %>

Resources