Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

<html:errors> struts tutorial or example

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.

like image 267
Tere Avatar asked Jun 02 '09 01:06

Tere


People also ask

What are error tags in HTML?

The html:errors tag can be used in two different ways. If used without a property attribute, it will display all the errors for the form. When used with the attribute, it displays only the errors for that specific property.

What is Struts in Java with example?

Struts is an open source framework that extends the Java Servlet API and employs a Model, View, Controller (MVC) architecture. It enables you to create maintainable, extensible, and flexible web applications based on standard technologies, such as JSP pages, JavaBeans, resource bundles, and XML.

What is error tag?

The <errors> tag renders field errors in an HTML 'span' tag. Displays errors for either an object or a particular field. This tag supports three main usage patterns: Field only - set ' path ' to the field name (or path) Object errors only - omit ' path '


1 Answers

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>
like image 181
Mark Lutton Avatar answered Oct 01 '22 02:10

Mark Lutton