Struts validate验证的具体实现

开发 后端
本文将对Struts validate验证进行讲解,Struts validate验证通过XML文件进行配置,希望本文对大家有所帮助。
Struts validate验证

1. ActionErrors和ActionError都是ActionMessages的子类,ActionError存放在ActionErrors 中,ActionError对象中的参数为配置文件中配置的相应的值,若配置文件中没有配置或配置文件不适合都不能用< htnl:errors property=""/>输出。

2.错误信息添加用 

ActionErrors errors=new ActionErrors(); 

errors.add("error_key",new ActionError("配置文件中的相应的值")) 

注解:void add(java.lang.String property,ActionError error)

在jsp页面中显示错误:

3.在Action中使用ActionMessages 他有两个add方法:

他有两个add方法:

void add(ActionMessages  message)
  Adds the meesage from the given ActionMessage object  to tjis set of meesage
void add(java.lang.String property,ActionMessage message)
             Add message to the set of message for the specifiled property.

错误信息添加使用errors.add("error_key",new ActionMessage("errors.loginerror"))
另外要调用this.saveMessages(request,errors)方法;
这是org.apache.structs.action.Action中 的方法,要用到的大概有两个:

1.saveErrors(少用)

protected void saveErrors(java.servlet,http.HttoServletRequest request,ActionErrors errors)
 Save the specifiled error message keys into the appropriate request attribute for use by the<html:errors>
tag,if any message are required.Otherwise,ensure that the request attribute is not created.

2.saveMessage(常用)

protect void saveMessage(javax.Servlet.http.HttpServletRequest request,ActionMessages  messages)
 Save the specified message keys into the appropriate request attribute for use by the<html:message>tag{
if messages="true"    is set},if any message are required,Otherwise,ensure that the request attribute  is not
created.

ActionErrors actionMessages ,ActionMessage ActionMessage是继承关系,父类在前,子类在后

if (!loadForm.getUsername().equals("123")) {

   ActionMessages error = new ActionMessages();
   error.add("username", new ActionMessage("error.username"));
   saveErrors(request, error);

   return mapping.findForward("S1");
  }

  if (!loadForm.getPassword().equals("abc")) {

   ActionMessages error = new ActionMessages();
   error.add("password", new ActionMessage("error.password"));
   saveErrors(request, error);

   return mapping.findForward("S1");
  }

  return mapping.findForward("S1");

error.username=<font color="red">\u5e10\u53f7\u9519</font>
error.password=<font color="red">\u5bc6\u7801\u9519</font>
error.up=<font color="red">\u5e10\u53f7\u6216\u5bc6\u7801\u4e0d\u80fd\u7a7a</font>

FORM里面

public ActionErrors validate(ActionMapping mapping,
   HttpServletRequest request) {

  ActionErrors errors = new ActionErrors();
if (null == username.trim() || null == password.trim()||username.length()<3

||password.length()<2) {

   errors.add("username", new ActionMessage("error.up"));
   System.out.println(errors);

   return errors;
  }

  return null;
 }

native2ascii 来处理中文汉字

【编辑推荐】

  1. 在Eclipse中开发struts应用程序
  2. 手把手教你在Eclipse中配置开发Struts
  3. Eclipse下开发struts完整解决乱码问题
  4. Struts相关背景介绍
  5. 使用Easy Struts for Eclipse开发Struts
责任编辑:张燕妮 来源: javaeye.com
相关推荐

2009-06-25 15:33:12

Struts2教程使用validate验证数据

2009-02-04 12:00:08

2009-11-25 11:33:26

PHP验证表单

2009-11-23 16:59:23

PHP图形验证码

2009-06-04 10:33:10

struts vali框架

2011-03-30 09:03:57

struts2

2009-06-25 15:37:12

Struts2教程Validation框

2009-06-05 09:58:20

struts2验证用户注册

2009-06-04 10:54:59

实战strutsMenu

2011-04-28 09:52:04

Struts2

2009-06-05 09:52:25

struts分页Hibernate

2009-02-04 13:13:03

2010-07-22 13:23:46

telnet SMTP

2010-06-08 14:28:16

Opensuse中文

2009-06-03 15:38:37

Struts框架RBAC

2009-07-20 16:51:59

Struts2.0+i

2010-07-13 10:38:54

2009-12-02 17:22:22

PHP抓取图片

2009-08-26 13:41:58

C#打印源码

2010-06-08 15:02:39

Opensuse u盘
点赞
收藏

51CTO技术栈公众号