springMVC 5.1.6 validation not working (BindingResult errors are 0 ?) - validation

I am facing an issue with my "User" Object being Validated before being registered into a database
Used Technologies :
- Spring Web MVC with Hibernate Validator
- Spring Data (JDBC)
- Maven
Here is a snippet from my Controller Code :
#RequestMapping(method = RequestMethod.POST,params = "add")
public ModelAndView add(#Valid #ModelAttribute("user") User user, BindingResult result) throws Exception {
if (!result.hasErrors()) { // ERRORS NEVER HAPPEN ??
userDao3.insertUser(user);
}
return returnHome();
}
my User Validation is as follows :
#Size(min = 3, max = 20, message = "{required.name}")
String name;
#Min(value = 25, message = "{min.age}")
int age;
#Min(value = 2000, message = "{invalid.salary}")
float salary;
my jsp layout as follows :
<c:set var="contextPath" value="${pageContext.request.contextPath}"/>
<div class="container">
<form:form method="post" action="${contextPath}/?add" modelAttribute="user">
<div class="row">
<div class="col-15">
<tags:message code="field.username"/>
</div>
<div class="col-50">
<form:input cssClass="field" path="name"/>
</div>
<div class="col-25">
<form:errors path="name"/>
</div>
</div>
<div class="row">
<div class="col-15">
<tags:message code="field.age"/>
</div>
<div class="col-50">
<form:input cssClass="field" path="age"/>
</div>
<div class="col-25">
<form:errors path="age"/>
</div>
</div>
<div class="row">
<div class="col-15">
<tags:message code="field.salary"/>
</div>
<div class="col-50">
<form:input cssClass="field" path="salary"/>
</div>
<div class="col-25">
<form:errors path="salary"/>
</div>
</div>
<div class="row">
<input type="submit" value="Submit">
</div>
</form:form>
my Spring mvc Configuration xml file :
<mvc:annotation-driven/>
<bean name="homeController" class="Controllers.HomeController">
<property name="userDao2" ref="userDao2"/>
<property name="userDao3" ref="userDao3"/>
</bean>
<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basenames">
<list>
<value>naming</value>
<value>ValidationMessages</value>
</list>
</property>
</bean>
and my pom's dependencies are :
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>5.2.5.Final</version>
</dependency>
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.3.1</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>5.1.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.1.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.1.6.RELEASE</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.19</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.1.6.RELEASE</version>
</dependency>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-web-api</artifactId>
<version>7.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>javax.servlet.jsp.jstl</groupId>
<artifactId>jstl-api</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.2</version>
<scope>provided</scope>
</dependency>
the issue is : that on debugging found that errors are always equal to 0 in the BindingResult Object.
Debugging results:
Please help me understand the issue.

Related

Javax #Valid annotation doesn't work as expected

I have a for with object creation. When I trying to create it and validate - I receiving an error, but not on the UI side, as expected. Don't understand why.
Here is my html code:
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org"
xmlns:layout="http://www.thymeleaf.org"
lang="en"
layout:decorate="~{layout/layout}">
<head>
<meta charset="utf-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<title>Добавить Объект</title>
</head>
<body>
<main class="content-wrapper">
<div class="container-fluid">
<form method="POST" th:action="#{/admin/objects/add}" th:object="${objectForm}">
<p class="h4 text-left">Добавить объект</p>
<div class="form-group col-md-3 offset-md-0"
th:classappend="${#fields.hasErrors('name')}? 'has-error':''">
<label for="exampleInputName"></label>
<input name="username" type="text" class="form-control" id="exampleInputName" required="required"
placeholder="Название(ручной ввод)"
th:field="*{name}"/>
<br/>
<p class="alert alert-danger"
th:if="${#fields.hasErrors('name')}"
th:errors="*{name}">Validation error</p>
</div>
<div class="form-group col-md-1 offset-md-0">
<button type="submit" class="btn btn-success offset-md-0 btn-sm active">Создать</button>
</div>
</form>
</div>
</main>
</body>
</html>
ObjectController:
#Controller
#Validated
#RequestMapping("/admin/objects")
public class ObjectController {
private static final Logger log = LogManager.getLogger(ObjectController.class);
private final ObjectsService objectsService;
private Pageable pageable;
#Autowired
public ObjectController(ObjectsService objectsService) {
this.objectsService = objectsService;
}
#GetMapping("/add")
public String printAddObject(Model model) {
model.addAttribute("objectForm",
new Object()
);
return "objects_add";
}
#PostMapping("/add")
public String addNewObject(#ModelAttribute("objectForm") #Valid Object objectForm,
Model model,
BindingResult bindingResult) {
if (bindingResult.hasErrors()) {
System.out.println("ERROR: " + bindingResult.getAllErrors().toString());
return "objects_add";
}
if (!objectsService.create(objectForm)) {
bindingResult.addError(new FieldError("objectForm",
"name",
"Объект с таким именем уже существует"
));
return "objects_add";
}
return "redirect:/admin/objects";
}
Object class:
import javax.persistence.*;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
#Entity(name = "objects")
public #Data class Object {
#Id
#GeneratedValue(strategy = GenerationType.SEQUENCE)
#Column(name = "object_id")
private Long objectId;
#NotNull
#Size(min = 2)
#Column(name = "name")
private String name;
}
When I'm clicking on submit Button and inputing one symbol, I am expecting that UI will show me an error message, but all I can see:
Browser error
Console error
What I am doing wrong?
my pom.xml:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.0</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>ru.eco.products.waste</groupId>
<artifactId>waste-web</artifactId>
<version>0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>products-waste-web-client</name>
<description>Eco-Waste-Products Project</description>
<properties>
<java.version>11</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>nz.net.ultraq.thymeleaf</groupId>
<artifactId>thymeleaf-layout-dialect</artifactId>
<version>2.5.1</version>
</dependency>
<dependency>
<groupId>org.thymeleaf.extras</groupId>
<artifactId>thymeleaf-extras-springsecurity5</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>29.0-jre</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-integration</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.integration</groupId>
<artifactId>spring-integration-http</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-quartz</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.session</groupId>
<artifactId>spring-session-core</artifactId>
</dependency>
<!-- <dependency>-->
<!-- <groupId>org.springframework.boot</groupId>-->
<!-- <artifactId>spring-boot-devtools</artifactId>-->
<!-- <scope>runtime</scope>-->
<!-- <optional>true</optional>-->
<!-- </dependency>-->
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>42.2.18</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.integration</groupId>
<artifactId>spring-integration-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.webjars</groupId>
<artifactId>bootstrap</artifactId>
<version>4.5.3</version>
</dependency>
<dependency>
<groupId>org.webjars</groupId>
<artifactId>jquery</artifactId>
<version>3.5.1</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
I found what was wrong:
Binging Result must be right after the Object that is marked #Valid
#Validated annotation shall no be used.

template might not exist or might not be accessible by any of the configured Template Resolvers after deploy

For some reason, after the deployment, I started to give a 500 error when saving (that is, the post fulfills the request, but reloading the same page already by get causes an error. And the first time the get request this page opens to show the filling form). Although everything works well on the local computer. I ask for help!
My pom.xml:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>ru.asu</groupId>
<artifactId>pdn</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>pdn</name>
<properties>
<java.version>11</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
<dependency>
<groupId>org.thymeleaf.extras</groupId>
<artifactId>thymeleaf-extras-springsecurity5</artifactId>
</dependency>
<dependency>
<groupId>org.thymeleaf.extras</groupId>
<artifactId>thymeleaf-extras-java8time</artifactId>
<version>3.0.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf-spring5</artifactId>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>org.flywaydb</groupId>
<artifactId>flyway-core</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>4.0.1</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>4.0.1</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.6</version>
</dependency>
<!-- JAXB -->
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.3.0</version>
</dependency>
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-core</artifactId>
<version>2.3.0</version>
</dependency>
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-impl</artifactId>
<version>2.3.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.flywaydb</groupId>
<artifactId>flyway-maven-plugin</artifactId>
<version>6.0.8</version>
</plugin>
</plugins>
</build>
</project>
My Controller:
#GetMapping("/new_violation")
public String showNewViolationForm(Model model) {
Violation violation = new Violation();
model.addAttribute("violation", violation);
return "new_violation";
}
#PostMapping("/new_violation")
public String saveViolation(
#Valid #ModelAttribute Violation violation,
BindingResult bindingResult,
Model model
) {
if (bindingResult.hasErrors()) {
Map<String, String> errorsMap = getErrors(bindingResult);
model.mergeAttributes(errorsMap);
model.addAttribute("violation", violation);
} else {
violationService.save(violation);
}
return "/new_violation";
}
And some of my Thymeleaf:
<tr class="text-center">
<form action="#" method="post" th:action="#{/new_violation}" th:object="${violation}">
<div>
<th>
<label>
<input th:field="*{numProtocol}" type="text"/>
</label>
<span class="form-control is-invalid" th:errors="*{numProtocol}"
th:if="${#fields.hasErrors('numProtocol')}">
</span>
</th>
<th>
<label>
<input th:field="*{dateProtocol}" type="date"/>
</label>
<span class="form-control is-invalid" th:errors="*{dateProtocol}"
th:if="${#fields.hasErrors('dateProtocol')}">
</span>
</th>
<th>
<label>
<input th:field="*{violationAddress}" type="text"/>
</label>
<span class="form-control is-invalid" th:errors="*{violationAddress}"
th:if="${#fields.hasErrors('violationAddress')}">
</span>
</th>
<th>
<div>
<label>
<input th:field="*{child.fio}" type="text"/>
</label>
<span class="form-control is-invalid" th:errors="*{child.fio}"
th:if="${#fields.hasErrors('child.fio')}">
</span>
</div>
</th>
<th>
<label>
<input th:field="*{child.address}" type="text"/>
</label>
<span class="form-control is-invalid" th:errors="*{child.address}"
th:if="${#fields.hasErrors('child.address')}">
</span>
</th>
<th>
<button class="btn btn-primary bg-danger" type="submit">Сохранить</button>
</th>
</div>
</form>
Looks like there is a typo in your return statement.
Instead of
return "/new_violation";
try this:
return "new_violation";
As the same appears in your get mapping. I am assuming the same should be in your post mapping too.

Bootstrap not working with spring-boot?

I am attempting to install bootstrap for use in my spring boot project, which uses thymeleaf. I am getting this error with the template (index.html, shown below):
Malformed markup: Attribute "class" appears more than once in element
I assume this is because bootstrap isn't installed properly. Below I have an image of the files also.
I am pretty sure all the dependencies are right.
INDEX HTML:
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="utf-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<link rel="stylesheet" href="/bootstrap-3.3.7/css/bootstrap.min.css"/>
<script src="/bootstrap-3.3.7/js/bootstrap.min.js"></script>
<!-- <link rel="stylesheet" type="text/css"
th:href="#{/webjars/bootstrap/3.3.7/css/bootstrap.min.css}" />
<link rel="stylesheet" type="text/css" th:href="#{/css/main.css}" />
<script
src="//netdna.bootstrapcdn.com/bootstrap/3.1.0/js/bootstrap.min.js">
</script>
<script src="//code.jquery.com/jquery-1.11.1.min.js"></script> -->
<title>Home</title>
<style></style>
</head>
<body>
<div class="navbar navbar-default" role="navigation" id="topnavbar">
<div class="container">
<div class="navbar-header">
<button class="navbar-toggle" type="button" data-
toggle="collapse" data-target="#navbar-main">
<span class="icon-bar"></span> <span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
</div>
<div class="navbar-collapse collapse" id="navbar-main">
<ul class="nav navbar-nav">
<li><span class="glyphicon glyphicon-home"></span>Profile</li>
<li>
<a href="/competition">
<span class="glyphicon glyphicon-star"></span> Competitions
</a>
</li>
</ul>
<ul class="nav navbar-nav navbar-right">
<li>
<a href="/logout"> <span class="glyphicon glyphicon-user"></span>
<strong>Log out</strong>
</a>
</li>
</ul>
</div>
</div>
</div>
<div class="container" class="row">
<div class="page-header" id="banner">
<div class="row">
<div class="col-lg-8 col-md-7 col-sm-6">
<img src="ban.png">
</div>
<div class="col-lg-4 col-md-5 col-sm-6">
<div class="sponsor"></div>
</div>
</div>
</div>
</div>
<div class="container"></div>
</body>
<script th:inline="javascript">
$(document).ready(function () {
var panels = $('.user-infos');
var panelsButton = $('.dropdown-user');
panels.hide();
//Click dropdown
panelsButton.click(function () {
//get data-for attribute
var dataFor = $(this).attr('data-for');
var idFor = $(dataFor);
//current button
var currentButton = $(this);
idFor.slideToggle(400, function () {
//Completed slidetoggle
if (idFor.is(':visible')) {
currentButton.html('<i class="glyphicon glyphicon-
chevron - up
text - muted
"></i>');
}
else {
currentButton.html('<i class="glyphicon glyphicon-
chevron - down
text - muted
"></i>');
}
})
});
$('[data-toggle="tooltip"]').tooltip();
$('button').click(function (e) {
e.preventDefault();
alert("This is a demo.\n :-)");
});
});
</script>
</html>
POM file:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.finalYearProject</groupId>
<artifactId>student-life</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>student-life</name>
<description>Jill's Student Life Final Year Project</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.8.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-
8
</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<!-- upgrade to thymeleaf version 3 -->
<thymeleaf.version>3.0.8.RELEASE</thymeleaf.version>
<thymeleaf-layout-dialect.version>2.2.2</thymeleaf-layout-
dialect.version>
<thymeleaf-extras-springsecurity4.version>3.0.2.RELEASE</thymeleaf-
extras-springsecurity4.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- <dependency>
<groupId>org.webjars</groupId>
<artifactId>bootstrap-datepicker</artifactId>
<version>1.0.1</version>
</dependency>
<dependency>
<groupId>org.webjars</groupId>
<artifactId>bootstrap</artifactId>
<version>3.3.7</version>
</dependency> -->
<dependency>
<groupId>org.webjars</groupId>
<artifactId>bootstrap</artifactId>
<version>3.3.7</version>
</dependency>
<dependency>
<groupId>org.webjars</groupId>
<artifactId>jquery</artifactId>
<version>3.2.1</version>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.thymeleaf.extras</groupId>
<artifactId>thymeleaf-extras-springsecurity4</artifactId>
</dependency>
<dependency>
<groupId>commons-beanutils</groupId>
<artifactId>commons-beanutils</artifactId>
</dependency>
<dependency>
<groupId>org.webjars</groupId>
<artifactId>bootstrap</artifactId>
<version>3.3.7</version>
</dependency>
<dependency>
<groupId>org.webjars</groupId>
<artifactId>jquery</artifactId>
<version>3.2.1</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.hsqldb</groupId>
<artifactId>hsqldb</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
</dependency>
<!-- <dependency>
<groupId>org.springframework.session</groupId>
<artifactId>spring-session-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.session</groupId>
<artifactId1>spring-session</artifactId>
<version>1.2.2.RELEASE</version>
</dependency> -->
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Project structure:
The exception will give the line # in the html where the error is occurring... in any case, the actual problem is here:
<div class="container" class="row">
Thymeleaf won't allow an element to have two attributes with the same name (Attribute "class" appears more than once in element). Just change it to:
<div class="container row">

form:checkbox/form:<anything> not rendering/displaying in Spring 4, input type="radio”/input type="checkbox" does

I have been asked to upgrade an application that was using Spring 3 (3.1.4.RELEASE and 3.2.5.RELEASE in POM) and have gone to (4.2.2.RELEASE and 4.3.8.RELEASE) respectively (for numerous dependencies. The application used to render correctly, show the JSP but now using any of the existing form:checkbox/form:radiobutton/form:input/form:errors work as the page is just blank, no errors. When I re-write the code and use input type="checkbox”/input type="radio" etc the page now shows ok.
I have looked around loads of examples/forums but nothing I have tried has helped. Am I missing some configuration change elsewhere that is needed? The server is WildFly too, but the application worked fine on this using Spring 3 dependencies.
Example of the JSP (with form:radiobutton/form:checkbox commented out and the other code below it that displays ok), called manageUser.jsp :-
<%# taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<%# taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>
<%# taglib prefix="c" uri="http://java.sun.com/jstl/core_rt"%>
<%# taglib prefix="spring" uri="http://www.springframework.org/tags"%>
<%# taglib uri = "http://www.springframework.org/tags/form" prefix = "form"%>
<div class="contentWrapper">
<fieldset>
<c:if test="${infoFlag}">
<div id="pageMessage">
<p><spring:message code="${infoMessage}"/></p>
</div>
</c:if>
<c:if test="${successFlag !=null && successFlag}">
<div class='message glbMsg success ajaxMsg'>
<span class="infoicon"> </span><p><spring:message code="${successMessage}"/></p>
</div>
</c:if>
<div>
<h3>Manage Users</h3>
<p id="pageIntro">
This section allows you to manage or create users and assign users to roles.
</p>
<div class="col">
<div id="user-tab-container" class="user-tab-container manage-user-page">
<ul class='etabs'>
<li class='usertab'>Manage User</li>
<li class='usertab'>Create User</li>
<li class='usertab' style="display:none;">User Search</li>
</ul>
<div class='panel-container'>
<div id="manage-user">
<form:form id="searchUser" class="singleColForm" method="POST" action="${pageContext.request.contextPath}/admin/searchUser.do" modelAttribute="user">
<div class="col mrgTop10">
<div class="col w2">
<div class="frmFld">
<label for="lastName">Last Name</label>
<input id="lastName" name="lastName" type="text" class="alphanumeric" maxlength="45" value="${user.lastName}" />
</div>
<div class="frmFld">
<label for="firstName">First Name</label>
<input id="firstName" name="firstName" type="text" class="alphanumeric" maxlength="45" value="${user.firstName}" />
</div>
<div class="frmFld">
<label for="eMail">E-mail</label>
<input id="eMail" name="eMail" type="text" class="email" maxlength="100" value="${user.eMail}" />
</div>
<div class="frmFld multiRadio">
<label>Status</label>
<div class="scrollableUserGroup">
<span class="radio">
<input id="userStatus[All]" name="userStatus" type="radio" value="-1" checked/>
<label for="userStatus[All]">All</label>
</span>
<!-- Original that doesn't work now -->
<%-- <c:forEach var="entry" items="${userStatusList}"> --%>
<%-- <span class="radio"> <form:radiobutton --%>
<%-- id="userStatus[${entry.name}]" name="userStatus" --%>
<%-- path="userStatus" value="${entry.id}" /> <label --%>
<%-- for="userStatus[${entry.name}]"><c:out value="${entry.name}"/></label> --%>
<!-- </span> -->
<%-- </c:forEach> --%>
<c:forEach var="entry" items="${userStatusList}">
<span class="radio">
<input type="radio" id="userStatus[${entry.name}]" name="userStatus" value="${entry.id}">
<label for="userStatus[${entry.name}]"><c:out value="${entry.name}"/></label>
</span>
</c:forEach>
</div>
</div>
</div>
<div class="col w2">
<label>User Groups</label>
<div class="frmFld">
<div class="scrollableUserGroup scrollableUserGroups">
<!-- Original that doesn't work now -->
<%-- <c:forEach items="${userGroup}" var="group"> --%>
<!-- <div class="userCB"> -->
<!-- <span class="customCB checkbox"> -->
<%-- <form:checkbox path="userGroups" value="${group.id}" /> --%>
<!-- <span class="box"> -->
<!-- <span class="tick"> </span> -->
<!-- </span> -->
<!-- </span> -->
<%-- <label class="breakword" style="margin-left: 20px;"><c:out value="${group.name}"/></label> --%>
<!-- </div> -->
<%-- </c:forEach> --%>
<c:forEach items="${userGroup}" var="group">
<div class="userCB">
<span class="customCB checkbox">
<input type="checkbox" value="${group.id}" name="userGroups" id="${lstPermission.id}">
<span class="box">
<span class="tick"> </span>
</span>
</span>
<label class="breakword" style="margin-left: 20px;"><c:out value="${group.name}"/></label>
</div>
</c:forEach>
</div>
</div>
</div>
</div>
<div class="buttons">
<a class="btn Big searchUser" href="${pageContext.request.contextPath}/admin/searchUser.do">Search user <span> </span></a>
<span class="hidden"> </span>
</div>
</form:form>
</div>
<div id="create-user">
<!-- Create user -->
</div>
<div id="search-user">
<!-- search user -->
</div>
</div>
</div>
</div>
</div>
</fieldset>
</div>
In another file called tiles-definition.xml it has the JSP filename:-
<definition name="manageUser" template="/WEB-INF/tiles/layout/user/manageUser.jsp" />
In the Controller, it has (with irrelevant other parts removed):
#Controller
#RequestMapping("/admin")
public class UserControllerImpl extends AbstractCommonController implements UserController {
Below is used in JSP
#ModelAttribute("userStatusList")
public Set<UserStatus> populateUserStatus(){
return userHelper.getUserStatusCollection();
}
#ModelAttribute("userGroup")
public Set<UserGroup> populateUserGroup(){
return userHelper.getUserGroupCollection();
}
…
#RequestMapping("/manageUser.do")
public String manageUser(Map<String, Object> model, HttpServletRequest request){
model.put("user", new UserTo());
return "manageUser";
}
…
}
dispatcher-servlet.xml (has the below in it along with some other parts that are needed):-
…
<bean id="jspViewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
<property name="order" value="1"/>
</bean>
POM.xml has (old version commented out, new added in) :-
…
<!-- Spring web dependency -->
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-core</artifactId>
<!-- <version>3.1.4.RELEASE</version> -->
<version>4.2.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-config</artifactId>
<!-- <version>3.1.4.RELEASE</version> -->
<version>4.2.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-taglibs</artifactId>
<!-- <version>3.1.4.RELEASE</version> -->
<version>4.2.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-web</artifactId>
<!-- <version>3.1.4.RELEASE</version> -->
<version>4.2.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<!-- <version>3.2.5.RELEASE</version> -->
<version>4.3.8.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-expression</artifactId>
<!-- <version>3.2.5.RELEASE</version> -->
<version>4.3.8.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<!-- <version>3.2.5.RELEASE</version> -->
<version>4.3.8.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<!-- <version>3.2.5.RELEASE</version> -->
<version>4.3.8.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<!-- <version>3.2.5.RELEASE</version> -->
<version>4.3.8.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<!-- <version>3.2.5.RELEASE</version> -->
<version>4.3.8.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<!-- <version>3.2.5.RELEASE</version> -->
<version>4.3.8.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<!-- <version>3.2.5.RELEASE</version> -->
<version>4.3.8.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<!-- <version>3.2.5.RELEASE</version> -->
<version>4.3.8.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<!-- <version>3.2.5.RELEASE</version> -->
<version>4.3.8.RELEASE</version>
</dependency>
Any help is much appreciated as I have been thrown in the deep end not knowing too much about Spring and would like to understand and get the original form:radiobutton/form:checkbox/etc working (displaying) and not use the re-written style. Is anything else required for suggestions?

isAuthenticated and isAnonymous are returning false simultaneously

In my current spring project, I had a view with this html code:
<th:block sec:authorize="isAuthenticated()">
<h2 sec:authentication="name"></h2>
</th:block>
<th:block sec:authorize="isAnonymous()">
<p> <a th:href="#{/loginPage}">Login</a> </p>
<p>
<ul>
<li> <a th:href="#{/homeFacebook}">Connect to Facebook</a> </li>
<li> <a th:href="#{/homeTwitter}">Connect to Twitter</a> </li>
</ul>
</p>
</th:block>
which when I run the application, nothing is being displayed in the page, because the conditionals sec:authorize="isAuthenticated()" and sec:authorize="isAnonymous()" are both returnin false;
My configuration:
At my Application.java class
#Bean
public SpringTemplateEngine templateEngine() {
SpringTemplateEngine engine = new SpringTemplateEngine();
engine.addDialect( new SpringSecurityDialect() );
return engine;
}
At my pom.xml file
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
<version>1.3.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf-spring3</artifactId>
<version>3.0.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.thymeleaf.extras</groupId>
<artifactId>thymeleaf-extras-springsecurity4</artifactId>
<version>2.1.2.RELEASE</version>
</dependency>
I have other projects whit similar configuration working fine, but in this one i stuck this issue. Someone can see what's wrong here?
update
I change the pom.xml to this:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
<version>1.3.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf-spring3</artifactId>
<version>LATEST</version>
</dependency>
<dependency>
<groupId>org.thymeleaf.extras</groupId>
<artifactId>thymeleaf-extras-springsecurity4</artifactId>
<version>LATEST</version>
</dependency>
and now instead of show nothing, both blocks are being displayed. (if i remove thymeleaf-spring3 from list of dependencies in the pom.xml file, the page display none of the blocks, like before)
update 2
with this code, the page display nothing:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.thymeleaf.extras</groupId>
<artifactId>thymeleaf-extras-springsecurity4</artifactId>
</dependency>
with that one display both blocks:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.thymeleaf.extras</groupId>
<artifactId>thymeleaf-extras-springsecurity3</artifactId>
<version>LATEST</version>
</dependency>
but sec:authentication is not processed.

Resources