How to use html:errors to display errors in struts

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

EmployeeForm

package com.htmlerrors;

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.htmlerrors;

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);

ActionErrors errors=new ActionErrors();

if((name == null) || (name.length()==0))
errors.add("ename",new ActionError("error.ename.required"));
if((code == null) ||(code.length()==0))
errors.add("ecode",new ActionError("error.ecode.required"));

if(!errors.isEmpty())

{
saveErrors(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.htmlerrors.EmployeeForm" />
</form-beans>

<action-mappings>
<action path="/Emp"
type="com.htmlerrors.EmployeeAction"
name="Empbean"
input="EmployeeForm.jsp"
validate="true">
<forward name="success" path="/Employeesuccess.jsp"/>
<forward name="fail" path="/EmployeeForm.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>EmployeeForm.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>

EmployeeForm.jsp

<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html"%>
<%@ taglib uri="/tags/struts-bean" prefix="bean"%>
<html:html>
<body>
<br><br>
<html:form action="/Emp">
<center><h2> <u>Employee Form</u></h2>
<br><br>
Name<html:text property="ename" > </html:text> <br>
<font color="red"><html:errors property="ename"/></font><br><br>
Code<html:text property="ecode" > </html:text> <br>
<font color="red"><html:errors property="ecode"/></font> <br><br>
<html:submit value="submit" />

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

EmployeeSuccess.jsp

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

No comments: