Ilp J2ee Stream J2ee 10 Struts V0.3

  • Uploaded by: Chacko Mathew
  • 0
  • 0
  • May 2020
  • PDF

This document was uploaded by user and they confirmed that they have the permission to share it. If you are author or own the copyright of this book, please report to us by using this DMCA report form. Report DMCA


Overview

Download & View Ilp J2ee Stream J2ee 10 Struts V0.3 as PDF for free.

More details

  • Words: 1,214
  • Pages: 26
Apache Struts Version 1.0.

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. TCS Internal

September 3, 2009

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.

TCS Internal

September 3, 2009

Struts Framework JSP

Initial Page (HTML/JSP)

JSP

JSP

Action Servlet

Action Form Action

STRUCTS-CONFIG.XML

TCS Internal

September 3, 2009

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.

TCS Internal

September 3, 2009

ActionForm Example package 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; } }

TCS Internal

September 3, 2009

The corresponding HTML page

TCS Internal

September 3, 2009

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 TCS Internal

September 3, 2009

Action - Example package 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”); } }

TCS Internal

September 3, 2009

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.

TCS Internal

September 3, 2009

Struts Configuration File •This is an XML file with name strutsconfig.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.

TCS Internal

September 3, 2009

struts-config.xml <struts-config>

TCS Internal

September 3, 2009

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

TCS Internal

September 3, 2009

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. TCS Internal

September 3, 2009

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. TCS Internal

September 3, 2009

The Strut Config Elements data-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

globalexceptions

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

TCS Internal

September 3, 2009

The Strut Config Elements formproperties

Describes a JavaBean property that can be used to configure ActionForm object

globalforwards

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.

actionmappings

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. TCS Internal

September 3, 2009

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. TCS Internal

September 3, 2009

Example: Using Servlet Student s = new Student(“David”); out.println(“”); out.println(“Student Info”); out.println(“

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

”); out.println(“”); out.println(“”); TCS Internal

September 3, 2009

Using JSP Tag <% Student s = new Student(“David”); %> Student Info

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

TCS Internal

September 3, 2009

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

Student name is

TCS Internal

September 3, 2009

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.

TCS Internal

September 3, 2009

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 TCS Internal

September 3, 2009

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

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

TCS Internal

September 3, 2009

Logic Tags

Evaluation For testing if values are equal, Tags less than, etc. Controlflow tags

fore redirecting or forwarding request

Repeat Tags

for iterating over any type of collection.

TCS Internal

September 3, 2009

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

TCS Internal

September 3, 2009

Related Documents


More Documents from "Chacko Mathew"