Struts2(四) Validate---ActionSupport
来源:互联网 发布:彩票选号优化方法 编辑:程序博客网 时间:2024/05/17 06:29
public class PersonAction extends ActionSupport{private String username;private String mobile;public String getUsername() {return username;}public void setUsername(String username) {this.username = username;}public String getMobile() {return mobile;}public void setMobile(String mobile) {this.mobile = mobile;}public String update(){ActionContext.getContext().put("message", "更新成功");return "message";}public String save(){ActionContext.getContext().put("message", "保存成功");return "message";}@Overridepublic void validate() {//会对action中的所有方法校验if(this.username==null || "".equals(this.username.trim())){this.addFieldError("username", "用户名不能为空");}if(this.mobile==null || "".equals(this.mobile.trim())){this.addFieldError("mobile", "手机号不能为空");}else{if(!Pattern.compile("^1[358]\\d{9}$").matcher(this.mobile).matches()){this.addFieldError("mobile", "手机号格式不正确");}}}}
2.8 增加数据校验
在上面应用中,即使浏览者输入任何用户名、密码,系统也会处理用户请求。在我们整个HelloWorld应用中,这种空用户名、空密码的情况不会引起太大的问题。但如果数据需要保存到数据库,或者需要根据用户输入的用户名、密码查询数据,这些空输入可能引起异常。
为了避免用户的输入引起底层异常,通常我们会在进行业务逻辑操作之前,先执行基本的数据校验。
2.8.1 继承ActionSupport
ActionSupport类是一个工具类,它已经实现了Action接口。除此之外,它还实现了Validateable接口,提供了数据校验功能。通过继承该ActionSupport类,可以简化Struts 2的Action开发。
在Validatable接口中定义了一个validate()方法,重写该方法,如果校验表单输入域出现错误,则将错误添加到ActionSupport类的fieldErrors域中,然后通过OGNL表达式负责输出。
为了让Struts 2增加输入数据校验的功能,改写程序中的LoginAction,增加重写validate方法。修改后的LoginAction类代码如下://Struts 2的Action类就是一个普通的Java类
public class LoginAction
{
//下面是Action内用于封装用户请求参数的两个属性
private String username;
private String password;
//username属性对应的getter方法
public String getUsername()
{
return username;
}
//username属性对应的setter方法
public void setUsername(String username)
{
this.username = username;
}
//password属性对应的getter方法
public String getPassword()
{
return password;
}
//password属性对应的setter方法
public void setPassword(String password)
{
this.password = password;
}
//处理用户请求的execute方法
public String execute() throws Exception
{
//当用户请求参数的username等于scott,密码请求参数为tiger时,返回success
字符串
//否则返回error的字符串
if (getUsername().equals("scott")
&& getPassword().equals("tiger") )
{
return "success";
}
else
{
return "error";
}
}
//完成输入校验需要重写的validate方法
public void validate()
{
//如果用户名为空,或者用户名为空字符串
if (getUsername() == null || getUsername().trim().equals(""))
{
//添加表单校验错误
addFieldError("username", "user.required");
}
//当密码为空,或者密码为空字符串时,添加表单校验错误
if (getPassword() == null || getPassword().trim().equals(""))
{
addFieldError("password", "pass.required");
}
}
}上面的Action类重写了validate方法,该方法会在执行系统的execute方法之前执行,如果执行该方法之后,Action类的fieldErrors中已经包含了数据校验错误,请求将被转发到input逻辑视图处。
为了在校验失败后,系统能将视图转入input处,必须在配置该Action时配置input属性。下面是修改后login Action的配置片段:<!-- 定义login的Action -->
<action name="Login" class="lee.LoginAction">
<!-- 定义input的逻辑视图名,对应login.jsp页面 -->
<result name="input">/login.jsp</result>
<!-- 定义error的逻辑视图名,对应error.jsp页面 -->
<result name=" success ">/error.jsp</result>
<!-- 定义welcome的逻辑视图名,对应welcome.jsp页面 -->
<result name="success">/welcome.jsp</result>
</action>对比上面的Action配置与前面的Action配置,我们发现该Action配置片段中增加了input逻辑视图的配置,该逻辑视图映射到login.jsp页面。
图2.13 输入校验的界面
前面已经提到:当用户提交请求时,请求得到execute方法处理之前,先会被validate方法处理,如果该方法处理结束后,Action的fieldErrors里的校验错误不为空,请求将被转发给input逻辑视图。如果我们不输入用户名、密码而直接提交表单,将看到如图2.13所示的界面。看到这里也许读者觉得非常神奇:我们仅仅在Action添加了数据校验错误,并未在输入页面输出这些校验错误信息,但图2.13所示的页面,却可以看到页面已经输出了这些校验信息——这是因为Struts 2的标签,上面的JSP页面中表单使用的并不是HTML表单,而是用了<s:form .../>标签,Struts 2的<s:form ... />标签已经具备了输出校验错误的能力。
提示 Struts 2的<s:form .../>默认已经提供了输出校验错误的能力。
但上面的程序还存在一个问题:校验信息的国际化。查看上面的Action类代码发现:重写validate方法时,如果发生校验失败的问题,校验错误的提示信息是以硬编码方式写死了——这就失去了国际化的能力。
实际上,ActionSupport类已经提供了国际化信息的能力,它提供了一个getText(String key)方法,该方法用于从资源文件中获取国际化信息。为了让校验信息支持国际化,再次改写Action里的validate方法,改写后的validate方法代码如下://执行数据校验的validate方法
public void validate()
{
//如果用户名为空,或者为空字符串
if (getUsername() == null || getUsername().trim().equals(""))
{
//添加校验错误提示,使用getText方法来使提示信息国际化
addFieldError("username", getText("user.required"));
}
if (getPassword() == null || getPassword().trim().equals(""))
{
addFieldError("password", getText("pass.required"));
}
}在上面的validate方法中,添加校验错误提示时,并不是直接给出了错误提示的字符串,而是调用了getText方法来获取错误提示。因为在Action中,使用getText方法来获取了两个国际化提示:user.required和pass.required,因此应该在国际化资源文件中添加这两条提示信息。
图2.14 国际化数据校验的错误提示
提示 ActionSupport增加了让提示信息国际化的能力,ActionSupport提供的getText方法可以根据资源文件加载获得国际化提示信息。
此时,如果没有任何输出,直接提交登录表单,将看到如图2.14所示的界面。
- Struts2(四) Validate---ActionSupport
- struts2 ActionSupport中的validate方法
- 用Struts2中的ActionSupport中的validate方法进行表单验证
- 用Struts2中的ActionSupport中的validate方法进行表单验证
- Struts2.ActionSupport
- Struts2使用ActionSupport类中的validate方法验证用户登陆信息
- struts2中的ActionSupport
- struts2中ActionSupport校验
- struts2查看ActionSupport源码
- struts2 ActionSupport类
- struts2中的actionSupport
- Struts2继承ActionSupport接口
- struts2的ActionSupport
- struts2 中 Actionsupport 的作用
- struts2 中 Actionsupport 的作用
- Struts2 继承ActionSupport的用处
- struts2 中 Actionsupport 的作用
- struts2 中 Actionsupport 的作用
- 2012年36个最佳前端开发指南
- Orange数据格式
- LCD设备驱动程序2
- CentOS常用命令
- Cygwin Application initialization failed: no display name and no $DISPLAY environment
- Struts2(四) Validate---ActionSupport
- spi_priv.h
- ext2文件系统
- PPPOE协议
- Android ListView滚动Item背景为黑色解决办法...
- 检测到有潜在危险的 Request.Form 值问题
- RFC里,系统会自动将小写转化为大写,提取不到数据的解决办法
- s3c6410存储器映射
- 2012—伴随我成长的一年