How to use html:messages to display errors in struts

Here I am explaining how to use html:messages to display errors in struts.

EmployeeForm

package com.htmlmessages;

import org.apache.struts.action.ActionForm;
/**
*
* @author Prasanthi
*
*/
public class EmployeeForm extends ActionForm{

String ename=null;
String ecode=null;

public String getEcode() {
return ecode;
}
public void setEcode(String ecode) {
this.ecode = ecode;
}
public String getEname() {
return ename;
}
public void setEname(String ename) {
this.ename = ename;
}

}

EmployeeAction

package com.htmlmessages;

import javax.servlet.http.*;
import org.apache.struts.action.*;
/**
*
* @author Prasanthi
*
*/
public class EmployeeAction extends Action{
public ActionForward execute(ActionMapping map,ActionForm form,HttpServletRequest req,HttpServletResponse res)
{
EmployeeForm emp=(EmployeeForm)form;

String name=emp.getEname();
String code=emp.getEcode();
req.setAttribute("n1",name);
req.setAttribute("c1",code);

ActionMessages errors = new ActionMessages();
if((name == null) || (name.length()==0))
errors.add("ename", new ActionMessage("error.ename.required"));
if((code == null) ||(code.length()==0))
errors.add("ecode", new ActionMessage("error.ecode.required"));
if(!errors.isEmpty())
{ saveMessages(req,errors);
return map.findForward("fail");
}
else

return map.findForward("success");
}

}

ApplicationResources.properties

error.ename.required=Name Is Required
error.ecode.required=Ecode is Required
title=welcome

struts-config.xml

<struts-config>
<form-beans>
<form-bean name="Empbean" type="com.htmlmessages.EmployeeForm" />
</form-beans>

<action-mappings>
<action path="/Empmessage"
type="com.htmlmessages.EmployeeAction"
name="Empbean"
input="/Employee-Messages.jsp"
validate="true">
<forward name="success" path="/Employee-Messages-success.jsp"/>
<forward name="fail" path="/Employee-Messages.jsp"/>
</action>
</action-mappings>
<message-resources parameter="ApplicationResources"/>
</struts-config>

web.xml


<web-app>

<servlet>
<servlet-name>action</servlet-name>
<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>

<load-on-startup>2</load-on-startup>
</servlet>

<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>

<welcome-file-list>
<welcome-file>Employee-Messages.jsp</welcome-file>
</welcome-file-list>
<taglib>
<taglib-uri>/tags/struts-html</taglib-uri>
<taglib-location>/WEB-INF/struts-html.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>/tags/struts-bean</taglib-uri>
<taglib-location>/WEB-INF/struts-bean.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>/tags/struts-logic</taglib-uri>
<taglib-location>/WEB-INF/struts-logic.tld</taglib-location>
</taglib>
</web-app>

Employee-Messages.jsp

<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html"%>
<%@ taglib uri="/tags/struts-bean" prefix="bean"%>
<%@ taglib uri="/tags/struts-logic" prefix="logic"%>
<html:html>

<body >
<br><br>
<html:form action="/Empmessage">
<logic:messagesPresent message="true">
<html:messages id="message" message="true">
<li><font color="red"><bean:write name="message" /></font></li>
</html:messages>
</logic:messagesPresent>
<center><h2> <u> Employee Form</u></h2>
<br><br>

Name<html:text property="ename" > </html:text> <br><br>
<html:messages id="message1" property="ename" message="true">
<font color="red"> <bean:write name="message1"/></font><br>
</html:messages>

Code<html:text property="ecode" > </html:text> <br><br>

<html:submit value="submit" />

</html:form>
</body>
</html:html>

Employee-Messages-success.jsp

<html>
<body ><center>
<h2><u>Employee Details</h2></u>
<h3> Name:<%=request.getAttribute("n1")%><br></h3>
<h3> Code:<%=request.getAttribute("c1")%><br></h3>
</center>

</body>
</html>

No comments: