Spring输入验证与数据绑定.docx
《Spring输入验证与数据绑定.docx》由会员分享,可在线阅读,更多相关《Spring输入验证与数据绑定.docx(23页珍藏版)》请在冰豆网上搜索。
Spring输入验证与数据绑定
Spring输入验证与数据绑定
Spring输入验证与数据绑定v/:
*{behavior:
url(#default#VML);}
o/:
*{behavior:
url(#default#VML);}
w/:
*{behavior:
url(#default#VML);}
.shape{behavior:
url(#default#VML);}
--
/*FontDefinitions*/
@font-face
{font-family:
Wingdings;
panose-1:
5000000000;
mso-font-charset:
2;
mso-generic-font-family:
auto;
mso-font-pitch:
variable;
mso-font-signature:
026843545600-21474836480;}
@font-face
{font-family:
宋体;
panose-1:
2160311111;
mso-font-alt:
SimSun;
mso-font-charset:
134;
mso-generic-font-family:
auto;
mso-font-pitch:
variable;
mso-font-signature:
31351352321602621450;}
@font-face
{font-family:
"/@宋体";
panose-1:
2160311111;
mso-font-charset:
134;
mso-generic-font-family:
auto;
mso-font-pitch:
variable;
mso-font-signature:
31351352321602621450;}
@font-face
{font-family:
"新宋体/,Bold";
panose-1:
0000000000;
mso-font-alt:
黑体;
mso-font-charset:
134;
mso-generic-font-family:
auto;
mso-font-format:
other;
mso-font-pitch:
auto;
mso-font-signature:
11351352321602621440;}
@font-face
{font-family:
"宋体/,Bold";
panose-1:
0000000000;
mso-font-alt:
黑体;
mso-font-charset:
134;
mso-generic-font-family:
auto;
mso-font-format:
other;
mso-font-pitch:
auto;
mso-font-signature:
11351352321602621440;}
@font-face
{font-family:
CourierNew;
panose-1:
0000000000;
mso-font-alt:
黑体;
mso-font-charset:
134;
mso-generic-font-family:
auto;
mso-font-format:
other;
mso-font-pitch:
auto;
mso-font-signature:
11351352321602621440;}
@font-face
{font-family:
"CourierNew/,Bold";
panose-1:
0000000000;
mso-font-alt:
黑体;
mso-font-charset:
134;
mso-generic-font-family:
auto;
mso-font-format:
other;
mso-font-pitch:
auto;
mso-font-signature:
11351352321602621440;}
@font-face
{font-family:
"/@新宋体/,Bold";
panose-1:
0000000000;
mso-font-charset:
134;
mso-generic-font-family:
auto;
mso-font-format:
other;
mso-font-pitch:
auto;
mso-font-signature:
11351352321602621440;}
@font-face
{font-family:
"/@宋体/,Bold";
panose-1:
0000000000;
mso-font-charset:
134;
mso-generic-font-family:
auto;
mso-font-format:
other;
mso-font-pitch:
auto;
mso-font-signature:
11351352321602621440;}
@font-face
{font-family:
"/@CourierNew";
panose-1:
0000000000;
mso-font-charset:
134;
mso-generic-font-family:
auto;
mso-font-format:
other;
mso-font-pitch:
auto;
mso-font-signature:
11351352321602621440;}
@font-face
{font-family:
"/@CourierNew/,Bold";
panose-1:
0000000000;
mso-font-charset:
134;
mso-generic-font-family:
auto;
mso-font-format:
other;
mso-font-pitch:
auto;
mso-font-signature:
11351352321602621440;}
/*StyleDefinitions*/
p.MsoNormal,li.MsoNormal,div.MsoNormal
{mso-style-parent:
"";
margin:
0cm;
margin-bottom:
.0001pt;
text-align:
justify;
text-justify:
inter-ideograph;
mso-pagination:
none;
font-size:
10.5pt;
mso-bidi-font-size:
12.0pt;
font-family:
"TimesNewRoman";
mso-fareast-font-family:
宋体;
mso-font-kerning:
1.0pt;}
/*PageDefinitions*/
@page
{mso-page-border-surround-header:
no;
mso-page-border-surround-footer:
no;}
@pageSection1
{size:
595.3pt841.9pt;
margin:
72.0pt90.0pt72.0pt90.0pt;
mso-header-margin:
42.55pt;
mso-footer-margin:
49.6pt;
mso-paper-source:
0;
layout-grid:
15.6pt;}
div.Section1
{page:
Section1;}
/*ListDefinitions*/
@listl0
{mso-list-id:
411119607;
mso-list-type:
hybrid;
mso-list-template-ids:
-11104116906111016886769871367698715676987036769871367698715676987036769871367698715;}
@listl0:
level1
{mso-level-number-format:
alpha-lower;
mso-level-text:
"%1/)";
mso-level-tab-stop:
18.75pt;
mso-level-number-position:
left;
margin-left:
18.75pt;
text-indent:
-18.75pt;
font-family:
"CourierNew";
mso-fareast-font-family:
"新宋体/,Bold";}
ol
{margin-bottom:
0cm;}
ul
{margin-bottom:
0cm;}
-->
输入验证与数据绑定
实例目标:
实现用户注册功能。
流程:
1.提供一个界面供用户输入注册信息,下面是一个简化的注册界面,仅提供了用
户名和密码的设置
<!
--[if!
vml]--><!
--[endif]-->
2.如果用户注册信息有误,显示错误界面,要求用户检查输入后重新注册。
<!
--[if!
vml]--><!
--[endif]-->
3.注册成功,显示操作成功提示。
<!
--[if!
vml]--><!
--[endif]-->
实例内容
<!
--[if!
supportLists]-->a)<!
--[endif]-->配置文件
首先,web.xml文件配置分发器如下:
<?
xmlversion="1.0"encoding="UTF-8"?
>
<web-appversion="2.4"xmlns="
xmlns:
xsi="http:
//www.w3.org/2001/XMLSchema-instance"
xsi:
schemaLocation="
<servlet>
<servlet-name>Dispatcher</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/Config.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>Dispatcher</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<error-page>
<exception-type>java.lang.Throwable</exception-type>
<location>/WEB-INF/jsp/errorpage.jsp</location>
</error-page>
<error-page>
<exception-type>500</exception-type>
<location>/WEB-INF/jsp/errorpage.jsp</location>
</error-page>
</web-app>
在这个实例中,我们选用JSTLView作为我们的表现层实现。
对应的配置文件如下。
Config.xml:
<beans>
<beanid="viewResolver"
class="org.springframework.web.servlet.view.InternalRes
ourceViewResolver">
<propertyname="viewClass">
<value>
org.springframework.web.servlet.view.JstlView
</value>
</property>
<propertyname="prefix">
<value>/WEB-INF/view/</value>
</property>
<propertyname="suffix">
<value>.jsp</value>
</property>
</bean>
<beanid="RegisterValidator"⑴
class="net.xiaxin.validator.RegisterValidator"/>
<beanid="RegisterAction"
class="net.xiaxin.action.RegisterAction">
<propertyname="commandClass">
<value>net.xiaxin.reqbean.RegisterInfo</value>
</property>
<propertyname="validator">⑵
<reflocal="RegisterValidator"/>
</property>
<propertyname="formView">⑶
<value>register</value>
</property>
<propertyname="successView">⑷
<value>RegisterSuccess</value>
</property>
</bean>
<!
--RequestMapping-->
<beanid="urlMapping"
class="org.springframework.web.servlet.handler.SimpleUr
lHandlerMapping">
<propertyname="mappings">
<props>
<propkey="/register.do">RegisterAction</prop>
</props>
</property>
</bean>
</beans>
这个配置文件与篇首MVC介绍中所用实例大同小异。
不同之处在于我们在这里引入了数
据验证配置节点:
⑴配置了一个数据验证Bean:
RegisterValidator
net.xiaxin.validator.RegisterValidator
⑵为逻辑处理单元RegisterAction定义输入数据校验Bean
这里通过一个Bean引用,将RegisterValidator配置为本Action的数据校验
类。
⑶指定本处理单元的显示界面。
formView是RegisterAction的父类SimpleFormController中定义的属
性,指定了本处理单元的显示界面。
这里即用户访问register.do时将显示的注册界面。
要注意的是,完成此界面后,我们必须通过“…/register.do”访问注册界面,
而不是“…/register.jsp”,因为我们必须首先借助Spring完成一系列初始
化工作(如创建对应的状态对象并与之关联)之后,register.jsp才能顺利执
行,否则我们会得到一个应用服务器内部错误。
⑷指定成功返回界面。
successView同样是RegisterAction的父类SimpleFormController中定
义的属性,它指向成功返回界面。
b)数据验证类
在Spring中,所有的数据验证类都必须实现接口:
org.springframework.validation.Validator
Validator接口定义了两个方法:
booleansupports(Classclazz);
用于检查当前输入的数据类型是否符合本类的检验范围。
Spring调用
Validator实现类时,首先会通过这个方法检查数据类型是_____否与此Validator
相匹配。
voidvalidate(Objectobj,Errorserrors);
数据校验方法。
Validator实现类通过实现这个方法,完成具体的数据校验逻辑。
RegisterValidator.java:
publicclassRegisterValidatorimplementsValidator{
publicbooleansupports(Classclazz){
returnRegisterInfo.class.isAssignableFrom(clazz);⑴
}
publicvoidvalidate(Objectobj,Errorserrors){
RegisterInforegInfo=(RegisterInfo)obj;⑵
//检查注册用户名是否合法
if(regInfo.getUsername().length()<4){
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,
"两次输入的密码不一致!
");
}
}
}
⑴RegisterInfo.class.isAssignableFrom方法用于判定参数类别,当传入
Class对象与当前类类别相同,或是当前类的父类(或当前类实现的接口)时返回真。
这
里我们将其用于对校验对象的数据类型进行判定(这里的判定条件为:
校验对象必须是
RegisterInfo类的实例)。
⑵RegisterInforegInfo=(RegisterInfo)obj;
将输入的数据对象转换为我们预定的数据类型。
⑶通过rejectValue方法将错误信息加入Error列表,此错误信息将被页面捕获并
显示在错误提示界面上。
rejectVlaue方法有4个参数:
1.ErrorCode
显示错误时,将根据错误代码识别错误信息类型。
2.MessageKey
上面关于ApplicationContext的国际化支持时,我们曾经谈及
MessageSource的使用,这里我们可以通过引入MessageSource实现提示信息
的参数化,此时,本参数将用作.properties文件中的消息索引。
3.ErrorArgument
如果提示信息中需要包含动态信息,则可通过此参数传递需要的动态信息对象。
具
体参见ApplicationContext中关于国际化实现的描述。
4.DefaultMessage
如果在当前MessageSource中没有发现MessageKey对应的信息数据,则以此
默认值返回。
这里我们暂时尚未考虑国际化支持,所有的信息都将通过DefaultMessage返
回。
关于国际化支持请参见稍后章节。
另外rejectValue还有另外几个简化版本,可根据情况选用。
其中RegisterInfo类定义如下:
publicclassRegisterInfo{
privateStringusername;
privateStringpassword1;
privateStringpassword2;
publicStringgetPassword1(){
returnpassword1;
}
publicvoidsetPassword1(Stringpassword1){
this.password1=password1;
}
publicStringgetPassword2(){
returnpassword2;
}
publicvoidsetPassword2(Stringpassword2){
this.password2=password2;
}
publicStringgetUsername(){
returnusername;
}
publicvoidsetUsername(Stringusername){
this.username=username;
}
}
c)注册界面
register.jsp提供了注册操作界面。
它同时提供了最初的注册界面,当输入参数非
法时,同时也会显示错误信息,提示用户检查输入。
register.jsp:
<!
--页面中使用了JSTLCoretaglib和Springli