Top Banner
Enterprise Modeling of MVC and Java EE Artifacts Ivar Grimstad Principal Consultant, Cybercom Sweden Gaurav Gupta JPA Modeler creator and lead developer Josh Juneau Applications Developer and Systems Analyst, Fermi National Accelerator Laboratory
51

Enterprise Modeling of MVC and Java EE Artifacts · Enterprise Modeling of MVC and Java EE Artifacts ... Let’s build an app. MVC Plugin for NetBeans. Using the MVC Plugin

Apr 02, 2018

Download

Documents

truongdieu
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Enterprise Modeling of MVC and Java EE Artifacts · Enterprise Modeling of MVC and Java EE Artifacts ... Let’s build an app. MVC Plugin for NetBeans. Using the MVC Plugin

Enterprise Modeling of MVC and Java EE Artifacts

Ivar Grimstad Principal Consultant, Cybercom Sweden Gaurav Gupta JPA Modeler creator and lead developer Josh Juneau Applications Developer and Systems Analyst, Fermi National Accelerator Laboratory

Page 2: Enterprise Modeling of MVC and Java EE Artifacts · Enterprise Modeling of MVC and Java EE Artifacts ... Let’s build an app. MVC Plugin for NetBeans. Using the MVC Plugin

@ivar_grimstad @jGauravGupta @javajuneau

Page 3: Enterprise Modeling of MVC and Java EE Artifacts · Enterprise Modeling of MVC and Java EE Artifacts ... Let’s build an app. MVC Plugin for NetBeans. Using the MVC Plugin

Introduction to MVC 1.0 JPA Modeler What about JSF?

Page 4: Enterprise Modeling of MVC and Java EE Artifacts · Enterprise Modeling of MVC and Java EE Artifacts ... Let’s build an app. MVC Plugin for NetBeans. Using the MVC Plugin

Introduction to MVC 1.0

Page 5: Enterprise Modeling of MVC and Java EE Artifacts · Enterprise Modeling of MVC and Java EE Artifacts ... Let’s build an app. MVC Plugin for NetBeans. Using the MVC Plugin

Action-based MVC

Page 6: Enterprise Modeling of MVC and Java EE Artifacts · Enterprise Modeling of MVC and Java EE Artifacts ... Let’s build an app. MVC Plugin for NetBeans. Using the MVC Plugin

Controller

Model View

Request

UpdateUpdate

Page 7: Enterprise Modeling of MVC and Java EE Artifacts · Enterprise Modeling of MVC and Java EE Artifacts ... Let’s build an app. MVC Plugin for NetBeans. Using the MVC Plugin

Controller

Model View

Request

UpdateUpdate

Page 8: Enterprise Modeling of MVC and Java EE Artifacts · Enterprise Modeling of MVC and Java EE Artifacts ... Let’s build an app. MVC Plugin for NetBeans. Using the MVC Plugin

Controller

Model View

Request

UpdateUpdate

Page 9: Enterprise Modeling of MVC and Java EE Artifacts · Enterprise Modeling of MVC and Java EE Artifacts ... Let’s build an app. MVC Plugin for NetBeans. Using the MVC Plugin

Existing Java EE Technologies

Page 10: Enterprise Modeling of MVC and Java EE Artifacts · Enterprise Modeling of MVC and Java EE Artifacts ... Let’s build an app. MVC Plugin for NetBeans. Using the MVC Plugin

Build MVC 1.0 on top of JAX-RS

Page 11: Enterprise Modeling of MVC and Java EE Artifacts · Enterprise Modeling of MVC and Java EE Artifacts ... Let’s build an app. MVC Plugin for NetBeans. Using the MVC Plugin

Controllers

Page 12: Enterprise Modeling of MVC and Java EE Artifacts · Enterprise Modeling of MVC and Java EE Artifacts ... Let’s build an app. MVC Plugin for NetBeans. Using the MVC Plugin

Controller

publicclassHelloController{

}

Page 13: Enterprise Modeling of MVC and Java EE Artifacts · Enterprise Modeling of MVC and Java EE Artifacts ... Let’s build an app. MVC Plugin for NetBeans. Using the MVC Plugin

Controller

@Path(“hello”)publicclassHelloController{

}

Page 14: Enterprise Modeling of MVC and Java EE Artifacts · Enterprise Modeling of MVC and Java EE Artifacts ... Let’s build an app. MVC Plugin for NetBeans. Using the MVC Plugin

Controller@Controller@Path(“hello”)publicclassHelloController{

}

Page 15: Enterprise Modeling of MVC and Java EE Artifacts · Enterprise Modeling of MVC and Java EE Artifacts ... Let’s build an app. MVC Plugin for NetBeans. Using the MVC Plugin

Views

Page 16: Enterprise Modeling of MVC and Java EE Artifacts · Enterprise Modeling of MVC and Java EE Artifacts ... Let’s build an app. MVC Plugin for NetBeans. Using the MVC Plugin

View@Controller@Path(“hello”)publicclassHelloController{

}

Page 17: Enterprise Modeling of MVC and Java EE Artifacts · Enterprise Modeling of MVC and Java EE Artifacts ... Let’s build an app. MVC Plugin for NetBeans. Using the MVC Plugin

View@Controller@Path(“hello”)publicclassHelloController{

@GETpublicStringhello(){return“hello.jsp”;}}

Page 18: Enterprise Modeling of MVC and Java EE Artifacts · Enterprise Modeling of MVC and Java EE Artifacts ... Let’s build an app. MVC Plugin for NetBeans. Using the MVC Plugin

View@Controller@Path(“hello”)publicclassHelloController{

@GETpublicViewablehello(){returnnewViewable(“hello.jsp”);}}

Page 19: Enterprise Modeling of MVC and Java EE Artifacts · Enterprise Modeling of MVC and Java EE Artifacts ... Let’s build an app. MVC Plugin for NetBeans. Using the MVC Plugin

View@Controller@Path(“hello”)publicclassHelloController{

@GETpublicResponsehello(){returnResponse.status(OK).entity(“hello.jsp”).build();}}

Page 20: Enterprise Modeling of MVC and Java EE Artifacts · Enterprise Modeling of MVC and Java EE Artifacts ... Let’s build an app. MVC Plugin for NetBeans. Using the MVC Plugin

View@Controller@Path(“hello”)publicclassHelloController{

@View(“hello.jsp”)@GETpublicvoidhello(){}}

Page 21: Enterprise Modeling of MVC and Java EE Artifacts · Enterprise Modeling of MVC and Java EE Artifacts ... Let’s build an app. MVC Plugin for NetBeans. Using the MVC Plugin

View@View(“hello.jsp”)@Controller@Path(“hello”)publicclassHelloController{

@GETpublicvoidhello(){}}

Page 22: Enterprise Modeling of MVC and Java EE Artifacts · Enterprise Modeling of MVC and Java EE Artifacts ... Let’s build an app. MVC Plugin for NetBeans. Using the MVC Plugin

Models

Page 23: Enterprise Modeling of MVC and Java EE Artifacts · Enterprise Modeling of MVC and Java EE Artifacts ... Let’s build an app. MVC Plugin for NetBeans. Using the MVC Plugin

Model@Named(“greeting”)@RequestScopedpublicclassGreeting{

privateStringmessage;publicvoidsetMessage(Stringmessage){this.message=message;}

publicvoidgetMessage(){returnmessage;}}

Page 24: Enterprise Modeling of MVC and Java EE Artifacts · Enterprise Modeling of MVC and Java EE Artifacts ... Let’s build an app. MVC Plugin for NetBeans. Using the MVC Plugin

Model@View(“hello.jsp”)@Controller@Path(“hello”)publicclassHelloController{

@GETpublicvoidhello(){}}

Page 25: Enterprise Modeling of MVC and Java EE Artifacts · Enterprise Modeling of MVC and Java EE Artifacts ... Let’s build an app. MVC Plugin for NetBeans. Using the MVC Plugin

Model@View(“hello.jsp”)@Controller@Path(“hello”)publicclassHelloController{

@InjectprivateGreetinggreeting;@GETpublicvoidhello(){greeting.setMessage(“HelloJavaOne!”);}}

Page 26: Enterprise Modeling of MVC and Java EE Artifacts · Enterprise Modeling of MVC and Java EE Artifacts ... Let’s build an app. MVC Plugin for NetBeans. Using the MVC Plugin

Model<%@page contentType=“text/html" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <title>MVC 1.0 Hello Demo</title> </head> <body> <h1>Hello ${greeting.message}</h1> </body> </html>

Page 27: Enterprise Modeling of MVC and Java EE Artifacts · Enterprise Modeling of MVC and Java EE Artifacts ... Let’s build an app. MVC Plugin for NetBeans. Using the MVC Plugin

Model@View(“hello.jsp”)@Controller@Path(“hello”)publicclassHelloController{

@GETpublicvoidhello(){}}

Page 28: Enterprise Modeling of MVC and Java EE Artifacts · Enterprise Modeling of MVC and Java EE Artifacts ... Let’s build an app. MVC Plugin for NetBeans. Using the MVC Plugin

Model@View(“hello.jsp”)@Controller@Path(“hello”)publicclassHelloController{

@InjectprivateModelsmodel;@GETpublicvoidhello(){model.put(“message”,“HelloJavOne!”);}}

Page 29: Enterprise Modeling of MVC and Java EE Artifacts · Enterprise Modeling of MVC and Java EE Artifacts ... Let’s build an app. MVC Plugin for NetBeans. Using the MVC Plugin

Model<%@page contentType=“text/html" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <title>MVC 1.0 Hello Demo</title> </head> <body> <h1>Hello ${message}</h1> </body> </html>

Page 30: Enterprise Modeling of MVC and Java EE Artifacts · Enterprise Modeling of MVC and Java EE Artifacts ... Let’s build an app. MVC Plugin for NetBeans. Using the MVC Plugin

Validation

Page 31: Enterprise Modeling of MVC and Java EE Artifacts · Enterprise Modeling of MVC and Java EE Artifacts ... Let’s build an app. MVC Plugin for NetBeans. Using the MVC Plugin

@Controller@Path("form")publicclassFormController{

@POSTpublicResponseformPost(@Valid@BeanParamFormDataBeanf){

returnResponse.status(OK).entity(“data.jsp”).build();}}

Validation

ConstraintValidationException

@Min, @NotNulletc.

Page 32: Enterprise Modeling of MVC and Java EE Artifacts · Enterprise Modeling of MVC and Java EE Artifacts ... Let’s build an app. MVC Plugin for NetBeans. Using the MVC Plugin

ValidationpublicclassFormViolationMapperimplementsExceptionMapper<ConstraintViolationException>{

publicResponsetoResponse(ConstraintViolationExceptione){

Set<ConstraintViolation<?>>s=e.getConstraintViolations();

//processviolations...returnResponse.status(BAD_REQUEST).entity(“error.jsp”).build();}}

Page 33: Enterprise Modeling of MVC and Java EE Artifacts · Enterprise Modeling of MVC and Java EE Artifacts ... Let’s build an app. MVC Plugin for NetBeans. Using the MVC Plugin

@Controller@Path("form")publicclassFormController{@InjectprivateBindingResultbr;

@POSTpublicResponseformPost(@Valid@BeanParamFormDataBeanf){if(br.isFailed()){returnResponse.status(BAD_REQUEST).entity(“error.jsp”).build();}returnResponse.status(OK).entity(“data.jsp”).build();}}

ValidationAllows for MVC Error Handling

Page 34: Enterprise Modeling of MVC and Java EE Artifacts · Enterprise Modeling of MVC and Java EE Artifacts ... Let’s build an app. MVC Plugin for NetBeans. Using the MVC Plugin

Security

Page 35: Enterprise Modeling of MVC and Java EE Artifacts · Enterprise Modeling of MVC and Java EE Artifacts ... Let’s build an app. MVC Plugin for NetBeans. Using the MVC Plugin

Cross Site Request Forgery@Controller@Path(“csrf”)publicclassHelloController{

@CsrfValid@POSTpublicResponsepost(@FormParam(“greeting")Stringgreet{}}

Page 36: Enterprise Modeling of MVC and Java EE Artifacts · Enterprise Modeling of MVC and Java EE Artifacts ... Let’s build an app. MVC Plugin for NetBeans. Using the MVC Plugin

Cross Site Request Forgery<%@page contentType=“text/html" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <title>MVC 1.0 Hello Demo</title> </head> <body> <form action="csrf" method="post" accept-charset="utf-8"> <input type="submit" value="Click here"/> <input type="hidden" name="${mvc.csrf.name}" value="${mvc.csrf.token}"/> </form> </body> </html>

Page 37: Enterprise Modeling of MVC and Java EE Artifacts · Enterprise Modeling of MVC and Java EE Artifacts ... Let’s build an app. MVC Plugin for NetBeans. Using the MVC Plugin

DEMO

Page 38: Enterprise Modeling of MVC and Java EE Artifacts · Enterprise Modeling of MVC and Java EE Artifacts ... Let’s build an app. MVC Plugin for NetBeans. Using the MVC Plugin

JPA Modeler

Page 39: Enterprise Modeling of MVC and Java EE Artifacts · Enterprise Modeling of MVC and Java EE Artifacts ... Let’s build an app. MVC Plugin for NetBeans. Using the MVC Plugin

JPA Modeler

• Entity and Database mapping tool

• Java EE 8 source code generator

• Wires the technology together

Page 40: Enterprise Modeling of MVC and Java EE Artifacts · Enterprise Modeling of MVC and Java EE Artifacts ... Let’s build an app. MVC Plugin for NetBeans. Using the MVC Plugin

DB Modeler

• Design schema transparently

• Multi Database support (e.g PostgreSQL, MySQL, Oracle etc)

• Generate SQL

• ORM knowledge not required

Page 41: Enterprise Modeling of MVC and Java EE Artifacts · Enterprise Modeling of MVC and Java EE Artifacts ... Let’s build an app. MVC Plugin for NetBeans. Using the MVC Plugin

▪ MVC 1.0 (JSR - 371)

▪ JAX-RS (REST)

▪ Security API (JSR - 375)

▪ CDI

▪ Bean Validation

▪ EJB

▪ JSP

▪ Swagger

▪ Metrics

▪ Angular JS

▪ Responsive Web Design

▪ HTML5 Boilerplate

▪ Twitter Bootstrap

Technology Stack

Page 42: Enterprise Modeling of MVC and Java EE Artifacts · Enterprise Modeling of MVC and Java EE Artifacts ... Let’s build an app. MVC Plugin for NetBeans. Using the MVC Plugin

Let’s build an app

Page 43: Enterprise Modeling of MVC and Java EE Artifacts · Enterprise Modeling of MVC and Java EE Artifacts ... Let’s build an app. MVC Plugin for NetBeans. Using the MVC Plugin

MVC Plugin for NetBeans

Page 44: Enterprise Modeling of MVC and Java EE Artifacts · Enterprise Modeling of MVC and Java EE Artifacts ... Let’s build an app. MVC Plugin for NetBeans. Using the MVC Plugin

Using the MVC Plugin

• Obtain and install the MVC Plugin

• Generate project with model and controllers

• View and navigate to MVC Controllers

• Create New Controller Class

Page 45: Enterprise Modeling of MVC and Java EE Artifacts · Enterprise Modeling of MVC and Java EE Artifacts ... Let’s build an app. MVC Plugin for NetBeans. Using the MVC Plugin

Plugin – In Progress• Work is in progress for the MVC plugin • Ability to create new MVC application that will include simple

controller method and view • Easy navigation from controller to view

• Send comments/feedback/suggestions my way

• Sources Available: • https://github.com/juneau001/BasicMVCSupport

Page 46: Enterprise Modeling of MVC and Java EE Artifacts · Enterprise Modeling of MVC and Java EE Artifacts ... Let’s build an app. MVC Plugin for NetBeans. Using the MVC Plugin

What About JSF?

Page 47: Enterprise Modeling of MVC and Java EE Artifacts · Enterprise Modeling of MVC and Java EE Artifacts ... Let’s build an app. MVC Plugin for NetBeans. Using the MVC Plugin

JSF Overview

• Mature Framework for Java EE

• Follows MVC Architecture

• Component-Based

• Many Libraries Available (PrimeFaces, PrettyFaces, MyFaces, etc.)…get up and running quickly with so many components available

• Utility Libraries also (PrimeFaces, OmniFaces, etc.)

Page 48: Enterprise Modeling of MVC and Java EE Artifacts · Enterprise Modeling of MVC and Java EE Artifacts ... Let’s build an app. MVC Plugin for NetBeans. Using the MVC Plugin

JSF in NetBeans IDE

• Create Maven Web Project

• Generate data model (entity classes from database)

• Generate JPA Diagram from entity classes • Can also generate database tables and entity classes with JPA

Modeler • Generate controllers and views

Page 49: Enterprise Modeling of MVC and Java EE Artifacts · Enterprise Modeling of MVC and Java EE Artifacts ... Let’s build an app. MVC Plugin for NetBeans. Using the MVC Plugin

MVC & JSFDifferent Use-Cases for Different Solutions

JSF • Component Based • Controller Logic• Automates Processing• Facelets• Rapid Development• Works well with REST• Stateful

MVC • Action-Based • Layered on top of JAX-RS• Manual Processing• Various View Options• Fine-grained Control• Great fit for REST • No State…Request-

based

Page 50: Enterprise Modeling of MVC and Java EE Artifacts · Enterprise Modeling of MVC and Java EE Artifacts ... Let’s build an app. MVC Plugin for NetBeans. Using the MVC Plugin

Summary

Page 51: Enterprise Modeling of MVC and Java EE Artifacts · Enterprise Modeling of MVC and Java EE Artifacts ... Let’s build an app. MVC Plugin for NetBeans. Using the MVC Plugin

@ivar_grimstadUGF6435 - JavaOne 2016

Project Page https://java.net/projects/mvc-spec

GitHub https://github.com/mvc-spec

JPA Modeler http://jpamodeler.github.io/