首页 > 其他 > 详细

struts validator例子

时间:2014-01-20 23:13:40      阅读:416      评论:0      收藏:0      [点我收藏+]
本文假设你已经:
1、工程中引入了struts的主要包
2、希望使用“注解”来配置页面跳转、validator,而不是xml文件

本文的例子:productId是Action中的一个属性,需要检验:它是非空的。如果校验失败,跳转至一个页面


开始了哈~
@Results({ @Result(name = "input", location = "/para-error.jsp") })
public class ProductAction extends ActionSupport

一、Action中的配置
1、配置validator校验器
action中的productId需要有set、get方法,同时,在set方法上面加上注解
@RequiredStringValidator(key = "productId.requried")
     public void setProductId(String productId)
     {
          this.productId = productId;
     }
RequiredStringValidator代表是一个“非空”校验,struts还提供了其他很多种类的校验器。key表示校验出错时的提示语,会映射到productId.requried对应的内容,productId.requried怎么配置,下面第三点会讲到

2、配置校验失败时的页面跳转
在Action头加入跳转逻辑


校验器校验失败时,默认都会跳到input对应的页面(对result、name这些没概念,就google一下),因此,当result name为input时,我们把它定向到prara-error.jsp这个页面,这是我们自己生成的一个页面

二、校验出错提示的JSP页面
在WebRoot目录下,创建一个jsp页面,重命名为prara-error.jsp
1、在jsp头部引入struts tags
<%@ taglib prefix="s" uri="/struts-tags"%>
2、在html body中加入s:fielderror
html body如下:
  <body>
    <s:fielderror/> <br>
  </body>

struts将自动将s:fielderror填充为校验出错的对应提示语

三、校验出错的提示语
1、在struts.xml中配置,将国际化资源文件指向mess.properties
<constant name="struts.custom.i18n.resources" value="mess"></constant>

2、在src目录中创建mess.properties,并指定productId.requried对应的值
例如我的mess.properties如下:
productId.requried=productId不能为空

最后,我们来看看,当检验失败时,页面的截图
bubuko.com,布布扣
bubuko.com,布布扣

struts validator例子

原文:http://blog.csdn.net/lizeyang/article/details/18420687

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!