Thursday, 1 March 2018

15.0 Java's Standard Bean Validation For Form - is required

Java's Standard Bean Validation For Form


Spring version 4 and higher supports Bean Validation


1. Require validation example (DOWNLOAD)





















-------------------------------------------------------------------------------------------------------------
Programmer.java // this is our POJO class where we'll add all the validations
-------------------------------------------------------------------------------------------------------------
package com.spring.demo;

public class Programmer {
String firstName;
String lastName;
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}

}


-------------------------------------------------------------------------------------------------------------
ProgramController.java //this is the servlet dispatcher which is our controller
-------------------------------------------------------------------------------------------------------------
package com.spring.demo;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class ProgramController {

@RequestMapping("/")
public String showLanding()
{
return "landing";
}
@RequestMapping("/Form")
public String showForm(Model model)
{
Programmer obj=new Programmer();
model.addAttribute(obj);
return "form";
}
@RequestMapping("/processForm")
public String processForm(@ModelAttribute("programmer") Programmer p,BindingResult br)
{
return "output";
}
}


-------------------------------------------------------------------------------------------------------------
landing.jsp
-------------------------------------------------------------------------------------------------------------
<html>
<head>
<title>Landing Page</title>
</head>
<body>
<h2>Welcome to the Spring MVC Practice tutorial.</h2>
<br>
<h3><a href="Form">CLICK HERE TO FILL THE PROGRAMMER'S FORM</a></h3>
</body>
</html>



-------------------------------------------------------------------------------------------------------------
form.jsp
-------------------------------------------------------------------------------------------------------------
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<html>
<head>
<title>Form</title>
</head>
<body bgcolor="grey">
<form:form action="processForm" modelAttribute="programmer">
<table border="1" style="border-collapse: collapse;background-color: white;">
<tr>
<td colspan="2" align="center">PROGRAMMER'S PROFILE</td>
</tr>
<tr>
<td>FIRST NAME </td>
<td><form:input path="firstName" /></td>
</tr>
<tr>
<td>LAST NAME </td>
<td><form:input path="lastName" /></td>
</tr>
<tr>
<td></td>
<td><input type="submit" value="SUBMIT"></td>
</tr>
</table>
</form:form>
</body>
</html>



-------------------------------------------------------------------------------------------------------------
output.jsp
-------------------------------------------------------------------------------------------------------------
<html>
<head>
<title>Landing Page</title>
</head>
<body bgcolor="grey">
<table border="1" style="border-collapse: collapse;background-color: white;">
<tr>
<td colspan="2" align="center">PROGRAMMER'S PROFILE</td>
</tr>
<tr>
<td>FIRST NAME </td>
<td>${programmer.firstName}</td>
</tr>
<tr>
<td>LAST NAME </td>
<td>${programmer.lastName}</td>
</tr>
</table>

</body>
</html>



-------------------------------------------------------------------------------------------------------------
Programmer.java
-------------------------------------------------------------------------------------------------------------
package com.spring.demo;

import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;

public class Programmer {

@NotNull
@Size(min=1,message="is required")
String firstName;

@NotNull
@Size(min=1,message="is required")
String lastName;
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}

}



-------------------------------------------------------------------------------------------------------------
form.jsp
-------------------------------------------------------------------------------------------------------------
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<html>
<head>
<title>Form Page</title>
<style type="text/css">
.error{color:red}
</style>
</head>
<body bgcolor="grey">
<form:form action="processForm" modelAttribute="programmer">
<table border="1" style="border-collapse: collapse;background-color: white;">
<tr>
<td colspan="2" align="center">PROGRAMMER'S PROFILE</td>
</tr>
<tr>
<td>FIRST NAME (*)</td>
<td>
<form:input path="firstName" />
<form:errors path="firstName" cssClass="error" />
</td>
</tr>
<tr>
<td>LAST NAME (*)</td>
<td>
<form:input path="lastName" />
<form:errors path="lastName" cssClass="error" />
</td>
</tr>
<tr>
<td></td>
<td><input type="submit" value="SUBMIT"></td>
</tr>
</table>
</form:form>
</body>
</html>


-------------------------------------------------------------------------------------------------------------
ProgramController.java
-------------------------------------------------------------------------------------------------------------
package com.spring.demo;

import javax.validation.Valid;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class ProgramController {

@RequestMapping("/")
public String showLanding()
{
return "landing";
}
@RequestMapping("/Form")
public String showForm(Model model)
{
Programmer obj=new Programmer();
model.addAttribute(obj);
return "form";
}
@RequestMapping("/processForm")
public String processForm(@Valid @ModelAttribute("programmer") Programmer p,BindingResult br)
{
if(br.hasErrors())
{
return "form";
}
else
{
return "output";
}
}
}

No comments:

Post a Comment