When I tried to validate with Controller using Hibernate Validator in WASLiberty environment, only the fields annotated with Hibernate Validator were not checked. It seems that some people are having trouble with similar events even though they have been resolved, so I will leave it as a memo.
First of all, from a simple configuration. Controller
package com.atu496.sample.pl.controller;
@Controller
public class MainController {
@GetMapping("/")
public ModelAndView get(@ModelAttribute SampleForm form, ModelAndView mav) {
mav.addObject("form", form);
mav.setViewName("index");
return mav;
}
@PostMapping("/exe/validation")
public String exe(@ModelAttribute @Valid SampleForm form, BindingResult result, RedirectAttributes attribute) {
if (result.hasErrors()) {ß
for (ObjectError error : result.getAllErrors()) {
String fieldErrors = ((FieldError) error).getField();
attribute.addFlashAttribute(fieldErrors + "errormsg", error.getDefaultMessage());
}
}
return "redirect:/";
}
}
Form
import org.hibernate.validator.constraints.Email;
import org.hibernate.validator.constraints.Length;
import org.hibernate.validator.constraints.NotBlank;
import org.hibernate.validator.constraints.NotEmpty;
import lombok.Data;
@Data
public class SampleForm {
@Length(min = 8, max = 20)
private String length;
@NotBlank
private String notblank;
@NotEmpty
private String notempty;
@Email
private String email;
}
Thymeleaf
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Hibernate Validator validation</title>
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet"
href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"
integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u"
crossorigin="anonymous">
<!-- Optional theme -->
<link rel="stylesheet"
href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css"
integrity="sha384-rHyoN1iRsVXV4nD0JutlnGaslCJuC7uwjduW9SVrLvRYooPp2bWYgmgJQIXwl/Sp"
crossorigin="anonymous">
<!-- Latest compiled and minified JavaScript -->
<script
src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"
integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa"
crossorigin="anonymous"></script>
</head>
<body>
<div class="container">
<div class="row">
<div class="jumbotron">
<h1>Hibernate Validator validation</h1>
<p>Hibernate Validator verification demo page.</p>
</div>
<div class="panel panel-success">
<div class="panel-heading">Validation check</div>
<div class="panel-body">
<form method="post" action="#" th:action="@{/exe/validation}">
<p>
@Length:<input type="text" name="length" /><span
th:text="${lengtherrormsg}"></span>
</P>
<p>
@Email:<input type="text" name="email" /><span
th:text="${emailerrormsg}"></span>
</P>
<p>
@NotEmpty:<input type="text" name="notnull" /><span
th:text="${notemptyerrormsg}"></span>
</P>
<p>
@NotBlank:<input type="text" name="notblank" /><span
th:text="${notblankerrormsg}"></span>
</p>
<button type="submit" class="btn btn-default">Run</button>
</form>
</div>
</div>
</div>
</div>
</body>
</html>
In this state, deploy to WAS Liberty and try it.
For some reason, only Not Empty is executed. .. ..
Try putting the same application on Tomcat. Yeah, it works. .. .. Why orz
The problem seems to be that the Hibernate Validator is not recognized by the WASLiberty class loader. By adding the following validation.xml to META-INF of the target project so that Hibernate Validator is used preferentially, validation was executed successfully. validation.xml
<?xml version="1.0" encoding="UTF-8"?>
<validation-config
xmlns="http://jboss.org/xml/ns/javax/validation/configuration"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://jboss.org/xml/ns/javax/validation/configuration http://www.jboss.org/xml/ns/javax/validation/configuration/validation-configuration-1.0.xsd">
<default-provider>org.hibernate.validator.HibernateValidator</default-provider>
</validation-config>
For reference, upload this verification application to Github. The event did not hit easily and I managed to solve it through trial and error, but since I spent a lot of time on this investigation, I would like people who are stuck with similar events to refer to it.
Recommended Posts