< html:errors> struts教程或示例 [英] <html:errors> struts tutorial or example

查看:76
本文介绍了< html:errors> struts教程或示例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在Struts中创建一个登录页面.想法是验证用户是否存在等,然后如果有错误,请以红色错误返回登录页面(典型的登录或任何表单页面验证).

I'm trying to make a login page in Struts. The idea is to validate if the user exists, etc, and then if there is an error, return to the login page with the errors in red (the typical login or any form page validation).

我想知道是否有人知道Struts中的错误管理教程.我正在专门寻找

I would like to know if someone knows an errors management tutorial in Struts. I'm looking specially for a tutorial (or example) of the

<html:errors>

标签,我认为它可以解决我的问题.

tag, which I think would solve my problem.

推荐答案

这里是一个简短的摘要.您有一个ActionForm类,例如MyForm:

Here's a quick summary. You have an ActionForm class, say MyForm:

<form-bean name="myForm" type="myapp.forms.MyForm"/>

您有一个Action类,例如MyAction:

<action path="/insert" type="myapp.actions.MyAction" name="myForm"
   input="/insert.jsp" validate="true" />
  <forward name="success" path="/insertDone.jsp"/>
</action>

动作中的名称"是指form-b​​ean中的名称".因为您拥有validate="true",所以您的ActionFormMyForm必须定义validate()方法,该方法将自动被调用:

"name" in the action refers to "name" in the form-bean. Because you have validate="true" your ActionForm class MyForm must define validate() method which will automatically be called:

public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) {
  ActionErrors errors = new ActionErrors();
  if ((username==null) || (username.length() < 1)) 
      errors.add("username", new ActionError("error.username.required"));
  return errors;
}

如果返回空的ActionErrors对象,Struts继续调用MyAction.execute().否则,Struts将显示/insert.jsp(因为这是您提供的input = parm),并展开html.errors标记以显示ActionErrors中的错误.

If it returns an empty ActionErrors object, Struts goes on to call your MyAction.execute(). Otherwise, Struts displays /insert.jsp (because that's the input= parm you gave) and expands the html.errors tag to display your errors from ActionErrors.

这篇关于&lt; html:errors&gt; struts教程或示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆