< html:messages /> html tag

< html:messages /> html tag

Previous Home Next

 

The <html:messages/> tag is used to displays a collection of messages stored in an ActionErrors object,ActionMessages object,a String, or a String array object. The messages are placed into the page scope in the body of this tag where they can be displayed by standard JSP methods.

 
It defaults to retrieving the messages from Globals.ERROR_KEY, but if the message attribute is set to true then the messages will be retrieved from Globals.MESSAGE_KEY. In order to use this tag successfully, you must have defined an application scope MessageResources bean under the default attribute name.

The struts link tag have following attributes:- 

  • id* :-This is identifies the scripting variable that contain the current element of the collection of messages on each iteration.so it is not null.
  • bundle :-Names a key,stored in the servlet context attribute that is used when retrieving messages. If it not specified then the default ResourceBoundle is used.
  • footer :- This will be printed after the collection of messages.This is an optional resource key.
  • header :-This will be printed before the collection of messages.This is an optional resource key.
  • locale :-This is used to select messages to be displayed. If not specified, defaults to the Struts standard value.
  • message :-This attribute is set to 'true' the bean will be retrieved from the Globals.MESSAGE_KEY constant string.
  • name :-This is identifies  the scripting variable containing the collection of messages to be displayed.
  •  If not present, the name specified by the Globals.ERROR_KEY constant string will be used.
  • property :-This is identifies an input property for which retrieved messages should be display.if no value is indicated then all messages are rendered.


Directory Structure of MessagesTagExample in Struts 1.3 Using MyEclipse IDE




index.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:messages tag example</title>
</head>
<body>
<h3>Struts html:messages tag example</h3>
<div style="color: red;">
<html:messages id="errorName" property="name">
	<bean:write name="errorName" />
</html:messages><br/>
<html:messages id="errorPassword" property="password">
	<bean:write name="errorPassword" />
</html:messages>
</div>
<html:form action="/messagesAction">
<bean:message key="label.name"/>:
<html:text property="name" name="messagesForm"/><br/>
<bean:message key="label.password"/>:
<html:text property="password" name="messagesForm"/><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="messagesForm" type="org.r4r.struts.MessagesForm"/>
  </form-beans>
  <global-exceptions />
  <global-forwards />
  <action-mappings >
  <action path="/messages" type="org.apache.struts.actions.ForwardAction" parameter="/index.jsp"/>
  <action path="/messagesAction" type="org.r4r.struts.MessagesAction" name="messagesForm" input="/index.jsp">	
  <forward name="success" path="/success.jsp"/>
  </action>
  </action-mappings>
  <message-resources parameter="org.r4r.struts.ApplicationResources" />
</struts-config>

MessagesForm.java

package 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 MessagesForm 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;
		}
}

MessagesAction.java

package 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 MessagesAction extends Action {
	public ActionForward execute(ActionMapping mapping,ActionForm form,
		HttpServletRequest request,HttpServletResponse response) throws Exception{
		@SuppressWarnings("unused")
		MessagesForm passwordForm=(MessagesForm)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
error.password.required= Password is required

success.jsp

<%@taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%>
 
<html>
<head>
<title>Struts html:messages tag example</title>
</head>
<body>
<h3>Struts html:messages tag Example</h3>
<h4>Welcome , <bean:write name="messagesForm" property="name" /></h4>
</body>
</html>

Output




Previous Home Next