Error Message In Struts 1.3
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow the company Business Learn more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up ActionError message not getting displayed in UI up vote 2 down vote favorite 1 The application is made on Struts 1.2. I have a login form, all the validations related to the login are getting displayed properly. For the validations, I am using the Validator framework. When the validation succeeds but the user authentication fails, then I am not able to display the login failure message properly. I am setting the ActionError message in the Action like this: Login loginDetails = validateUser(loginForm); if(loginDetails == null){ errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("error.invalidCredentials")); return mapping.findForward("failure"); } Below is the entry made in the ApplicationResources.properties file: error.invalidCredentials=User Name and Password does not match. When the loginDetails is null then the errors are getting added properly (I verified this by debugging the code). But the messages are not getting displayed in the UI. This is what I added in the UI:
error handling Struts Validation and error handling Struts provides capabilities to validate form properties in two major modes, one is Java and the other is XML related. Another feature of Struts is error handling. In this tutorial we want to explain both of them and show you a small example application using these features. General Author: Sascha Wolski Sebastian Hennebrueder Date Updated: December 2007 First edition: February 2005 Software: Struts Framework 1.3 PDF download: http://www.laliluna.de/download/struts-validation-error-handling-en.pdf Source download: http://www.laliluna.de/download/struts-validation-error-handling.zip Requirements We require the basics of Struts to understand this tutorial. If you are http://stackoverflow.com/questions/14480631/actionerror-message-not-getting-displayed-in-ui new to Struts, read the ‘first steps in struts' tutorial, you can find at http://www.laliluna.de/articles/posts/first-steps-using-struts-tutorial.html. Create a struts project Let's start, create a new struts project and a package named de.laliluna.tutorial.validation. Action form class (java related validation) Create a new class ExampleForm in the package de.laliluna.tutorial.validation.form, which extends the class ActionForm. Add two properties, name of type String and age of type String. Add a http://www.laliluna.com/articles/posts/struts-form-validation-error-handling.html getter and setter method for each property. Initialize the properties in the reset() method. The following source code shows the content of the class ExampleForm. public class ExampleForm extends ActionForm { private String name; private Integer age; public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } } The Validate Method The action form class provides a method validate() which is called before the action class is executed. So you can validate your properties within this method. The return-value of the method is a collection (ActionErrors) of error messages (ActionMessage).
You can validate your properties and add error messages to this collection for each wrong validation. In the JSP file you can display these messages to the user. The error messages are message keys of the message resource bundle. (You will find more infos about message resource bundle at http://www.laliluna.de/articles/posts/struts-message-resources-tutorial.html.) Implement the validate() method of the action form class. Validate each property and add an error message for each wrong validation. These are the criteria for the vStruts 2.x Wicket Spring Hibernate JSF Action Errors in Struts with example code. How to display errors in JSP http://javaonlineguide.net/2014/09/action-errors-in-struts-with-example-code-how-to-display-errors-in-jsp-page.html page? VK September 7, 2014 struts The below tutorial explains that how to use ActionErrors class in struts web applications and also covers how the errors generated by the validate() method of ActionForm, are displayed in the JSP page. ActionErrors is a class that extends org.apache.struts.action.ActionMessages class. It adds error(s) generated by the validate() error message method or any method which returns ActionError of an ActionForm. Errors (ActionError objects) are added to the ActionErrors instance using add method which has the following form. public void add(java.lang.String property, ActionError error) - this method is depreciated. You can use add(String, ActionMessage) instead. property - Property name (or ActionErrors.GLOBAL_ERROR) - to decide the error message in validation errors are either global to the entire ActionForm bean they are associated with, or they are specific to a particular bean property . error - The error message to be added Each error is represented by an ActionError object, which contains a message key and up to four arguments as replacement values for substitution in the resulting message . ActionError has the following form. public ActionError(java.lang.String key, java.lang.Object value0, java.lang.Object value1, java.lang.Object value2, java.lang.Object value3) where key is Message key for this error message and value0 - 1st replacement value, value1 - 2nd replacement value , value2 - 3rd replacement value , value3 - 4th replacement value, where value0, value1 , value2 , value3 are optional. eg: mandatoryFields="Login Id and Password" new ActionError("fields.required", mandatoryFields) where fields.required is the message key and mandatoryFields is the message text (1st replacement value) used for substitution with {0} in the resulting error message, before it is displayed in the JSP. Message key, corr