最近看到夏昕《Spring开发指南》的“输入验证与数据绑定”一节。相关代码如下:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<!--Definition of View Resolver -->
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass">
<value>org.springframework.web.servlet.view.JstlView</value>
</property>
<property name="prefix">
<value>/WEB-INF/view/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
<!-- 输入验证与数据绑定 -->
<bean id="RegisterValidator" class="net.xiaxin.validator.RegisterValidator"/>
<bean id="RegisterAction" class="net.xiaxin.action.RegisterAction">
<property name="commandClass">
<value>net.xiaxin.reqbean.RegisterInfo</value>
</property>
<property name="validator">
<ref local="RegisterValidator"/>
</property>
<property name="formView">
<value>register</value>
</property>
<property name="successView">
<value>RegisterSuccess</value>
</property>
</bean>
<!--Request Mapping -->
<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="/register.do">RegisterAction</prop>
</props>
</property>
</bean>
<!---Action Definition-->
<bean id="LoginAction" class="net.xiaxin.action.LoginAction">
<!--
<property name="sessionForm"><value>true</value></property>
<property name="commandName"><value>LoginInfo</value></property>
-->
<property name="commandClass">
<value>net.xiaxin.action.LoginInfo</value>
</property>
<property name="fail_view">
<value>loginfail</value>
</property>
<property name="success_view">
<value>main</value>
</property>
</bean>
</beans>
///////////////////////////////////////////////////////////////////////////////////////
package net.xiaxin.validator;
import net.xiaxin.reqbean.RegisterInfo;
import org.springframework.validation.Errors;
import org.springframework.validation.Validator;
public class RegisterValidator implements Validator {
public boolean supports(Class clazz) {
return RegisterInfo.class.isAssignableFrom(clazz);
}
public void validate(Object obj, Errors errors) {
RegisterInfo regInfo = (RegisterInfo) obj;
// 检查注册用户名是否合法
if (regInfo.getUsername().length() < 4) {
//rejectVlaue(Error Code, Message Key, Error Argument, Default Message);
errors.rejectValue("username","less4chars",null,"用户名长度必须大于等于4个字母!");
}
/*检查用户名是否已经存在
if (UserDAO.getUser(regInfo.getUsername()) != null) {
errors.rejectValue("username",
"existed",
null,
"用户已存在!");
}
*/
if (regInfo.getPassword1().length() < 6) {
errors.rejectValue("password1","less6chars",null,"密码长度必须大于等于6个字母");
}
if (!regInfo.getPassword2().equals(regInfo.getPassword1()))
{
errors.rejectValue("password2","notsame",null,"两次输入的密码不一致!");
}
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
package net.xiaxin.reqbean;
public class RegisterInfo {
private String username;
private String password1;
private String password2;
public String getPassword1() {
return password1;
}
public void setPassword1(String password) {
this.password1 = password;
}
public String getPassword2() {
return password2;
}
public void setPassword2(String password) {
this.password2 = password;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
}
启动Tomcat, 直接访问URL:http://localhost:8080/springtest/register.do,出现报错界面,出错代码如下:
java.lang.NullPointerException
net.xiaxin.validator.RegisterValidator.validate(RegisterValidator.java:18)
RegisterValidator.java的第18行为:if (regInfo.getUsername().length() < 4) {
教程上说必须通过“…/register.do”访问注册界面,先通过SPRING完成一系列的初始化工作(如创建对应的状态对象并与之关联)之后,register.jsp才能顺利执行,否则我们会得到一个应用服务器内部错误。
可是如果最先访问register.do的话,register.jsp还没出现,当然里面的username,password1,password2为null了。这就有点互相矛盾了。是不是要在RegisterInfo 里面初始化呢?