Whenever I am trying to run my web application I am getting the error below.
Can you please help.
2019-04-17 07:33:13.881 ERROR 21660 --- [io-9393-exec-10] org.thymeleaf.TemplateEngine : [THYMELEAF][http-nio-9393-exec-10] Exception processing template "index": Exception evaluating SpringEL expression: "#request.userPrincipal.name" (template: "/_header" - line 10, col 48)
org.thymeleaf.exceptions.TemplateProcessingException: Exception evaluating SpringEL expression: "#request.userPrincipal.name" (template: "/_header" - line 10, col 48)
Caused by: org.springframework.expression.spel.SpelEvaluationException: EL1007E: Property or field 'name' cannot be found on null
My header.html looks like this
<div class="header-bar">
<th:block sec:authorize="isAuthenticated()">
Hello
<a th:href="#{/admin/accountInfo}" th:utext="${#request.userPrincipal.name}">..</a>
|
<a th:href="#{/admin/logout}">Logout</a>
</th:block>
<th:block sec:authorize="!isAuthenticated()">
<a th:href="#{/admin/login}">Login</a>
</th:block>
</div>
Try modify the code like this
<div class="header-container"
xmlns:th="http://www.thymeleaf.org"
xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity">
And update the porn.xml
<dependency>
<groupId>org.thymeleaf.extras</groupId>
<artifactId>thymeleaf-extras-springsecurity5</artifactId>
</dependency>
It will work correctly
This looks like the code (isAuthenticated) is not taking effect.
Try updating your pom file to use the following version:
thymeleaf-extras-springsecurity5
And update your html header file to:
xmlns:sec="http://www.thymeleaf.org/extras/spring-security"
Related
I'm getting following error in my new Angular 11 app. Could not found any syntax errors.
core.js:6156 ERROR DOMException: Failed to execute 'setAttribute' on 'Element': 'ng-reflect-Dev Portal' is not a valid attribute name.
at EmulatedEncapsulationDomRenderer2.setAttribute (http://localhost:8080/admin/vendor.js:62217:16)
at setNgReflectProperty (http://localhost:8080/admin/vendor.js:38053:26)
at setNgReflectProperties (http://localhost:8080/admin/vendor.js:38078:13)
at elementPropertyInternal (http://localhost:8080/admin/vendor.js:38001:13)
at Module.ɵɵproperty (http://localhost:8080/admin/vendor.js:42749:9)
at DevMainHeaderComponent_Template (http://localhost:8080/admin/app-layout-layout-module.js:394:65)
at executeTemplate (http://localhost:8080/admin/vendor.js:37594:9)
at refreshView (http://localhost:8080/admin/vendor.js:37463:13)
My html template as follows,
<div class="row main-header-container">
<div class="col-xs-12 col-sm-12 col-md-12 col-lg-8 col-xl-8 offset-lg-2 offset-xl-2">
<div class="row">
<div class="col-xs-6 col-sm-6 col-md-6 col-lg-2 col-xl-2" [ngClass]="(isLoggedInUser === true)?'offset-lg-3 offset-xl-3':'offset-lg-9 offset-xl-9'">
<select class="form-control">
<option>English</option>
<option>Finish</option>
</select>
</div>
</div>
</div>
</div>
TS File as follows,
export class DevMainHeaderComponent implements OnInit {
public isLoggedInUser = true;
}
If I removed ngClass directive it works, but I cant find any issue with ngClass syntax here, even I tried hardcoded like this [ngClass]="['first']", still getting same error.
This console error is not throwing with ng serve and working fine, only in prod it happens.
If your are using a condition to determine a class you should use a different syntax like so:
[ngClass]="{'first': true, 'second': true, 'third': false}"
Example:
[ngClass]="{'checked': rating > 0}"
https://angular.io/api/common/NgClass
I'm building my project with maven, my project name is <name>Dev Portal</name>. It contains a space. This project name is adding as attribute. And attribute cannot contains space.
<!--bindings={
"ng-reflect-Dev Portal": "true"
}-->
Removing space from name resolved the problem<name>DevPortal</name>
With Angular 9 this behavior was not there.
I've tried this in several different angular apps and get the same error. I've verified that the latest version of Handlebars is installed. Whenever I try to use the triple mustache {{{ }}} to render html as html within an html file, the compiler throws this error (I'm using VSCode, but the same error occurs within the stackblitz below):
Template parse errors:
Parser Error: Missing expected : at the end of the expression [{{{item}}}] in ng:///AppModule/AppComponent.html#9:37 ("DropListEnterPredicate]="enter" (cdkDropListDropped)="drop()">
<div cdkDrag class="example-box">[ERROR ->]{{{item}}}</div>
</div>
</div> "): ng:///AppModule/AppComponent.html#9:37
Parser Error: Unexpected end of expression: {{{item}}} at the end of the expression [{{{item}}}] in ng:///AppModule/AppComponent.html#9:37 ("DropListEnterPredicate]="enter" (cdkDropListDropped)="drop()">
<div cdkDrag class="example-box">[ERROR ->]{{{item}}}</div>
</div>
</div> "): ng:///AppModule/AppComponent.html#9:37
Parser Error: Missing expected } at the end of the expression [{{{item}}}] in ng:///AppModule/AppComponent.html#9:37 ("DropListEnterPredicate]="enter" (cdkDropListDropped)="drop()">
<div cdkDrag class="example-box">[ERROR ->]{{{item}}}</div>
</div>
</div> "): ng:///AppModule/AppComponent.html#9:37
I've copied a simple stackblitz, in it I've duplicated the error by putting the triple braces around the {{{item}}}.
https://stackblitz.com/edit/angular-4kmtwp
I've tried { {{ item }} }, {{ '{' }} {{ item }} {{ '}' }}, &123;&123;&123; item &125;&125;&125; but none of those render the html.
The solution was to change the line:
<div cdkDrag class="example-box">{{{item}}}</div>
to
<div cdkDrag class="example-box" [innerHtml]="item"></div>
As an additional note, if there are class names within the innerHtml that you need to style, you may need to set the encapsulation within the typescript file as ViewEncapsulation.None.
i.e. encapsulation: ViewEncapsulation.None
I am new to thymeleaf. I am bit confused now. Please check out the codes below
<th:block th:with="${someVarible=false}">
<th:block th:each="dem : ${demo}">
<th:block th:if="${dem.status==0}">
//Here I need to change the value of someVarible to true
</th:block>
</th:block>
<th:block th:if="${someVariable}">Its true</th:block>
</th:block>
I need to edit the value of someVarible . How can I do it. Thanks in advance.
As Lukas said, it's not possible to change the value of a variable in Thymeleaf, because that only applies to the content within that element. However it is possibe to achieve something very similar using Thymeleaf only.
You can use Collection Selection and the ^[...] syntax to select the first element in your list that matches the criteria status==0. This expression would look like:
${demo.^[status==0]}
If the demo list contains an element with status==0, then that will be returned. Otherwise, it will result in null. This can be used directly in your th:if:
<th:block th:if="${demo.^[status==0]}">Its true</th:block>
Or, if you need to use someVariable for other things too, you can assign it to a variable using th:with (Docs):
<th:block th:with="someVariable=${demo.^[status==0]}">
<th:block th:if="${someVariable}">Its true</th:block>
</th:block>
You can't achieve your desired functionality as you described with Thymeleaf.
th:with does just local variable definition which is only available for evaluation inside that fragment.
<div class="example1" th:with="foo=${bar}">
<!--/* foo is availabile here */-->
<th:block th:text="${foo}" />
</div>
<div class="example2">
<!--/* foo is NOT availabile here! */-->
</div>
And you can't change that variable in template. Thymeleaf is just presentation layer, you're trying to achieve something which has to be done on application layer (Java code).
On application layer (Java code) you could do:
Map<Integer, Boolean> fooMap = new HashMap<Integer, Boolean>();
for(Demo demo : demos) {
if(demo.getStatus() == 0) {
fooMap.put(demo.getId(), true);
} else {
fooMap.put(demo.getId(), false);
}
}
And then on presentation layer (Thymeleaf):
<th:block th:each="demo : ${demos}">
<th:block th:text="${demo.getId()}" />
</th:block>
<th:block th:each="demo : ${demos}">
<th:block th:if="${fooMap.get(demo.getId()) == true}">It's true</th:block>
</th:block>
If you don't want to use HashMap you could use inheritance and extend object Demo.
(Please note that code which I wrote is not tested, so it may need some small fixed, but I hope that I helped you.)
Do you have a form? How do you send data to server?
If your variable of type boolean you can add, e.g., a checkbox for editing :
<th:block th:with="${someVarible=false}">
<th:block th:each="dem : ${demo}">
<th:block th:if="${dem.status==0}">
<label for="someVariableCheck">Edit someVariable</label>
<input id="someVariableCheck" type="checkbox" th:value="${someVariable}"/>
</th:block>
</th:block>
<th:block th:if="${someVariable}">Its true</th:block>
I a using Spring security with an HTML page using thymeleaf. I have a problem to use the "sec:authorize" property in this case:
<ul class="nav nav-tabs margin15-bottom">
<li th:each="criteriaGroup,iterGroups : ${aGroupList}"
th:class="${iterGroups.index == 0}? 'active'">
<a th:href="'#' + ${criteriaGroup.id}" data-toggle="tab"
th:text="${criteriaGroup.groupName}"></a>
</li>
</ul>
Now I want to add a spring security property like: if I have this particular criteria, I need a authorization (sec:authorize="hasRole('Criteria')") although I will not see the tab corresponding to this criteria:
<ul class="nav nav-tabs margin15-bottom">
<li th:each="aGroup,iterGroups : ${aGroupList}" th:class="${iterGroups.index == 0}? 'active'"
th:sec:authorize="$({criteriaGroup.id =='criteriaA'} || ${criteriaGroup.id =='criteriaB'}) ? 'hasRole('Criteria')'">
<a th:href="'#' + ${criteriaGroup.id}" data-toggle="tab"
th:text="${criteriaGroup.groupName}"></a>
</li>
</ul>
But when I am doing this I have the following error:
org.thymeleaf.exceptions.TemplateProcessingException: Error processing template: dialect prefix "th" is set as non-lenient but attribute "th:sec:authorize" has not been removed during process
How can I avoid it?
Remove th: in front of sec:authorize
The Spring Security 3 integration module is a Thymeleaf dialect. More information can be found here.
Perhaps you'd want to use the #authentication object instead of the sec dialect.
From the docs:
<div th:text="${#authentication.name}">
The value of the "name" property of the authentication object should appear here.
</div>
~/Views/PlanBuilder/PlanBuilder.cshtml
#{
ViewBag.Title = "PlanBuilder";
Layout = "~/Views/Shared/_LayoutPlanBuilder.cshtml";
}
<div id="tabs-1">
<div id="wrap" class="left">
<ul>
#Html.Partial("~/Views/Hall/Index.cshtml");
</ul>
</div>
</div>
~/Views/Hall/Index.cshtml
#model IEnumerable<ShaadiPlanner.Models.Hall>
#{
ViewBag.Title = "Index";
Layout = "~/Views/Shared/_LayoutPlanBuilder.cshtml";
}
#foreach (var item in Model) <!-- ERROR LINE -->
{
<li id="#Html.DisplayFor(modelItem => item.ID)" class="options left">
<div class="box-header">
<h4 class="left"><span class="name">#Html.DisplayFor(modelItem => item.Name)</span> (#Html.DisplayFor(modelItem => item.Area))</h4>
<h4 class="right">Rs. <span class="price">#Html.DisplayFor(modelItem => item.EstCost)</span></h4>
</div>
</li>
}
Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.
Note: When i define Model in the #Html.Partial code like this then #Html.Partial("~/Views/Hall/Index.cshtml", model); it give following error.
Compilation Error
Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.
Compiler Error Message: CS0103: The name 'model' does not exist in the current context
Try uppercasing Model, i.e.
#Html.Partial("~/Views/Hall/Index.cshtml", Model);
As #webdeveloper comments, you have not got a model declared in your view!