Friday, October 22, 2010

Spring with in a Day Part-7

Spring server side validation
------------------------------------------------------
Validation Rules:
• username is required and minimum length 5 characters.
• pwdOne an pwdTwo must be same.
• Age is required and must between 15 to 45

JSP code (urform.html):-
< %@ taglib prefix=”c” uri=”http://java.sun.com/jstl/core” %>
< %@ taglib prefix=”spring” uri=”http://www.springframework.org/tags” %>
< %@ page isELIgnored=”true” %>
< form method=”post” action=”ru.html”>
Name: < input type=”text” name=”username”>< /br>
Password: < input type=”text” name=”pwdOne”>< /br>
Retype Password: < input type=”text” name=”pwdTwo”>< /br>
Email: < input type=”text” name=”email”>< /br>
Age: < input type=”text” name=”age”>< /br>
< input type=”submit” value=”Register”>< /br>
< /form>



Command Class –
package pack;
public class UserRegCB {
String username;
String pwdOne;
String pwdTwo;
String email;
int age;
//add setter and getter for all properties
}

Note:
------------
class cat {}
class Rat {}
public class TessApp {
public static void main (String [] args) throws Exception {
Class c1 = Class.forName(“Cat”);
Class C2 = Class.froName(“Rat”);
System.out.println(Cat.class.isAssignableFrom(c1); //true
System.out.println(Cat.class.isAssifnableFrom(c2); //false
}
}
 Cat.class.isAssignableFrom() returns TRUE if the class c1 contains the information about Cat class or the information about the sub-class of the Cat class.



To take care of validation we must provide a class implementing the Validator interface.
package pack;
public class URCBValidator implements Validater {
public Boolean supports(Class cls) {
return pack.UserRegCB.class.isAssignableFrom(cls);
}
public void validate (Object cmd, Errors errors) {
UserRegCB cb = (UserRegCB) cmd;
If(cb.getUserName() == null || cb.getUserName().trim().equsals(“”)){
errors.rejectValue(“username”,”user.req.err”,”User is required”);
} else If(cb.getUserName().lenght < 5){
errors.rejectValue(“username”,”user.len.err”,
”User name less than 5 characters”);
}
If(cb.getPwdOne() == null || cb.getPwdOne().trim().equsals(“”)){
errors.rejectValue(“pwdOne”,”pwdOne.req.err”,
”Password is required”);
} else If(cb.getPwdTwo() == null || cb.getPwdTwo().trim().equsals(“”)){
errors.rejectValue(“pwdTwo”,”pwdTwo.req.err”,
”Password is required”);
} else If(cb.getPwdOne().equals(ch.getPwdTwo())){
errors.rejectValue(“pwdTwo”,”pwdTwo.same.err”,
”Retyped password is not same with Password”);
}
If(cb.getAge() == null || cb.getAge().trim().equsals(“”)){
errors.rejectValue(“age”,”age.req.err”,”Age is required”);
} else If(cb.getAge().lenght < 15 || cb.getAge().lenght > 45){
errors.rejectValue(“age”,”age.len.err”,”Age is not between 15 to 45”);
}
}
}

 The method supports() and Validate() will be called by the spring code.
 The supports() is used to check whether the Validator object can take care of validating the data available in a specific command bean object.
 And validate() is used to check whether the data is valid or not.

package pack;
public class URController extends AbstructCommandController {
public URController() {
this.setCommandName(“urcb”);
this.setCommandClass(UserRegCB.class);
this.setValidator(new URCBValidator());
}
protected ModelAndView handle (HttpServletRequest reuest,
HttpServletResponse response, Object cmd, BindException be)
throws Exception {
UserRegCB cb=(UserRegCB) cmd;
Map map = be.getModel();
ModelAndView mav=new ModelAndView();
mav.setViewName(“dispRes”);
mav.addAllObjects(map);
return mav;
}
}

spring-servlet.xml file –
< bean name=”/ru.htm” class=”pack.URController”>< /bean>

JSP code (dispReg.jsp):-
< %@ taglib prefix=”c” uri=”http://java.sun.com/jstl/core” %>
< %@ taglib prefix=”spring” uri=”http://www.springframework.org/tags” %>
< %@ page isELIgnored=”true” %>
< c:out value=”${requestScope.urcb.userName}”/>
< spring:bind path=”urcb.userName”>
< c:forEach var=”emsg” item=”${status.errorMessages}”>
< c:out value=”${emsg}” />< /br>
< /c:forEach>
< /spring:bind>

No comments:

Post a Comment