I have a Java web project with an html page and a controller class.
(there is a REST resources configuration class that sets resources path to /resources
but it is irrelevant to the question)
html page:
<html>
<head>
<title>Start Page</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<h1>Hello World!</h1>
<form action="resources/postman" method="POST">
<label for="ageInput">Age:</label>
<input type="number" id="ageInput" name="age"/><br/><br/>
<input type="submit" value="Submit"/>
</form>
</body>
</html>
and here is the controller class:
@Controller
@Path("postman")
public class PostController {
@MvcBinding
@FormParam("age")
@Min(18)
private int age;
@Inject
private BindingResult bindingResult;
@Inject
private Models models;
@POST
public String processForm() {
if (bindingResult.isFailed()) {
models.put("errors", bindingResult.getAllMessages());
return "fail.jsp";
}
return "success.jsp";
}
}
The problem is no matter what value I provide in the ageInput
validation is always successful. I can provide both negative values as well as positive lower than 18 and I always am redirected to success.jsp
.
What can I do to trigger the validation?
I am using Eclipse Krezo as an implementation of Jakarta MVC api.
UPDATE: It seems that there is a need for @Valid annotation to be used for the validation to be triggered. The code below works as expected:
@POST
public String processForm(
@Valid
@FormParam("age")
@Min(18)
@MvcBinding
int age) {
if (bindingResult.isFailed()) {
return "fail.jsp";
}
return "success.jsp";
}
I have a Java web project with an html page and a controller class.
(there is a REST resources configuration class that sets resources path to /resources
but it is irrelevant to the question)
html page:
<html>
<head>
<title>Start Page</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<h1>Hello World!</h1>
<form action="resources/postman" method="POST">
<label for="ageInput">Age:</label>
<input type="number" id="ageInput" name="age"/><br/><br/>
<input type="submit" value="Submit"/>
</form>
</body>
</html>
and here is the controller class:
@Controller
@Path("postman")
public class PostController {
@MvcBinding
@FormParam("age")
@Min(18)
private int age;
@Inject
private BindingResult bindingResult;
@Inject
private Models models;
@POST
public String processForm() {
if (bindingResult.isFailed()) {
models.put("errors", bindingResult.getAllMessages());
return "fail.jsp";
}
return "success.jsp";
}
}
The problem is no matter what value I provide in the ageInput
validation is always successful. I can provide both negative values as well as positive lower than 18 and I always am redirected to success.jsp
.
What can I do to trigger the validation?
I am using Eclipse Krezo as an implementation of Jakarta MVC api.
UPDATE: It seems that there is a need for @Valid annotation to be used for the validation to be triggered. The code below works as expected:
@POST
public String processForm(
@Valid
@FormParam("age")
@Min(18)
@MvcBinding
int age) {
if (bindingResult.isFailed()) {
return "fail.jsp";
}
return "success.jsp";
}
Share
Improve this question
edited Mar 3 at 20:32
zajer
asked Mar 2 at 21:10
zajerzajer
8138 silver badges19 bronze badges
2
- What server are you running in? – James R. Perkins Commented Mar 3 at 17:41
- @JamesR.Perkins Payara 6.2024.9 – zajer Commented Mar 3 at 19:00
1 Answer
Reset to default 0A simple refactoring on your codes here.
Add a
@RequestScoped
on the controller class to make it available in request scope, in the future, Jakarta REST will switch to use CDI to replace its own Dependency injection.Extract the form binding data into a simple POJO, here, eg.
class PersonForm{ @NotBlank @FormParam("age") @MvcBinding private int age; // getters and settters }
Inject
Models
,ResultBinding
in the controller class. In the initial action, create a pojo instance and put in the models.@GET @Path("new") public String add() { PersonFormform = new PersonForm(); models.put("person", form); return "form.xhtml"; }
and on the submit method, add
@Valid @BeanParam PersonForm form
.@Post public Response save(@Valid @BeanParam PersonForm form) { log.log(Level.INFO, "saving new person@{0}", form); if (validationResult.isFailed()) { validationResult.getAllErrors() .forEach((ParamError t) -> { alert.addError(t.getParamName(), "", t.getMessage()); }); models.put("errors", alert); models.put("persom", form); return Response.status(BAD_REQUEST).entity("form.xhtml").build(); } // else read the form data, handle it, and redirect to list page.
In the pages, use el to bind the data bwteen POJO and html form. Thus the value will be kept in the form when there are validation failure.
// display errors // ... <input id="age" name="age" type="text" class="form-control" value="#{person.age}"/>
When navigating the views/pages, firstly go to postman/new
to display the initial form page, and add some data and submit form data via POST /postman
, if it is failed it will show the failed errors and stick on the form page.
Jakarta MVC copied most of the features of the Spring MVC and Apache Struts, it is a great alternative of these frameworks.
Check my Jakarta MVC example here.