< html:errors /> html tag
Previous | Home | Next |
This Tag displays a set of error messages prepared by a business logic component and stored in the ActionErrors object, a String, or a String array . you must have defined an application scope MessageResources bean under the default attribute name.
The message keys specified in the following attributes :-errors.header :- This message text will end with <ul> to start the error messages list. Text will be rendered before the error messages list.
errors.footer :- This message text will begin with </ul> to end the error messages list .Text that will be rendered after the error messages list
errors.prefix :- Text that will be rendered before each individual error in the list
errors.suffix :- First Each individual error in the list after that text will be rendered.The struts Errors tag have following attributes:-
1.bundle :-The servlet context attribute key for the MessageResources instance to use. if this is not specified than defaults to the application resources configured for our action servlet.2.name :-Error messages can be stored in this bean. If not present than it specified by the Globals.ERROR_KEY constant string will be used.
The Following are many other attributes which we can use :footer header locale prefix suffix property
Directory Structure of ErrorsTagExample in Struts 1.3 Using MyEclipse IDEindex.jsp<%@taglib uri="http://struts.apache.org/tags-html" prefix="html"%> <%@taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%> <html> <head> <title>Struts html:errors tag example</title> </head> <body> <h3>Struts html:errors tag example</h3> <div style="color: red;"> <html:errors/> </div> <html:form action="/errorsAction"> <bean:message key="label.name"/>: <html:text property="name" name="errorsForm"/><br/> <bean:message key="label.password"/>: <html:text property="password" name="errorsForm"/><br/> <br/> <html:submit><bean:message key="label.submit" /></html:submit> <html:reset><bean:message key="label.reset" /></html:reset> </html:form> </body> </html>web.xml<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd" > <web-app> <display-name>Maven Struts Examples</display-name> <servlet> <servlet-name>action</servlet-name> <servlet-class> org.apache.struts.action.ActionServlet </servlet-class> <init-param> <param-name>config</param-name> <param-value> /WEB-INF/struts-config.xml </param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>action</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> </web-app>struts-config.xml<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.3//EN" "http://struts.apache.org/dtds/struts-config_1_3.dtd"> <struts-config> <form-beans> <form-bean name="errorsForm" type="org.r4r.struts.ErrorsForm"/> </form-beans> <global-exceptions /> <global-forwards /> <action-mappings > <action path="/errors" type="org.apache.struts.actions.ForwardAction" parameter="/index.jsp"/> <action path="/errorsAction" type="org.r4r.struts.ErrorsAction" name="errorsForm" input="/index.jsp"> <forward name="success" path="/success.jsp"/> </action> </action-mappings> <message-resources parameter="org.r4r.struts.ApplicationResources" /> </struts-config>ErrorsForm.javapackage org.r4r.struts; import javax.servlet.http.HttpServletRequest; import org.apache.struts.action.ActionErrors; import org.apache.struts.action.ActionForm; import org.apache.struts.action.ActionMapping; import org.apache.struts.action.ActionMessage; @SuppressWarnings("serial") public class ErrorsForm extends ActionForm { private String name; private String password; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) { ActionErrors errors = new ActionErrors(); if (name == null || name.length() < 1) { errors.add("name", new ActionMessage("error.name.required")); } if (password == null || password.length() < 1) { errors.add("password", new ActionMessage("error.password.required")); } return errors; } }ErrorsAction.javapackage org.r4r.struts; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.struts.action.Action; import org.apache.struts.action.ActionForm; import org.apache.struts.action.ActionForward; import org.apache.struts.action.ActionMapping; public class ErrorsAction extends Action { public ActionForward execute(ActionMapping mapping,ActionForm form, HttpServletRequest request,HttpServletResponse response) throws Exception{ @SuppressWarnings("unused") ErrorsForm passwordForm=(ErrorsForm)form; return mapping.findForward("success"); } }ApplicationResources.properties#label message label.name = User Name label.password = Password label.submit = Submit label.reset = Reset error.name.required= Name is required<br/> error.password.required= Password is requiredsuccess.jsp<%@taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%> <html> <head> <title>Struts html:errors tag example</title> </head> <body> <h3>Struts html:errors tag Example</h3> <h4>Welcome , <bean:write name="errorsForm" property="name" /></h4> </body> </html>Output
Previous | Home | Next |