struts教程或示例

时间:2022-01-03 15:28:19

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

我想知道是否有人知道Struts中的错误管理教程。我正在寻找专门的教程(或示例)

<html:errors>

tag, which I think would solve my problem.

标签,我认为这将解决我的问题。

2 个解决方案

#1


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

这是一个快速摘要。你有一个ActionForm类,比如MyForm:

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

You have an Action class, say MyAction:

你有一个Action类,比如MyAction:

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

"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:

动作中的“name”指的是form-b​​ean中的“name”。因为你有validate =“true”你的ActionForm类MyForm必须定义自动调用的validate()方法:

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;
}

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.

如果它返回一个空的ActionErrors对象,Struts继续调用你的MyAction.execute()。否则,Struts会显示/insert.jsp(因为这是你给出的input = parm)并展开html.errors标记以显示ActionErrors中的错误。

#2


Here's one: //struts.apache.org/1.3.5/struts-taglib/apidocs/org/apache/struts/taglib/html/package-summary.html#package_description

这是一个://struts.apache.org/1.3.5/struts-taglib/apidocs/org/apache/struts/taglib/html/package-summary.html#package_description

Here I'm assuming Struts 1. I don't know if it has changed for Struts 2.

在这里我假设Struts 1.我不知道它是否已经改变了Struts 2。

You can put an errors.header and errors.footer into your message resources file:

您可以将errors.header和errors.footer放入消息资源文件中:

errors.header=<h3><font color="red">Errors:</font></h3><ul>
errors.footer=</ul>

The header and footer are displayed only if the ActionErrors object has any errors in it.

仅当ActionErrors对象中包含任何错误时,才会显示页眉和页脚。

In your Action class, do this:

在Action类中,执行以下操作:

ActionErrors errors = new ActionErrors();
if (badInput) {
  errors.add(ActionErrors.GLOBAL_ERROR,
    new ActionError("error.bad.input", badString);    // key in messages resource file
                                    // badString will replace {0} in message
}

Then before returning:

然后返回:

saveErrors(request, errors);

In your messages resource file:

在您的消息资源文件中:

error.bad.input=<li>Bad input:  '{0}' is invalid.</li>

Now when the <html:errors/> tag is processed, it will turn into:

现在,当处理

标记时,它将变成:
<h3><font color="red">Errors:</font></h3><ul>
<li>Bad input: 'xxyyzzz' is invalid.<li>
</ul>

#1


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

这是一个快速摘要。你有一个ActionForm类,比如MyForm:

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

You have an Action class, say MyAction:

你有一个Action类,比如MyAction:

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

"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:

动作中的“name”指的是form-b​​ean中的“name”。因为你有validate =“true”你的ActionForm类MyForm必须定义自动调用的validate()方法:

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;
}

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.

如果它返回一个空的ActionErrors对象,Struts继续调用你的MyAction.execute()。否则,Struts会显示/insert.jsp(因为这是你给出的input = parm)并展开html.errors标记以显示ActionErrors中的错误。

#2


Here's one: //struts.apache.org/1.3.5/struts-taglib/apidocs/org/apache/struts/taglib/html/package-summary.html#package_description

这是一个://struts.apache.org/1.3.5/struts-taglib/apidocs/org/apache/struts/taglib/html/package-summary.html#package_description

Here I'm assuming Struts 1. I don't know if it has changed for Struts 2.

在这里我假设Struts 1.我不知道它是否已经改变了Struts 2。

You can put an errors.header and errors.footer into your message resources file:

您可以将errors.header和errors.footer放入消息资源文件中:

errors.header=<h3><font color="red">Errors:</font></h3><ul>
errors.footer=</ul>

The header and footer are displayed only if the ActionErrors object has any errors in it.

仅当ActionErrors对象中包含任何错误时,才会显示页眉和页脚。

In your Action class, do this:

在Action类中,执行以下操作:

ActionErrors errors = new ActionErrors();
if (badInput) {
  errors.add(ActionErrors.GLOBAL_ERROR,
    new ActionError("error.bad.input", badString);    // key in messages resource file
                                    // badString will replace {0} in message
}

Then before returning:

然后返回:

saveErrors(request, errors);

In your messages resource file:

在您的消息资源文件中:

error.bad.input=<li>Bad input:  '{0}' is invalid.</li>

Now when the <html:errors/> tag is processed, it will turn into:

现在,当处理

标记时,它将变成:
<h3><font color="red">Errors:</font></h3><ul>
<li>Bad input: 'xxyyzzz' is invalid.<li>
</ul>