Do a small change in the previous example.
---------------------------------------------------------------------------------------------------------------
Programmer.java
---------------------------------------------------------------------------------------------------------------
package com.spring.demo.validation;
import java.util.LinkedHashMap;
import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Pattern;
import javax.validation.constraints.Size;
public class Programmer {
@NotNull
@Size(min=1,message="is required")
private String firstName;
@NotNull
@Size(min=1,message="is required")
private String lastName;
@NotNull(message="is required")
@Pattern(regexp="^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$",message="please enter a valid email id")
private String email;
@NotNull(message="is required")
@Min(value=0,message="must be greater than or equal to zero")
@Max(value=10,message="must be less than or equal to 10")
private int experience;
private String[] editor;
private String time;
private String language;
LinkedHashMap<String, String> expertise=new LinkedHashMap<>();
public Programmer(){
expertise=new LinkedHashMap<>();
expertise.put("JAVA", "JAVA");
expertise.put("CPP", "CPP");
expertise.put("PYTHON", "PYTHON");
expertise.put("PHP", "PHP");
expertise.put("ASP", "ASP");
}
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;
}
public String getTime() {
return time;
}
public void setTime(String time) {
this.time = time;
}
public LinkedHashMap<String, String> getExpertise() {
return expertise;
}
public String getLanguage() {
return language;
}
public void setLanguage(String language) {
this.language = language;
}
public String[] getEditor() {
return editor;
}
public void setEditor(String[] editor) {
this.editor = editor;
}
public int getExperience() {
return experience;
}
public void setExperience(int experience) {
this.experience = experience;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
---------------------------------------------------------------------------------------------------------------
form.jsp
---------------------------------------------------------------------------------------------------------------
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<html>
<head>
<title>FORM</title>
<style type="text/css">
.error{color: red;}
</style>
</head>
<body>
<h2>Welcome to Spring Validation Practice</h2>
<form:form action="processForm" modelAttribute="programmer">
<table>
<tr>
<td></td>
<td>PROGRAMMER</td>
<td></td>
</tr>
<tr>
<td>First Name (*)</td>
<td><form:input path="firstName" /></td>
<td><form:errors path="firstName" cssClass="error" /></td>
</tr>
<tr>
<td>Last Name (*)</td>
<td><form:input path="lastName"/></td>
<td><form:errors path="lastName" cssClass="error" /></td>
</tr>
<tr>
<td>Which time is good for coding</td>
<td>
<form:radiobutton path="time" value="Day"/>Day
<form:radiobutton path="time" value="Night"/>Night
</td>
<td></td>
</tr>
<tr>
<td>What is your Expertise </td>
<td>
<form:select path="language">
<form:options items="${programmer.expertise}" />
</form:select>
</td>
<td></td>
</tr>
<tr>
<td>Which is your Favourite Editor</td>
<td>
<form:checkbox path="editor" value="Notepad++"/>Notepad++
<form:checkbox path="editor" value="Edit++"/>Edit++
<form:checkbox path="editor" value="Sublime"/>Sublime
<form:checkbox path="editor" value="Comodo"/>Comodo
</td>
<td></td>
</tr>
<tr>
<td>Experience in coding (*)</td>
<td><form:input path="experience"/></td>
<td><form:errors path="experience" cssClass="error" /></td>
</tr>
<tr>
<td>Email (*)</td>
<td><form:input path="email"/></td>
<td><form:errors path="email" cssClass="error" /></td>
</tr>
<tr>
<td></td>
<td><input type="submit" value="SUBMIT"></td>
<td></td>
</tr>
</table>
</form:form>
</body>
</html>
---------------------------------------------------------------------------------------------------------------
output.jsp
---------------------------------------------------------------------------------------------------------------
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
<title>FORM</title>
<style type="text/css">
.error{color: red;}
</style>
</head>
<body>
<h2>Welcome to Spring Validation Practice</h2>
<table border=1 style="width: 50%;border-collapse: collapse;">
<tr><td colspan="2" align="center">PROGRAMMER</td></tr>
<tr>
<td>First Name</td>
<td>${programmer.firstName}</td>
</tr>
<tr>
<td>Last Name</td>
<td>${programmer.lastName}</td>
</tr>
<tr>
<td>Which time is good for coding</td>
<td>${programmer.time}</td>
</tr>
<tr>
<td>What is your Expertise</td>
<td>${programmer.language}</td>
</tr>
<tr>
<td>Which is your Favourite Editor</td>
<td><c:forEach items="${programmer.editor}" var="temp">${temp} , </c:forEach></td>
</tr>
<tr>
<td>Experience in coding</td>
<td>${programmer.experience}</td>
</tr>
<tr>
<td>Experience in coding</td>
<td>${programmer.email}</td>
</tr>
</table>
</body>
</html>
No comments:
Post a Comment