<html:errors> struts tutorial or example
Asked Answered
D

3

7

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).

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.

Duvalier answered 2/6, 2009 at 1:34 Comment(0)
M
10

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

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

You have an Action class, say 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:

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.

Mayda answered 4/6, 2009 at 16:40 Comment(2)
Thank you! After hours of work I finally could validate my form. In the end, I prefer to validate in the bean instead in the action and used the validation.xml (for the minlenght, required, etc) and the database validation (the user exists, the password is correct...). The only thing I had to change was the use of ".add(String, ActionError)" of ActionErrors because is deprecated (Struts 1.2.x) and used ".add(String, ActionMessage)" which is essencially the same. Again, thank you very much for your answers!Duvalier
@Mark Lutton: if the form was created beginning from another Action (for example the labels/values for some radio buttons were retrieved from a DB and put into the request), how can the radio buttons be displayed again together with the error (in the case that a radio button is required to be selected)? I don't succeed in displaying them, when I submit the form (only the error is printed).Thousandth
M
16

Here's one: //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.

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

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.

In your Action class, do this:

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>
Mayda answered 2/6, 2009 at 3:22 Comment(3)
This is certainly a colorful answer, isn't it? I haven't mastered how the formatting works here.Mayda
Thank you very much for your answer! I'm still trying to validate (following your sugestiongs, they're really good! I needed something like this, all explained in the same place) I'll let you know when it works! ;)Duvalier
The best explanation of this is in the book "The Struts Framework: Practical Guide for Java Programmers" by Sue Spielman, ISBN 1-55860-862-1. Chapter 5. It goes step-by-step.Mayda
M
10

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

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

You have an Action class, say 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:

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.

Mayda answered 4/6, 2009 at 16:40 Comment(2)
Thank you! After hours of work I finally could validate my form. In the end, I prefer to validate in the bean instead in the action and used the validation.xml (for the minlenght, required, etc) and the database validation (the user exists, the password is correct...). The only thing I had to change was the use of ".add(String, ActionError)" of ActionErrors because is deprecated (Struts 1.2.x) and used ".add(String, ActionMessage)" which is essencially the same. Again, thank you very much for your answers!Duvalier
@Mark Lutton: if the form was created beginning from another Action (for example the labels/values for some radio buttons were retrieved from a DB and put into the request), how can the radio buttons be displayed again together with the error (in the case that a radio button is required to be selected)? I don't succeed in displaying them, when I submit the form (only the error is printed).Thousandth
M
0

In struts action:

ActionErrors errors = new ActionErrors();
errors.add("", new ActionMessage("login.msg.err"));
saveErrors(request, errors);

The "hello.msg.err" is define in MessageBundle.properties

login.msg.err=Wrong user name or password

In you JSP, error will be shown by h:errors tag:

<h:errors/>

This video shows you step by step & explain you all those things: https://youtu.be/YcCsJtqI72A

Maggot answered 12/5, 2019 at 15:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.