Step 2. In the JSP add <html:errors/> in the failure.jsp
<%@ taglib uri="/WEB-INF/struts-tiles.tld" prefix="tiles" %>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>
<html:form>
<html:errors/> // this is for display message from resource bundle in JSP
</html:form>
Step 3. In the the Resource Bundle. application_resource.properties file //Here you can add localization.
error.empname.required=Employee Name is Required
Step 4. In the Action class
public class EmpAction extends Action {
public ActionForward execute(ActionMapping mapping,
ActionForm form, HttpServletRequest request,
HttpServletResponse response) throws Exception
{
//your logic
ActionErrors errors = new ActionErrors(); // This is required for Error
EmpForm empform =(EmpForm)form;
String empName = empform.getFirstName()// User entered data you can get like this.
if(empName == null){
errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("error.empname.required"));
saveErrors(req, errors);
return mapping.findForward("failure");
} else {
return mapping.findForward("success");
}
}
Step 5. Add Action mapping and form entry into the stuts-confix.xml
package com.techfaq.form;
public class EmpForm extends ActionForm {
int empId;
String firstName;
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public int getEmpId() {
return empId;
}
public void setEmpId(int empId) {
this.empId = empId;
}
}
Now in the browser type http://localhost:8080/testApp/EmpForm.jsp
Don't Enter Emp name in the text box and submit the "Save" BUTTON.
Based on mapping.findForward("failure");
it will return failure.jsp.
In the browser you can see. In red color.