Model-View-Controller. and. Struts 2



Similar documents
Web Frameworks and WebWork

Web Applications and Struts 2

Java and Web. WebWork

Pentesting Web Frameworks (preview of next year's SEC642 update)

In this chapter, we lay the foundation for all our further discussions. We start

How To Understand The Architecture Of Java 2Ee, J2Ee, And J2E (Java) In A Wordpress Blog Post

Creating Java EE Applications and Servlets with IntelliJ IDEA

Servlet 3.0. Alexis Moussine-Pouchkine. mercredi 13 avril 2011

An introduction to creating JSF applications in Rational Application Developer Version 8.0

Web Container Components Servlet JSP Tag Libraries

Web Applications. Originals of Slides and Source Code for Examples:

Web Applications. For live Java training, please see training courses at

Course Name: Course in JSP Course Code: P5

DTS Web Developers Guide

Complete Java Web Development

Tutorial: Building a Web Application with Struts

Web Presentation Layer Architecture

CREATE A CUSTOM THEME WEBSPHERE PORTAL

Drupal CMS for marketing sites

JBoss Portlet Container. User Guide. Release 2.0

Web Programming II JSP (Java Server Pages) ASP request processing. The Problem. The Problem. Enterprise Application Development using J2EE

CrownPeak Java Web Hosting. Version 0.20

SSC - Web development Model-View-Controller for Java web application development

Developing Web Applications using JavaServer Pages and Servlets

Design and Functional Specification

BIRT Application and BIRT Report Deployment Functional Specification

JBoss SOAP Web Services User Guide. Version: M5

<Insert Picture Here> Hudson Security Architecture. Winston Prakash. Click to edit Master subtitle style

GlassFish v3. Building an ex tensible modular Java EE application server. Jerome Dochez and Ludovic Champenois Sun Microsystems, Inc.

Mastering Tomcat Development

PowerTier Web Development Tools 4

Aspect Oriented Programming. with. Spring

Glassfish, JAVA EE, Servlets, JSP, EJB

2. Follow the installation directions and install the server on ccc

This course provides students with the knowledge and skills to develop ASP.NET MVC 4 web applications.

Sightly Component Development

Tutorial: Building a Dojo Application using IBM Rational Application Developer Loan Payment Calculator

Oracle WebLogic Foundation of Oracle Fusion Middleware. Lawrence Manickam Toyork Systems Inc

Course Number: IAC-SOFT-WDAD Web Design and Application Development

Crawl Proxy Installation and Configuration Guide

MarkLogic Server. Reference Application Architecture Guide. MarkLogic 8 February, Copyright 2015 MarkLogic Corporation. All rights reserved.

zen Platform technical white paper

Alfresco. Wiley Publishing, Inc. PROFESSIONAL. PRACTICAL SOLUTIONS FOR ENTERPRISE. John Newton CONTENT MANAGEMENT. Michael Farman Michael G.

Case Studies of Running the Platform. NetBeans UML Servlet JSP GlassFish EJB

Getting Started with Web Applications

Web Application Developer s Guide

Server Setup and Configuration

An introduction to web programming with Java

Servlet and JSP Filters

Hello World Portlet Rendered with JSP for WebSphere Portal Version 4.1

Spring Security SAML module

Java Application Developer Certificate Program Competencies

MVC pattern in java web programming

Design Approaches of Web Application with Efficient Performance in JAVA

Client-server 3-tier N-tier

Getting Started Developing JavaScript Web Apps. this = that. VT Geospatial Forum 2015

Struts Tools Tutorial. Version: M5

Proposal for DSpace Web MVC

Java Servlet 3.0. Rajiv Mordani Spec Lead

Liferay Enterprise ecommerce. Adding ecommerce functionality to Liferay Reading Time: 10 minutes

S3 Monitor Design and Implementation Plans

HPC Portal Development Platform with E-Business and HPC Portlets

Developing ASP.NET MVC 4 Web Applications MOC 20486

NetBeans IDE Field Guide

Sonatype CLM Enforcement Points - Continuous Integration (CI) Sonatype CLM Enforcement Points - Continuous Integration (CI)

Web Application Architectures

Oracle Hyperion Financial Management Custom Pages Development Guide

Software Development Kit

Certified PHP/MySQL Web Developer Course

Development. with NetBeans 5.0. A Quick Start in Basic Web and Struts Applications. Geertjan Wielenga

Enterprise Application Development In Java with AJAX and ORM

Customer Bank Account Management System Technical Specification Document

Introducing Apache Pivot. Greg Brown, Todd Volkert 6/10/2010

Security Code Review- Identifying Web Vulnerabilities

enterprise^ IBM WebSphere Application Server v7.0 Security "publishing Secure your WebSphere applications with Java EE and JAAS security standards

Stock Trader System. Architecture Description

Oracle Hyperion Financial Management Developer and Customization Guide

Java with Eclipse: Setup & Getting Started

Modern Web Application Framework Python, SQL Alchemy, Jinja2 & Flask

Cache Configuration Reference

Oracle Forms Services Secure Web.Show_Document() calls to Oracle Reports Server 6i

Client-Server Architecture & J2EE Platform Technologies Overview Ahmed K. Ezzat

Web Development on the SOEN 6011 Server

ACM Crossroads Student Magazine The ACM's First Electronic Publication

Research Article. ISSN (Print) *Corresponding author Lili Wang

Oracle Identity Analytics Architecture. An Oracle White Paper July 2010

Install guide for Websphere 7.0

Business Process Management IBM Business Process Manager V7.5

Course Information Course Number: IWT 1229 Course Name: Web Development and Design Foundation

Web Application Architecture (based J2EE 1.4 Tutorial)

Workshop for WebLogic introduces new tools in support of Java EE 5.0 standards. The support for Java EE5 includes the following technologies:

Developing ASP.NET MVC 4 Web Applications

Developer Tutorial Version 1. 0 February 2015

Application Notes for Packaging and Deploying Avaya Communications Process Manager Sample SDK Web Application on a JBoss Application Server Issue 1.

Elgg 1.8 Social Networking

AD-HOC QUERY BUILDER

SESM Tag Library. Configuring a Tag Library APPENDIX

Transcription:

Model-View-Controller and Struts 2

Problem area Mixing application logic and markup is bad practise Harder to change and maintain Error prone Harder to re-use public void doget( HttpServletRequest request, HttpServletResponse response ) { PrintWriter out = response.getwriter(); out.println( <html>\n<body> ); if ( request.getparameter( foo ).equals( bar ) ) out.println( <p>foo is bar!</p> ); else out.println( <p>foo is not bar!</p> ); } out.println( </body>\n</html> );

Advantages Separation of application logic and web design through the MVC pattern Integration with template languages Some provides built-in components for Form validation Error handling Request parameter type conversion Internationalization IDE integration

Struts 2 Built on top of XWork a command pattern framework Integrated with the Spring IoC container Provides a clean implementation of the MVC pattern

The MVC pattern Breaks an application into three parts: Model: The domain object model / service layer View: Template code / markup Controller: Presentation logic / action classes Defines interaction between components to promote separation of concerns and loose coupling Each file has one responsibility Enables division of labour between programmers and designers Facilitates unit testing Easier to understand, change and debug

MVC with Front Controller Front controller. Maps request URLs to controller classes. Implemented as a servlet. Web browser. Displays output. Web page template like JSP or Velocity. Command instances/ Action classes. Interacts with backend services of the system. Backend services working with the API/ data model.

Action Flow Response to client (random.vm) Result Client HTTP request to URL (getrandomstring.action) web.xml Stack of interceptors struts.xml (config file) (Struts 2) Servlet dispatcher Stack of interceptors Action classes (User code) (GetRandomStringAction.java)

web.xml Servlet container configuration file Maps URL patterns to the Struts dispatcher Most typical pattern is *.action Located in WEB-INF/ folder Can redirect to the Filter- or ServletDispatcher <filter> <filter-name>struts</filter-name> <filter-class>org.apache.struts2.dispatcher.filterdispatcher</filter-class> </filter> <filter-mapping> <filter-name>struts</filter-name> <url-pattern>*.action</url-pattern> </filter-mapping>

struts.xml Struts (dispatcher) configuration file Located in root of classpath (by default) struts-default.xml is included automatically Maps URLs to action classes Maps result codes to results <struts> <package name="default" extends= struts-default"> <action name= invertstring" class="no.uio.inf5750.example.action.invertstringaction"> <result name="success" type="velocity">word.vm</result> </action> </package> </struts>

Action classes Java code executed when a URL is requested Must implement the Action interface or extend ActionSupport Provides the execute method Must return a result code (SUCCESS, ERROR, INPUT) Used to map to results Properties set by the request through public set-methods Properties made available to the response through public get-methods

Action classes HTTP request getrandomstring.action?word=someenteredword Implements Action Must correspond to request parameter, exposed through set-method Must correspond to property name in view, exposed through get-method Execute method with user code public class InvertStringAction implements Action { private String word; public void setword( String word ) { this.word = word; } private String invertedword; public String getinvertedword() { return this.invertedword; } public String execute() { char[] chars = word.tochararray(); // do some inverting here... Must return a result code (defined in Action) } } invertedword = buffer.tostring(); return SUCCESS;

View Struts 2 integrates with many view technologies: JSP Velocity Freemarker JasperReports Values sent to controller with POST or GET as usual Values made available to the view by the controller

View - velocity A popular template engine and -language Java Action class public String getinvertedword() { return this.invertedword; } <html> Form action to URL mapped in struts.xml Input name corresponding to set-method in action class Velocity parameter corresponding to get-method in action class <body> <form method= post action= invertstring.action > <p><input type= text name= word ></p> <p><input type= submit value= Invert ></p> </form> <p>$invertedword</p> </body> </html>

struts.xml (2) Different result codes can be mapped to different results <struts> <package name="default" extends= struts-default"> <action name= invertstring" class="no.uio.inf5750.example.action.invertstringaction"> <result name="success" type="velocity">word.vm</result> <result name= input type= velocity >input.vm</result> </action> </package> </struts>

struts.xml (3) Static parameters can be defined Requires public set-methods in action classes Automatic type conversion is provided <struts> <package name="default" extends= struts-default"> <action name= invertstring" class="no.uio.inf5750.example.action.getrandomstringaction"> <result name="success" type="velocity">random.vm</result> <param name= numberofchars >32</param> </action> </package> </struts>

struts.xml (4) Files can include other files Files are merged Facilitates breaking complex applications into manageable modules Specified files are searched for in classpath Configuration can be separated into multiple files / JARs <struts> <include file= struts-public.xml /> <include file= struts-secure.xml /> </struts>

struts.xml (5) Actions can be grouped in packages Useful for large systems to promote modular design A package can extend other packages Definitions from the extended package are included Configuration of commons elements can be centralized <struts> <package name="default" extends= struts-default"> <action name= invertstring class= no.uio.no.example.action.invertstringaction > <! mapping omitted --> </action> </package> <package name= secure extends= default > <! Secure action mappings --> </package> </struts>

struts.xml (6) Actions can be grouped in namespaces Namespaces map URLs to actions Actions identified by the name and the namespace it belongs to Facilitates modularization and improves maintainability <struts> <include file="webwork-default.xml"/> <package name= secure" extends= default namespace= /secure > <action name= getusername class= no.uio.inf5750.example.action.getusernameaction > <result name= success type= velocity >username.vm</result> </action> </package> </struts>

Interceptors Invoked before and/or after the execution of an action Enables centralization of concerns like security, logging <struts> <package name="default" extends= struts-default"> <interceptors> <interceptor name= profiling class= no.uio.example.interceptor.profilinginterceptor /> </interceptors> <action name= invertstring class= no.uio.no.example.action.invertstringaction > <result name= success type= velocity >word.vm</result> <interceptor-ref name= profiling /> </action> </package> </struts>

Interceptor stacks Interceptors should be grouped in stacks A default interceptor stack can be defined Should include the Struts default stack <struts> <! package omitted <interceptors> <interceptor name= profiling class= no.uio.example.interceptor.profilinginterceptor /> <interceptor name= logging class= no.uio.example.logging.logginginterceptor /> <interceptor-stack name= examplestack > <interceptor-ref name= defaultstack /> <interceptor-ref name= profiling /> <interceptor-ref name= logging /> </interceptor-stack> </interceptors> <default-interceptor-ref name= examplestack /> </struts>

Result types Determines behaviour between action execution and view rendering Several result types are bundled: Dispatcher (JSP) Default - will generate a JSP view Velocity Will generate a Velocity view Redirect Will redirect the request to the specified action after execution Chain Same as redirect but makes all parameters available to the following action

Result types Chain result type. The properties in GetRandomStringAction will be available for InvertStringAction. Redirect result type. Redirects the request to another action after being executed. Velocity result type. <struts> <package name="default" extends= struts-default"> <action name= getrandomstring class= no.uio...getrandomstringaction > <result name= success type= chain >invertstring</result> <result name= input type= redirect >error.action</result> </action> <action name= invertstring class= no.uio...invertstringaction > <result name= success type= velocity >word.vm</result> </action> </package> </struts> Generates a HTML response based on a Velocity template.

IoC Struts 2 integrates with the Spring IoC container The class property in action mappings refers to Spring bean identifiers instead of classes <struts> <! Include file and package omitted --> struts.xml Struts 2 configuration file <action name= getrandomstring class= getrandomstringaction > <result name= success type= chain >invertstring</result> </action> </struts> beans.xml Spring configuration file <bean id= getrandomstringaction class= no.uio.inf5750.example.action.getrandomstringaction />

Velocity Velocity is a template language Template: basis for documents with similar structure Template language: format defining where variables should be replaced in a document Features include: Variable replacement Simple control structures Method invocation Velocity result is included in struts-default.xml Velocity is a runtime language Fast Error prone

Velocity Variable replacement Control structures Method call <html> <head><title>$word</title></head> <body> #if ( $word == Hello ) <div style= background-color: red > #elseif ( $word == Goodbye ) <div style =background-color: blue > #else <div> #end $word.substring( 10 ) </div> Loop #foreach ( $word in $words ) <p>$word</p> #end </body> </html>

Struts 2 in DHIS 2 Web commons project (dhis-web-commons) Java code for widgets, security, portal Interceptor, result configuration Application logic interceptors Custom results Web commons resources project (dhis-web-commonsresource ) Web resources like templates, javascripts, css

Web modules in DHIS 2 Templates included in backbone template main.vm Static params in Struts 2 configuration for page and menu Must depend on dhis-web-commons and dhis-webcommons-resources Struts packages must Include dhis-web-commons.xml Extend dhis-web-commons package Have the same package name as the artifact id Have the same namespace as the artifact id Development tip: $ mvn jetty:run war Packages and deploys war file to Jetty for rapid development

Resources Brown, Davis, Stanlick: Struts 2 in Action Velocity user guide: http://velocity.apache.org/engine/devel/user-guide.html Struts home page: http://struts.apache.org Example code on course homepage