Java Reference
In-Depth Information
Listing 3-32. WEB-INF/classes/messages_en_US.properties
mandatoryfirstname.policydetails.firstName=First name field is mandatory
mandatoryfirstname.policydetails.mandatorylastname=Last name field is mandatory
mandatoryfirstname.policydetails.mandatoryAge=Age å
field is mandatory and should be an integer(0-9)
Note that the message keys are different from the error keys. This is because the
MessageCodesResolver implementation converts the error key to append the command
name and field name. Finally, we also need to modify the JSP slightly to display validation
error messages alongside the corresponding fields. For this purpose, I will use the tag
library provided by Spring to simplify the development of JSP-based views. Listing 3-33
shows the modified JSP.
Listing 3-33. WEB-INF/jsp/createPolicy.jsp
<%@ taglib prefix="form" uri=" http://www.springframework.org/tags/form" %>
<%@ taglib prefix="c" uri=" http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri=" http://java.sun.com/jsp/jstl/fmt" %>
<html>
<head>
<title>Underwriting</title>
<style>
.error { color: red; }
</style>
</head>
<form:form action="" method="POST" commandName="policydetails">
First Name <form:input path="firstName"/>
<form:errors path="firstName" cssClass="error"/><br/>
Last Name <form:input path="lastName"/>
<form:errors path="lastName" cssClass="error"/><br/>
Age <form:input path="age"/> <form:errors path="age" cssClass="error"/><br/>
<input type="submit" value="Save" />
</form:form>
</body>
</html>
 
Search WWH ::




Custom Search