26
Apache Struts Version 1.0.

ILP J2EE Stream J2EE 10 Struts v0.3

Embed Size (px)

DESCRIPTION

The Objective of J2EE course are:To understand multi-tiered enterprise applications.To understand J2EE framework for developing enterprise applications.To understand various components of J2EE like JSP, Servlets, EJB etc. and effectively use them.To understand Application Server and its configurations.To learn and deploy web based applications in application server.To learn supporting technologies like XML, JavaScript and Strut framework

Citation preview

Page 1: ILP J2EE Stream J2EE 10 Struts v0.3

Apache StrutsVersion 1.0.

Page 2: ILP J2EE Stream J2EE 10 Struts v0.3

April 8, 2023TCS Internal

Introduction•Apache Struts is a framework which helps developing J2EE applications using Model 2 MVC architecture.

•In the normal model 2 MVC architecture, servlets acts as controller, bean acts as model and JSP pages acts as view.

•There is no link between all the three and the programmer has to manually do the link in his code.

•Using Struts, the link between model, view and controller is automatically taken care.

Page 3: ILP J2EE Stream J2EE 10 Struts v0.3

April 8, 2023TCS Internal

Important Classes•The central part of the framework is ActionServlet.•This class controls navigational flow.•The class Action is used to access business objects.•The input from the user is handled by ActionForm class.•The class ActionMapping bundles the above classes together.

Page 4: ILP J2EE Stream J2EE 10 Struts v0.3

April 8, 2023TCS Internal

Struts Framework

ActionServlet

Action

ActionFormInitial Page

(HTML/JSP)

JSP JSPJSP

STRUCTS-CONFIG.XML

Page 5: ILP J2EE Stream J2EE 10 Struts v0.3

April 8, 2023TCS Internal

Action Form•The ActionForm is a JavaBean that extends org.apache.struts.action.ActionFrom.

•This object captures the input fields sent through the request.•ActionForm has a corresponding property for each field on the HTML form.

Page 6: ILP J2EE Stream J2EE 10 Struts v0.3

April 8, 2023TCS Internal

ActionForm Examplepackage app;import org.apache.struts.action.*;public class LoginForm extends ActionForm{protected String username;protected String password;public String getUserName(){ return username;}public String getPassword(){ return password; }public void setUsername(String username){

this.username = username;}public void setPassword(String password){

this.password = password;}}

Page 7: ILP J2EE Stream J2EE 10 Struts v0.3

April 8, 2023TCS Internal

The corresponding HTML page

Page 8: ILP J2EE Stream J2EE 10 Struts v0.3

April 8, 2023TCS Internal

Action•Action is responsible doing the actual business logic such a validation, accessing business information, etc.

•Depending on the result of action, it takes a decision of which ActionForward should be sent, and informs it to the action servlet.

•The core method of Action object is perform. The new versions use the method execute instead of perform.

Page 9: ILP J2EE Stream J2EE 10 Struts v0.3

April 8, 2023TCS Internal

Action - Examplepackage app;import org.apache.struts.action.*;import javax.servlet.http.*;import java.io.*;public class LoginAction extends Action {public ActionForward perform(ActionMapping mapping,

ActionForm form, HttpServletRequest req, HttpServletResponse res){

LoginForm lf = (LoginForm) form;String username = lf.getUsername();String password = lf.getPassword();if(DbUtil.verifyUser(username, password)){

return mapping.findForward(“success”);}else{

return mapping.findForward(“failure”);}

}

Page 10: ILP J2EE Stream J2EE 10 Struts v0.3

April 8, 2023TCS Internal

ActionMapping Class•In web applications, resources are identified through a Uniform Resource Identifier (URI) mechanism.

•Resources includes HTML pages, JSP pages, and any custom actions.

•ActionMapping objects are used to give custom Actions for URI, or path.

•Action mapping object is built using the struts-config.xml file.

Page 11: ILP J2EE Stream J2EE 10 Struts v0.3

April 8, 2023TCS Internal

Struts Configuration File•This is an XML file with name struts-config.xml•This file contains details that ActionServlet needs to handle the requests made to your application.

•This file is stored in the WEB-INF folder.

Page 12: ILP J2EE Stream J2EE 10 Struts v0.3

April 8, 2023TCS Internal

struts-config.xml<?xml version=“1.0” encoding=“ISO-8859-1” ?><!DOCTYPE struts-config PUBLIC“-//Apache SoftwareFoundation//DTD Struts Configuaration 1.0//EN”http://jakarta.apache.org/struts/dtds/struts-config_1_0.dtd>

<struts-config><form-beans>

<form-bean name=“loginForm” type=“app.RegistrationForm”/></form-beans><action-mappings>

<action path=“/login”type=“app.LoginAction”name=“loginForm”,input=“login.jsp”<forward name=“success” path=“/success.html”/><forward name=“failure” path=“/failure.html”/>

</action><action-mappings>

</strut-config>

Page 13: ILP J2EE Stream J2EE 10 Struts v0.3

April 8, 2023TCS Internal

ActionServlet Object•This is the core object which works behind the scenes, binding other components together.

Page 14: ILP J2EE Stream J2EE 10 Struts v0.3

April 8, 2023TCS Internal

The complete picture•A client requests a path that matches the Action URI pattern.•The container passes the request to ActionServlet.•ActionServlet looks up the mapping for the path in configuration file.

•If the mapping specifies a form bean, the ActionServlet sees if there is one already exists or creates a new one.

•If it is a form bean, the ActionServlet resets and populates it from the HTTP request.

•If the mapping has a validate property set to true, it calls validate on form bean.

Page 15: ILP J2EE Stream J2EE 10 Struts v0.3

April 8, 2023TCS Internal

The Complete Picture (Contd...)•If it fails, the servlet forwards to the path specified by the input property and the control flow ends.

•If the mapping specifies an Action type, it is reused if it already exists or instantiated.

•The Action’s perform or execute method is called and passed the instantiated form bean.

•The Action may populate the form bean, call business objects, and do what ever is needed.

•The Action returns ActionForward object to the ActionServlet.•The ActionServlet forwards the jsp page depending on ActionForward result.

Page 16: ILP J2EE Stream J2EE 10 Struts v0.3

April 8, 2023TCS Internal

The Strut Config Elementsdata-sources Contains a set of DataSource objects (JDBC 2.0 )

data-source Specifies a DataSource object that is to be instantiated.

set-property Specifies a method name and initial value of an additional JavaBean

global-exceptions

Describes a set of exceptions that might be thrown by an Action object

exceptions Registers an ExceptionHandler for an exception type

form-beans A set of form bean descriptors for this application module.

form-bean Describes Actionform subclass that can be referenced by Action object

Page 17: ILP J2EE Stream J2EE 10 Struts v0.3

April 8, 2023TCS Internal

The Strut Config Elementsform-properties Describes a JavaBean property that can be used to configure ActionForm object

global-forwards Describes a set of ActionForward objects that are available to All Action objects.

forward Describes an ActionForward that is to be made available to an Action as a return value.

action-mappings Describes a set of action mapping that are available for processing.

action Describes an ActionMapping object that is to be used to process a request for a specific

controller Describes the controller config bean that encapsulates an application module’s runtime configuration.

message resources

Describes a MessageResources object with message templates for this module.

pulg-in Specifies the fully qualified class name of general purpose application plug-in module.

Page 18: ILP J2EE Stream J2EE 10 Struts v0.3

April 8, 2023TCS Internal

What are tags?•Normal browsers render HTML pages.•Any java program (servlets) has to print all the HTML tags along with dynamic content. This is a tedious process.

•Tags enable to embed java code along with HTML code.•JSP is a set of tag library.•JSTL and Struct provides some additional feature in addition to the normal tags. These are called as Tag extensions.

Page 19: ILP J2EE Stream J2EE 10 Struts v0.3

April 8, 2023TCS Internal

Example: Using ServletStudent s = new Student(“David”);

out.println(“<html>”);

out.println(“<head><title>Student Info</title></head”);

out.println(“<body>”);

out.println(“<h1>”);

out.println(“Student name is “ + s.getName());

out.println(“</h1>”);

out.println(“</body>”);

out.println(“</html>”);

Page 20: ILP J2EE Stream J2EE 10 Struts v0.3

April 8, 2023TCS Internal

Using JSP Tag<%

Student s = new Student(“David”);

%>

<html>

<head><title>Student Info</title></head”);

<body>

<h1>

Student name is <%= s.getName() %>

</h1>

</body>

</html>

Page 21: ILP J2EE Stream J2EE 10 Struts v0.3

April 8, 2023TCS Internal

Example: Using tag extension<%@ taglib uri=“/tags/struts-bean” prefix=“bean”%>

<html>

<head><title>Student Info</title></head”);

<body>

<h1>

Student name is <bean:write name=“Student” property=“name”/></h1>

</body>

</html>

Page 22: ILP J2EE Stream J2EE 10 Struts v0.3

April 8, 2023TCS Internal

Struct Tag Libraries•struts-bean library

– Tags useful in accessing JavaBeans and their properties•struts-html library

– Tags used to create HTML input forms•struts-logic library

– Tags used for generating conditional output like looping, etc.

Page 23: ILP J2EE Stream J2EE 10 Struts v0.3

April 8, 2023TCS Internal

Bean Tags•cookie variable to handle request cookies.•define variable based on specified bean property•header variable based on specified request header.• include To load response from dynamic application request•message Renders internationalized message string•page Expose the item from the page context as a bean•parameter variable for request parameter• resource loads application resource•size bean containing number of elements in collection•struts exposes struts internal configuration•write renders the value of a bean property

Page 24: ILP J2EE Stream J2EE 10 Struts v0.3

April 8, 2023TCS Internal

HTML tags• base• button• checkbox• errors• file • form• hidden• html• image• img• link

• messages• option, options• password• radio• reset• rewrite• select• submit• text• textarea

Page 25: ILP J2EE Stream J2EE 10 Struts v0.3

April 8, 2023TCS Internal

Logic Tags

Evaluation Tags

For testing if values are equal, less than, etc.

Control-flow tags

fore redirecting or forwarding request

Repeat Tags for iterating over any type of collection.

Page 26: ILP J2EE Stream J2EE 10 Struts v0.3

April 8, 2023TCS Internal

Reference:•Struts in Action: Building web applications with the leading Java framework, Ted Husted et. al., Mannin Publication, 2004.