Sunteți pe pagina 1din 6

When ever the submit button is clicked it first goes to web.xml it matches with the servlet mapping *.

do pattern and Invokes the struts-config.xml it has certain tags like action mappings tag.formbeans tag,global forwards tag,global exceptions tag First the request goes to action mapping tag and picks up the action which has the same url pattern from which the request was sent i., from register.jsp the url pattern is action =\register and the action has

other attributes like

<action path="/register" type="app.RegisterAction" name="RegisterForm" input = "/register.jsp" scope="request" validate="true">

<forward name="success" path="/success.jsp"/> <forward name="failure" path="/failure.jsp"/>

type= gives the location of the java file name = gives the name of the formbean input = gives the name of the jsp to be invoked if so something goes wrong scope=gives the response on request BASED ON THE REQUEST CORRESPONING ACTION FORM AND ACTION CLASS ARE INVOKED .

In the Action Class whenever the logical name success is encountered success.jsp is invoked and if failure is encountered failure.jsp is invoked so that is the use of forward tag in strutsconfig.xml JSP (VIEW LAYER)
<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Insert title here</title> </head> <body> <form method = post action=\register> Username<input type = text name = username><br> Password<input type = password name = password><br> Password<input type = password name = password><br> <input type = submit value = submit> <input type = submit value = reset> </form> </body> </html>

Web.xml

<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE web-app PUBLIC '-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN' 'http://java.sun.com/dtd/web-app_2_3.dtd'> <web-app> <servlet> <servlet-name>action</servlet-name> <servlet-class>org.apache.struts.action.ActionServlet</servlet-class> <init-param> <param-name>application</param-name> <param-value>ApplicationResources</param-value> </init-param> <init-param> <param-name>config</param-name> <param-value>/WEB-INF/struts-config.xml</param-value> </init-param> <init-param> <param-name>debug</param-name> <param-value>3</param-value> </init-param> <init-param> <param-name>detail</param-name> <param-value>3</param-value> </init-param> <load-on-startup>1</load-on-startup>
</servlet> <!-- Action Servlet Mapping --> <servlet-mapping> <servlet-name>action</servlet-name> <url-pattern>/do/*</url-pattern> </servlet-mapping> <!-- The Welcome File List --> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <!-- tag libs --> <taglib> <taglib-uri>tld/struts-bean</taglib-uri> <taglib-location>/WEB-INF/tlds/struts-bean.tld</taglib-location> </taglib> <taglib> <taglib-uri>tld/struts-html</taglib-uri> <taglib-location>/WEB-INF/tlds/struts-html.tld</taglib-location> </taglib> <taglib> <taglib-uri>tld/struts-logic</taglib-uri> <taglib-location>/WEB-INF/tlds/struts-logic.tld</taglib-location> </taglib> <taglib> <taglib-uri>jstl/c</taglib-uri> <taglib-location>/WEB-INF/tlds/c.tld</taglib-location> </taglib> </web-app>

Struts-Config.xml <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE web-app PUBLIC '-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN' 'http://java.sun.com/dtd/web-app_2_3.dtd'> <struts-config> <form-beans> <form-bean name="RegisterForm" type="app.RegisterForm"/> </form-beans> <action-mappings> <action path="/register" type="app.RegisterAction" name="RegisterForm" input = "/register.jsp" scope="request" validate="true"> <forward name="success" path="/success.jsp"/> <forward name="failure" path="/failure.jsp"/> </action> </action-mappings> </struts-config> RegisterAction(Controller Layer) package app; import org.apache.struts.action.*; import org.apache.struts.action.ActionForm; import org.apache.struts.action.ActionForward; import org.apache.struts.action.ActionMapping; import javax.servlet.http.*; public class RegisterAction extends Action { public ActionForward execute(ActionMapping mapping, ActionForm form,HttpServletRequest req,HttpServletResponse res){ RegisterForm fm = (RegisterForm)form; String uname = fm.getUsername(); String pwd1 = fm.getPassword1(); String pwd2 = fm.getPassword2(); System.out.println(pwd1); System.out.println(pwd2); System.out.println(uname); System.out.println("check it out"); if (pwd1.equals(pwd2)&&uname!=null) { System.out.println("success"); return mapping.findForward("success"); } else {System.out.println("failure"); return mapping.findForward("failure"); } } }

RegisterForm(Model) package app;

import org.apache.struts.action.*; import org.apache.struts.action.ActionForm; import org.apache.struts.action.ActionForward; import org.apache.struts.action.ActionMapping; import org.apache.struts.util.MessageResources; public class RegisterForm extends ActionForm{ protected String username; protected String password1; protected String password2; public void setUsername(String username){ this.username = username; } public void setPassword1(String pwd1){ password1 = pwd1; }

public void setPassword2(String pwd2){ password2 = pwd2; } public String getUsername(){ } public String getPassword1(){ } public String getPassword2(){ } } Struts is a framework for developing web application It follows MVC-2 Architecture Earlier when using JSPs and view and business logic were lumped together So when and exception occurs it was difficult to debug where the error is from So in mvc 2 architecture everything is saperated model is handled by action form View by jsps and controller by action class this makes debugging easy. All Action classes must extend struts Action Class The main method in the struts action class is execute method it has four parameters
ActionMapping mapping, ActionForm form,HttpServletRequest req,HttpServletResponse res

return username; return password1;

return password2;

The ActionForm instance is created in the Action class and all the setter methods in the ActionForm are called using the get method Business Logic is written in this Action class All form classes must extend struts Action form The Two methds inside the Action Form are reset and validate

Earlier in struts 1.2 Perfom method was used in place of execute method

S-ar putea să vă placă și