Customizing Error Messages In Struts2
Contents |
App EngineApache AntApache MavenjQueryJava MongoDBQuartz SchedulerLog4jContact Us Struts 2 ActionError & ActionMessage ExampleBy mkyong | June 10, 2010 | Viewed : 184,278 times +596 pv/w Download It - Struts2-ActionError-ActionMessage-Example.zipA tutorial to show how to display error message in jsp using struts2 the use of the Struts 2's ActionError and ActionMessage class.1. ActionError
Struts Action Messages Example
- is used to send error feedback message to user - display via class="errors"> a simple login form, display the error message (actionerror) if the username is not equal to "mkyong", otherwise redirect to another page and display the a welcome message (actionmessage). In addition, all the label and error messages are retrieve from the resource bundle (properties file).1. Folder StructureSee this project structure 2. Properties fileTwo properties files to struts2 addfielderror store the messages.LoginAction.properties #Welcome messages welcome.hello = Hello #error message username.required = Username is required password.required = Password is required global.properties #Global messages global.username = Username global.password = Password global.submit = Submit global.reset = Reset 3. ActionA classic action class, do a simple checking to make sure the username is equal to "mkyong", and set the error message with addActionError() or successful message with addActionMessage(). package com.mkyong.user.action; import com.opensymphony.xwork2.ActionSupport; public class LoginAction extends ActionSupport{ private String username; private String password; public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } //business logic public String execute() { return "SUCCESS"; } //simple validation public void validate(){ if("mkyong".equals(getUsername())){ addActionMessage("You are valid user!"); }else{ addActionError("I don't know you, dont try to hack me!"); } } } 4. JSP ViewTwo simple JSP pages with css style to customize the error message.login.jsp <%@ page contentType="text/html; charset=UTF-8" %> <%@ taglib prefix="s" here for a quick overview of the site Help Center Detailed answers to any questions you struts2 fielderror might have Meta Discuss the workings and policies of this site struts html:messages About Us Learn more about Stack Overflow the company Business Learn more about hiring developers or actionerror in struts 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 https://www.mkyong.com/struts2/struts-2-actionerror-actionmessage-example/ 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up Displaying Validation Error Messages in Struts2 up vote 1 down vote favorite 1 I am new to Struts 2, have worked in Struts 1 earlier. How can we bind a error message with a UI component (e.g. http://stackoverflow.com/questions/16672435/displaying-validation-error-messages-in-struts2 a Text box) ? I don't want the error message to be a global one. For achieving the same in Struts 1: In the form validate method, I used this: ActionErrors errors = new ActionErrors(); if(userName != null && userName.length() <= 0) errors.add("userName",new ActionError("error.userName.required")); and in the UI, for displaying the message: & JSP Hibernate Ajax SOAP Web Services Core Java JDBC Tutorial XLS -XLSX -CSV I/O FTP in Java Soap-UI SOAPUI Tutorials Soap UI Updates Home » Struts 1 » ActionError & ActionMessage Example in Struts http://www.simplecodestuffs.com/struts-2-actionerror-actionmessage-example/ 2 ActionError & ActionMessage Example in Struts 2 Posted by Mohaideen Jamil on Apr 18, 2013 in Struts 1, Struts 2 Tutorial, Struts-2 | 5 comments In this tutorial we will learn about ActionError https://struts.apache.org/docs/form-validation.html & ActionMessage class and its usage. a) ActionError class is used to send error feedback message to user and it get rendered in jsp by using Actions>Processing Forms>Form Validation Apache Struts 2 Documentation Form Validation Edit Page Browse Space Add Page Add News This tutorial assumes you've completed the Processing Forms tutorial and have a working form_processing project. The example code for this tutorial, form_validation, is available for checkout from the Struts 2 GitHub repository: https://github.com/apache/struts-examples.IntroductionIn this tutorial we'll explore using Struts 2 to validate the user's input on a form. There are two ways you can use Struts 2 to do form validation. This tutorial will cover the more basic method, where the validation is included in the Struts 2 Action class.The Struts 2 user mailing list is an excellent place to get help. If you are having a problem getting the tutorial example applications to work search the Struts 2 mailing list. If you don't find an answer to your problem, post a question on the mailing list.Add validate MethodTo enable the Struts 2 Action class to validate a user's input on a Struts 2 form, you must define a validate method in your Action class. Using the example from Processing Forms tutorial, let's say that we have these business rules:1. User must provide a first name 2. User must provide an email address 3. User younger then 18 cannot registerIf you recall from the Processing Forms tutorial the user's input into the form fields is placed by Struts 2 into the Java model class personBean. So a user's input into the firstName field would end up as the value for personBean's firstName instance field (via the personBean.setFirstName method).In the validate method we can refer to get the values of personBean's instance fields by using the appropriate get methods. Once we have the values we can employ logic to enforce our business rules.Add the following validate method to Register.java (the Action class).validate method public void validate(){ if ( personBean.getFirstName().length() == 0 ){ addFieldError( "personBean.firstName", "First name is required." ); } if ( personBean.getEmail()Struts Display Error Messages In Jsp
Action Message Struts 1
ActionError & ActionMessage Example