2) ActionClass
package com.visualbuilder;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionMessages; import org.apache.struts.action.ActionForm; import org.apache.struts.action.ActionForward; import org.apache.struts.action.ActionMapping; import org.apache.struts.action.ActionMessage; import org.apache.struts.action.ActionMessages; import org.apache.struts.actions.Action;
import org.apache.struts.action.DynaActionForm;
public class DynaActionFormExample extends Action{
public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { DynaActionForm inputForm = (DynaActionForm)form;
//Create object of ActionMesssages ActionMessages errors = new ActionMessages(); //Check and collect errors if(((String)inputForm.get("name")).equals("")) { errors.add("name",new ActionMessage("error.name.required")); } if(((String)inputForm.get("email")).equals("")) { errors.add("email",new ActionMessage("error.email.required")); } //Saves the error saveErrors(request,errors); return mapping.getInputForward(); }
}
(3) JSP Page
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %> <%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %> <%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic" %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html:html> <logic:messagesPresent> <html:messages id="msg"> <p><strong><font color="red"><bean:write name="msg" /></font></strong></p> </html:messages> </logic:messagesPresent>
<logic:messagesPresent message="true"> <html:messages message="true" id="msg"> <p><strong><bean:write name="msg" /></strong></p> </html:messages> </logic:messagesPresent> <html:form action="/dynaActionExample.do" method="post"> <table> <tr><td>Enter the name :- </td><td><html:text property="name"/> </td></tr> <tr><td>Enter the Email:- </td><td><html:text property="email"/></td></tr> </table> <html:submit>Submit</html:submit> </html:form>
</html:html>
Output:-
The following will be the output of the application.
|