Prerequistes
Please read : Java spring – quickstart
Email Form Validation
EmailForm.java
public class EmailForm {
@Size(min=2, max=30, message="Size must be between 2 and 30.")
@Email(message="Malformed email address.")
@NotEmpty(message = "Please enter your email address.")
private String email;
public String getEmail() {
return this.email;
}
public void setEmail(String email) {
this.email = email;
}
} |
public class EmailForm {
@Size(min=2, max=30, message="Size must be between 2 and 30.")
@Email(message="Malformed email address.")
@NotEmpty(message = "Please enter your email address.")
private String email;
public String getEmail() {
return this.email;
}
public void setEmail(String email) {
this.email = email;
}
}
emailForm.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Email Form</title>
<style>
.error {
color: red; font-weight: bold;
}
</style>
</head>
<body>
<div align="center">
<h2 id="login-title">Email Form</h2>
<table border="0" width="90%">
<form:form action="email" commandName="emailForm">
<tr>
<td align="left" width="20%">Email: </td>
<td align="left" width="40%"><form:input path="email" size="30"/></td>
<td align="left"><form:errors path="email" cssClass="error"/></td>
</tr>
<tr>
<td></td>
<td align="center"><input type="submit" value="submit" id="submit-id"/></td>
<td></td>
</tr>
</form:form>
</table>
</div>
</body>
</html> |
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Email Form</title>
<style>
.error {
color: red; font-weight: bold;
}
</style>
</head>
<body>
<div align="center">
<h2 id="login-title">Email Form</h2>
<table border="0" width="90%">
<form:form action="email" commandName="emailForm">
<tr>
<td align="left" width="20%">Email: </td>
<td align="left" width="40%"><form:input path="email" size="30"/></td>
<td align="left"><form:errors path="email" cssClass="error"/></td>
</tr>
<tr>
<td></td>
<td align="center"><input type="submit" value="submit" id="submit-id"/></td>
<td></td>
</tr>
</form:form>
</table>
</div>
</body>
</html>
EmailFormController.java
@Controller
public class EmailFormController {
private static final Logger LOGGER = LoggerFactory.getLogger(EmailFormController.class);
@RequestMapping(value = "/email", method = RequestMethod.GET)
public String validateForm(Map<String, Object> model) {
EmailForm emailForm = new EmailForm();
model.put("emailForm", emailForm);
return "emailForm";
}
@RequestMapping(value = "/email", method = RequestMethod.POST)
public String checkForm(@Valid @ModelAttribute("emailForm") EmailForm emailForm,
BindingResult result) {
LOGGER.info("check Form email : "+emailForm.getEmail());
if (result.hasErrors()) {
return "emailForm";
}
return "formSuccess";
}
} |
@Controller
public class EmailFormController {
private static final Logger LOGGER = LoggerFactory.getLogger(EmailFormController.class);
@RequestMapping(value = "/email", method = RequestMethod.GET)
public String validateForm(Map<String, Object> model) {
EmailForm emailForm = new EmailForm();
model.put("emailForm", emailForm);
return "emailForm";
}
@RequestMapping(value = "/email", method = RequestMethod.POST)
public String checkForm(@Valid @ModelAttribute("emailForm") EmailForm emailForm,
BindingResult result) {
LOGGER.info("check Form email : "+emailForm.getEmail());
if (result.hasErrors()) {
return "emailForm";
}
return "formSuccess";
}
}
Password Form Validation
PasswordForm.java
public class PasswordForm {
@NotEmpty(message = "Please enter your password.")
@Size(min = 6, max = 15, message = "Your password must between 6 and 15 characters.")
private String password;
/**
* @return the password
*/
public String getPassword() {
return password;
}
/**
* @param password the password to set
*/
public void setPassword(String password) {
this.password = password;
}
} |
public class PasswordForm {
@NotEmpty(message = "Please enter your password.")
@Size(min = 6, max = 15, message = "Your password must between 6 and 15 characters.")
private String password;
/**
* @return the password
*/
public String getPassword() {
return password;
}
/**
* @param password the password to set
*/
public void setPassword(String password) {
this.password = password;
}
}
passwordForm.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Password Form</title>
<style>
.error {
color: red; font-weight: bold;
}
</style>
</head>
<body>
<div align="center">
<h2 id="login-title">Password Form</h2>
<table border="0" width="90%">
<form:form action="password" commandName="passwordForm">
<tr>
<td align="left" width="20%">password: </td>
<td align="left" width="40%"><form:password path="password" size="30"/></td>
<td align="left"><form:errors path="password" cssClass="error"/></td>
</tr>
<tr>
<td></td>
<td align="center"><input type="submit" value="submit" id="submit-id"/></td>
<td></td>
</tr>
</form:form>
</table>
</div>
</body>
</html> |
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Password Form</title>
<style>
.error {
color: red; font-weight: bold;
}
</style>
</head>
<body>
<div align="center">
<h2 id="login-title">Password Form</h2>
<table border="0" width="90%">
<form:form action="password" commandName="passwordForm">
<tr>
<td align="left" width="20%">password: </td>
<td align="left" width="40%"><form:password path="password" size="30"/></td>
<td align="left"><form:errors path="password" cssClass="error"/></td>
</tr>
<tr>
<td></td>
<td align="center"><input type="submit" value="submit" id="submit-id"/></td>
<td></td>
</tr>
</form:form>
</table>
</div>
</body>
</html>
PasswordFormController.java
@Controller
public class PasswordFormController {
private static final Logger LOGGER = LoggerFactory.getLogger(PasswordFormController.class);
@RequestMapping(value = "/password", method = RequestMethod.GET)
public String validateForm(Map<String, Object> model) {
PasswordForm passwordForm = new PasswordForm();
model.put("passwordForm", passwordForm);
return "passwordForm";
}
@RequestMapping(value = "/password", method = RequestMethod.POST)
public String checkForm(@Valid @ModelAttribute("passwordForm") PasswordForm passwordForm,
BindingResult result) {
LOGGER.info("check Form password : "+passwordForm.getPassword());
if (result.hasErrors()) {
return "passwordForm";
}
return "formSuccess";
}
} |
@Controller
public class PasswordFormController {
private static final Logger LOGGER = LoggerFactory.getLogger(PasswordFormController.class);
@RequestMapping(value = "/password", method = RequestMethod.GET)
public String validateForm(Map<String, Object> model) {
PasswordForm passwordForm = new PasswordForm();
model.put("passwordForm", passwordForm);
return "passwordForm";
}
@RequestMapping(value = "/password", method = RequestMethod.POST)
public String checkForm(@Valid @ModelAttribute("passwordForm") PasswordForm passwordForm,
BindingResult result) {
LOGGER.info("check Form password : "+passwordForm.getPassword());
if (result.hasErrors()) {
return "passwordForm";
}
return "formSuccess";
}
}