struts jsp传入的值,form是否自动检查类型

来源:百度知道 编辑:UC知道 时间:2024/05/12 00:45:57
jsp传入的值,form是否自动检查类型
如jsp传入的String类型<input type = "text" name ="id" >
但在form中id是int类型,是否自动检查类型,如果不是在ActionErrors怎么写
public ActionErrors validate(ActionMapping actionMapping, HttpServletRequest httpServletRequest) {

}
哦,我好想还没说清楚,如果传入一String,如(abc),form会不会报错,要怎么写这个异常呢,谢谢

可以通过request.getParameter("id").trim()读取String类型的信息,然后再通过Integer.parseInt()方法转成int,如果需要在ActionErrors写的话,可以在格式转换是捕捉异常NumberFormatException,创建一个ActionErrors通过add方法传入ActionErrors中,以下是例子(实现用户名和密码的输入检查):
public ActionErrors validate(ActionMapping mapping,
HttpServletRequest request) {

ActionErrors aes = new ActionErrors();

//这个方法可以加入一个有效性的检查
if(username.trim().length()<1||username == null){
//产生一个错误ActionMessage对象,加入到错误对象集合中
aes.add("username",new ActionMessage("no.username"));
}

if(userpassword.trim().length()<1||userpassword == null){
//产生一个错误ActionMessage对象,加入到错误对象集合中
aes.add("userpassword",new ActionMessage("no.userpassword"));
}
System.out.println("validate");
return aes;
}