Você está na página 1de 23

Chandan Bhardwaj

What is Struts? What is MVC? Model? View? Controller? View State?

Is framework for java based web application which is based on MVC Model-2 design pattern. It is open Source. Current release version 2.0 Developed in May 2000 and Released in June 2001 Apache Jakarta Project

Is a collection of class and interface which is based on certain design pattern to achive specific problem. Or Frameworks are reusable, semi-complete'' applications that can be applied to produce custom applications

The Struts Framework is a rich collection of java Libraries. The component of struts: Base Framework JSP tag libraries Tiles Plugin Validator Plugin

Design Pattern is Standard through which we have to achieve the goal. Java support 24 design pattern. MVC is one of them. MVC Design Pattern:- is to separate the application object (model) from the way it is represented to the user (view) from the way in which the user controls it (controller). It is two type:MVC Model 1 MVC Model 2 Struts support MVC Model2. What is difference between Model 1 and Model 2?

Struts does not specify how to implement Model part of MVC JSP and Struts Tag Libraries used for View part of MVC Struts Controller implemented as: ActionServlet and ActionMapping 3 Major Components Servlet controller (Controller) Java Server Pages (View) Application Business Logic (Model)
View Jsp page Response Request Controller Action Servlet Model

DAO

JSP

BO

The model provides an interface to the data and/or services used by an application. The Model components provide business logic. Model components come in different form can be as a basic java been or as EJBs or Web Services Model layer can be break into 3 conceptual sub layer:MVC
Model layer

External interface

Business logic Data access

The view components are responsible for presenting information to the users and accepting the input from them. The view object refers to the model. They are responsible for displaying the information provided by the model components. ActionForm Beans Extends the ActionForm class Create one for each input form in the application JavaBeans style properties must match form entries View layer can be broken down into the following component:JSP pages Form Beans JSP tag libraries Resource Bundles.

org.apache.struts.ActionServlet is a main component of Struts Struts use configuration file called struts-config.xml This file provides for a configuration of controller components (actions), resources, model components: JavaBeans and other resources
Action Servlet

Action

Request Processor

Action Forms are a Java Beans Extend org.apache.struts.action.ActionForm Override: validate() - optional reset() - optional Provide JavaBeans/style properties that match an HTML form input/output values

For every action in the application , a custom Action is defined These a similar to servlets Stateless do not put any state in the Action classes Extends: org.apache.struts.action.Action Implementations subclass this class and provide implementation of execute method: ActionForward execute(ActionMapping,ActionForm , HttpServletRequest , HttpServletResponse );

ActionMapping - provides access to the information stored in the configuration file (struts-config.xml) entry that configures this Action class. HttpServletRequest requestThis is the standard JSP or Servlet request object. HttpServletResponse responseThis is the standard JSP or Servlet response object. ActionForm formThis is the form bean. By this time, the form bean has been pre-populated and the validate() method has been called and returned with no errors. All the data entered by the user is available through the form bean. Action.execute() Return Value: ActionForward ActionForward represents a destination to which the controller, RequestProcessor, might be directed to perform a RequestDispatcher.forward or HttpServletResponse.sendRedirect to, as a result of processing activities of an Action class.

It is to perform the processing for all request by the Action Servlet. it takes each request and break down into several small tasks. The process () method of Request Processor Class:processMultipart() processPath() processContent() processPreprocessor() processActionCreate() processActionPerform() processActionConfig()

4 ActionForm 3 1 Web Browser displays page 2 Controller

Action 5 Model

struts-config

JSP Page

The model is comprised of a combination of EJBs, Java Beans, and a set of Java classes extending the Struts base class ActionForm The view is represented by JSPs. The controller is implemented by the Struts ActionServlet

Struts-Config.xml

Action
Action servlet Request processor

Bean Java

Jsp PoJo

DAO

BO Web.xml

JSP Page

Database

Login .jsp:-

<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %> <%@page contentType="text/html" pageEncoding="UTF-8"%> <!DOCTYPE html> <html:html>
<head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Login Page</title> </head> <body bgcolor="green" text="white"> <html:form action="myLogin"> Enter UserName : <html:text property="userName" /><br> Enter password : <html:password property="pass"/> <center><html:submit value="SignIn"/></center> </html:form> </body> </html:html>

Struts-Config.xml:<struts-config> <form-beans> <form-bean name="LoginForm" type="com.ats.practice.beans.LoginBean"/> </form-beans>

<global-exceptions></global-exceptions>
<global-forwards> </global-forwards> <action-mappings>

action-mapping name

<action path="/myLogin name="LoginForm type="com. actions.LoginAction"> <forward name="SUCCESS" path="/Success.jsp"/> <forward name="FAILURE" path="/Failure.jsp"/> mapping type </action> </action-mappings> forward definition
<controller processorClass="org.apache.struts.tiles.TilesRequestProcessor"/> <message-resources parameter="com/myapp/struts/ApplicationResource"/> </struts-config>

public class LoginAction extends Action{ public ActionForward execute(ActionMapping mapping,ActionForm form,HttpServletRequest request,HttpServletResponse response ){ LoginBean bean = (LoginBean)form; String userName = bean.getUserName(); String pass = bean.getPass(); LoginBO bo = new LoginBO(); String target="FAILURE"; boolean result = bo.authenticate(userName, pass); if(result) target="SUCCESS";

} }

return mapping.findForward(target);

public class LoginBean extends ActionForm { private String userName; private String pass; public String getUserName() { return userName; }
public void setUserName(String userName) { this.userName = userName; } public String getPass() { return pass; public void setPass(String pass) { this.pass = pass;

public class LoginBO { LoginDAO dao = new LoginDAO(); public boolean authenticate(String user, String pass){ boolean b = dao.verifyUser(user, pass); return b; } public class LoginDAO { public boolean verifyUser(String user,String pass){ if(user.equals("Admin")&& pass.equals(chandan")){ return true; } return false;

Thank you

Você também pode gostar