- When any struts based web application loaded into serverinitially it will load the ActionServlet class, due to<load-on-startup> tag in web.xml.
- It will read struts-config.xml file, due to <init-param> tag inweb.xml.
- When anybody makes request to the respective webapplication it will display the file which is mentioned in<welcome-file-list> tag of web.xml file.
- When the view page(.jsp) is displayed to the user internally the strutsframework (ActionServlet) will create an object for the formbean class
- Which extends the ActionForm by calling zero argument constructor(default construtor) which is mapped with the Action class path mentioned in .jsp file <html:form action=”/path”> later it will call the reset() and all the getter methods of the form bean class.
- When the user fills the form and submit the input, our form bean class again calls the default constructor, reset(), all setter() methods, validate() methods respectively.
- Validate method return ActionErrors object. If the ActionErrors object contains any error values(ActionError/ActionMessage values) then the ActionServlet calls the input page mapped with respective action class(<action input=”/input.jps”) that page will display to the user with error messages.
- If ActionErrors object is null(doesn’t contains any ActionError/ ActionMessage) then the ActionServlet calls the execute() method of the Action class which is bind with input request (<action path=”/path” type=”package.OurActionClass”).
- execute() method will return ActionForward object. Depend onthe value, again the ActionServlet will call the respectiveview(.jps page) as response and send it back to user.
Web.xml
<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><taglib><taglib-uri>/WEB-INF/struts-html.tld</taglib-uri><taglib-location>/WEB-INF/struts-html.tld</taglib-location></taglib>
struts-Config.xml
<action path=”/Name” type=”example.NameAction” name=”nameForm” input=”/index.jsp”><forward name=”success” path=”/displayname.jsp”/><forward name=”failure” path=”/index.jsp”/></action>
NameAction.java
public class NameAction extends Action {public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {String target = new String(“success”);if ( form != null ) {// Use the NameForm to get the request parametersNameForm nameForm = (NameForm)form;String name = nameForm.getName();}// if no mane supplied Set the target to failureif ( name == null ) {target = new String(“failure”);} else {request.setAttribute(“NAME”, name);}return (mapping.findForward(target));}}
NameForm.java
public class NameForm extends ActionForm {private String name = null;public String getName() {return (name);}public void setName(String name) {this.name = name;}public void reset(ActionMapping mapping, HttpServletRequest request) {this.name = null;}}
Index.jsp
<html><head><title>Sample Struts Application</title></head><body><html:form action=”Name” name=”nameForm” type=”example.NameForm”><table width=”80%” border=”0″><tr><td>Name:</td><td><html:text property=”name” /></td></tr><tr><td><html:submit /></td></tr></table></html:form></body></html>
displayName.jsp
<html><head><title>Sample Struts Display Name</title></head><body><table width=”80%” border=”0″><tr><td>Hello <%= request.getAttribute(“NAME”) %> !!</td></tr></table></body></html>
No comments:
Post a Comment