Struts Framework Day-2 Ashok Chakravarti. DataSource Usage Sample Struts-config.xml ….......................

Slides:



Advertisements
Similar presentations
Struts Portlet Adding an Action Copyright © Liferay, Inc. All Rights Reserved. No material may be reproduced electronically or in print without.
Advertisements

Web Development with Karsten Schulz Terp-Nielsen Master Principal Sales Consultant Oracle Denmark.
Apache Struts Technology
19 augustus 2003augustus 2003 JSP-2. BICT 2JDBC BICT 3Install MySQL Download MySQL daemon – Free – Windows version… Start Daemon – Mysqld-nt.exe Download.
Web MVC-2: Apache Struts Rimon Mikhaiel
MVC Frameworks Alternatives to coding the MVC pattern.
Browsers and Servers CGI Processing Model ( Common Gateway Interface ) © Norman White, 2013.
Struts1 Apache Struts Dr Jim Briggs. Struts2 What is Struts? Struts is an open source framework for building Java web applications Supports MVC/Model.
Internet Technologies 1 Master of Information System Management Java Server Faces Model/View/Controller Design Pattern for Web Development Slides.
Struts Basics SSE USTC Qing Ding. Agenda What is and Why Struts? Struts architecture – Controller: Focus of this presentation – Model – View Struts tag.
Chapter 14: Advanced Topics: DBMS, SQL, and ASP.NET
Apache Struts Technology A MVC Framework for Java Web Applications.
Struts 2.0 an Overview ( )
UNIT-V The MVC architecture and Struts Framework.
Lecture 2 - Struts ENTERPRISE JAVA. 2 Contents  Servlet Deployment  Servlet Filters  Model View Controllers  Struts  Dependency Injection.
Form Handling, Validation and Functions. Form Handling Forms are a graphical user interfaces (GUIs) that enables the interaction between users and servers.
1 CIS336 Website design, implementation and management (also Semester 2 of CIS219, CIS221 and IT226) Lecture 9 JavaServer Pages (JSP) (Based on Møller.
Advance Database Management Systems Lab no. 5 PHP Web Pages.
JavaScript Form Validation
BEST PRACTICES - Java By Configuration Use global-forwards/results Helps to avoid duplicate jsp files and redundancy forward mapping.
Author: DoanNX Version 2.0/Time: 30’. The ways to solve it There are 2 main ways:  Client-side: Using JavaScript (now one very good option is JQuery).
1 Customizing Kuali: A Technical Perspective Naser Alavi (Michigan State University) Warren Liang (University of California, Irvine)
CSCI 6962: Server-side Design and Programming Validation Tools in Java Server Faces.
CSE 305 Theory of Database Tutorial on Connecting with Sybase from Java program and Developing GUI Jalal Mahmud, TA, CSE 305.
Using JavaBeans and Custom Tags in JSP Lesson 3B / Slide 1 of 37 J2EE Web Components Pre-assessment Questions 1.The _____________ attribute of a JSP page.
Introduction to MVC and the Jakarta Struts Framework By Gyanendra Dwivedi Software Consultant.
WEEK 3 AND 4 USING CLIENT-SIDE SCRIPTS TO ENHANCE WEB APPLICATIONS.
Java Servlets and Java Server Pages Carol Wolf Computer Science.
Active Server Pages ASP is Microsoft’s server-side script engine for dynamically-generated web pages. Most common language used is VBScript. If you use.
Basic Struts Architecture Client Server Database Request Response Control View Model Server Struts Framework.
Chapter 7 Java Server Pages. Objectives Explain how the separation of concerns principle applies to JSP Describe the operation and life-cycle of a JSP.
CSCI 6962: Server-side Design and Programming Introduction to Java Server Faces.
Jakarta Struts Presented by Object Computing, Inc. (OCI) Written by Greg Elliott
Java Server Pages A JSP page is a text-based document that contains two types of text: static template data, which can be expressed in any text-based format,
Mark Dixon 1 12 – Java Beans. Mark Dixon 2 Session Aims & Objectives Aims –To cover the use of Java Beans Objectives, by end of this week’s sessions,
CGS – 4854 Summer 2012 Web Site Construction and Management Instructor: Francisco R. Ortega Chapter 2.
8/15/2003System Specialists Corporation How to implement BC4J with the Struts Framework Eugene Sicat Technical Architect/Project Manager System Specialists.
By: Saurabh Dixit.  Groovy server pages  Taglibs  Validators in grails.
Struts J2EE web application framework “ Model 2 ” Model View Controller Controller Servlet Key features XML metadata Struts taglib Simplified form validation.
Lecturer: Prof. Piero Fraternali, Teaching Assistant: Alessandro Bozzon, Advanced Web Technologies: Struts–
Using Client-Side Scripts to Enhance Web Applications 1.
Mark Dixon 1 09 – Java Servlets. Mark Dixon 2 Session Aims & Objectives Aims –To cover a range of web-application design techniques Objectives, by end.
 Whether using paper forms or forms on the web, forms are used for gathering information. User enter information into designated areas, or fields. Forms.
How to Build a Struts Application with JBuilder 9.
Domain Driven Web Development With WebJinn Sergei Kojarski College of Computer & Information Science Northeastern University joint work with David H. Lorenz.
Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. Forms with Spring MVC Handling Form.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 31.1 Reviewing the Bookstore Application 31.2.
Struts Framework Anna Paščenko. What is Struts?  An open source framework for building Java web applications.
Struts An Open-source Architecture for Web Applications Facilitator: Tripti Shukla.
REVOLUTION TO NEXT GENERATION Struts2.0 20/02/ Avishek Arang ::
J2EE Struts 1 3 Struts: Introduction 1 4 Once you’ve coded a lot of JSP applications, you find yourself doing a lot of repetitive things. Also,
CSC 2720 Building Web Applications JavaServer Pages (JSP) JSP Directives and Action Elements.
CSCI 6962: Server-side Design and Programming JSF DataTables and Shopping Carts.
©SoftMooreSlide 1 Introduction to HTML: Forms ©SoftMooreSlide 2 Forms Forms provide a simple mechanism for collecting user data and submitting it to.
COMP9321 Web Application Engineering Semester 2, 2015 Dr. Amin Beheshti Service Oriented Computing Group, CSE, UNSW Australia Week 3 1COMP9321, 15s2, Week.
1 Architecting Web Applications using JSF & Struts ASSIGNMENT GUIDELINE.
8 Chapter Eight Server-side Scripts. 8 Chapter Objectives Create dynamic Web pages that retrieve and display database data using Active Server Pages Process.
Preface IIntroduction Objectives I-2 Course Overview I-3 1Oracle Application Development Framework Objectives 1-2 J2EE Platform 1-3 Benefits of the J2EE.
13 Copyright © 2004, Oracle. All rights reserved. Adding Validation and Error Handling.
11 Copyright © 2004, Oracle. All rights reserved. Customizing Actions.
Implementation Struts Framework for well-architectured web applications Model-View-Controller design pattern.
JavaServer Faces framework Craig McClanahan is presented that created Struts web framework and based on experience gathered designed JavaServer.
 Java Server Pages (JSP) By Offir Golan. What is JSP?  A technology that allows for the creation of dynamically generated web pages based on HTML, XML,
APACHE STRUTS ASHISH SINGH TOMAR ast2124. OUTLINE Introduction The Model-View-Controller Design Pattern Struts’ implementation of the MVC Pattern Additional.
Struts2 Validation using XML Approach. May 12, 2011 Need For Validation Validation can be defined as the assessment of an action so as to ensure that.
12 Copyright © 2004, Oracle. All rights reserved. Using ADF Struts Components.
Apache Struts Technology A MVC Framework for Java Web Applications.
V Web Tier Architecture MVC and Struts. v First, there were servlets And things were good Faster than CGI programs More scalable with built-in.
Struts 2 Development. Topics  Roles in Struts Development  Control Flow  Actions  Struts 2 Views and Target  Struts 2 Custom Tags  Validation 
Web Programming– UFCFB Lecture 17
Presentation transcript:

Struts Framework Day-2 Ashok Chakravarti

DataSource Usage Sample Struts-config.xml …

Sample of code under execute() public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { javax.sql.DataSource dataSource = null; java.sql.Connection myConnection = null; try { dataSource = getDataSource(request); myConnection = dataSource.getConnection(); PreparedStatement statement = myConnection.prepareStatement("SELECT password FROM BasicAuthentication WHERE userid=?");

Sample of code under execute() statement.setString(1, loginForm.getUserName()); ResultSet resultSet = statement.executeQuery(); if(resultSet.next()) { String password = resultSet.getString("password"); if((password != null) && password.equalsIgnoreCase(loginForm.getPassword())) { target = "success"; request.setAttribute("userName", loginForm.getUserName()); } else { target = "failure"; }

Sample of code under execute() catch (SQLException sqle) { getServlet().log("Connection.process", sqle);} finally { try { if(myConnection != null) {myConnection.close();} } catch (SQLException e) { getServlet().log("Connection.close", e); }

If action mapping defined in the configuration file, the Controller Servlet/RequestProcessor will perform the following: Check session or request for instance of bean of appropriate class If no session/request bean exists, one is created automatically For every request parameter whose name corresponds to the name of a property in the bean, the corresponding setter method will be called The validate() method of ActionForm is called The updated ActionForm bean will be passed to the Action Class execute() method when it is called, making these values immediately available The execute() method must return an instance of ActionForward – instructing the controller which is the next view (page) The next page is merged with data passed in request or session object, and then renders HTML Request Processing Basics

Form Construction The Basics of Form Processing This section provides information on the following tags: - Render an HTML element - T ext box INPUT element — INPUT type=hidden element — Place a Submit INPUT element — Place a Submit INPUT element which can be used to “cancel” workflow

” simplAction ” - action name corresponds to action name in configuration <action path="/simpleAction" cancellable="true" type="edu.depaul.struts.SimpleAction" name="simpleBean" //– name of bean mapped to form controls scope="request" input="/index.jsp" >

Generates HTML: Bean form bound: public class SimpleForm extends ActionForm { private String firstName; private String lastName;

Where address is a form bean property which will be set with a selected value. Generates:

Validation can be provided: FormBean.validate(ActionMapping mapping, HttpServletRequest request) Directly in the Action.execute() method: ActionErrors errors = new ActionErrors(); errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("error.task.notassigned")); if (!errors.empty()) { saveErrors(request, errors); return (mapping.findForward("failure")); } Input Validation

Message Configuration Resource Bundle specified in a struts-config file: Simple Java properties file: message_key=Message Text Usage at Form to display errors - outputs all error messages - outputs a message for property “ propName ” if one exists

DynaActionForms org.apache.struts.action.DynaActionForm config: No need to write any Java. Validation? There is no validate() method. Solution1: validate in Action Solution 2: sub-class the DynaActionForm class and add a validate () method Solution 3: Use validation framework Disadvantage: not type safe, typos can lead to hours of debugging (no Compile time type checking)

execute(...) { DynaActionForm loginForm = (DynaActionForm )form; String name = (String)myForm.get(“name”); } DynaActionForms inside Action

Validation Framework Jakarta Commons Validator Framework Deployed as a Struts Plugin Extended by Struts to provide generic validation rules Common Rules: check for required fields check for max/min size check for the right data type

Validator Built-in Rules required – field data provided minlength – more than min length maxlength – less than max length range – in range of values mask – correct format (takes regexp) date – validates correct date format – validates correct fromat

Validator Files validation-rules.xml – contains all possible rules validation.xml – contains mappings of forms, properties and rules

Validator Setup Following entry needs to be added to struts-config.xml <plug-in className="org.apache.struts.validator.ValidatorPlugIn"> <set-property property="pathnames" value="/org/apache/struts/validator/validator- rules.xml, /WEB-INF/validation.xml"/>

Questions??

DispatchAction org.apache.struts.actions.DispatchAction class provides a mechanism for modularizing a set of related functions into a single action. abstract Action that dispatches to a public method that is named by the request parameter whose name is specified by the parameter property. This Action is useful to combine many similar actions into a single Action class and help in simplifying application design.

Sample DispatchAction Class public ActionForward createAccount(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception public ActionForward changePassword(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception public ActionForward deleteAccount(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception

Sample struts-config.xml

Changes to ActionForm The getter needed for this special parameter determines which radio button is initially selected No setter needed for this special parameter It is automatically populated when the input form is submitted.

Sample Form

Sample JSP Page taglib uri=" prefix="html" %> address: Password: <html:radio property="operation" value="createAccount"/> Create Account <html:radio property="operation" value="changePassword"/> Change Password <html:radio property="operation" value="deleteAccount"/> Delete Account

Sample confirmation JSP Page taglib uri=" prefix="bean" %> Account Created Account successfully created for. Congratulations.

Sample confirmation JSP Page