51
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

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

Embed Size (px)

Citation preview

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

@ivar_grimstad @jGauravGupta @javajuneau

Introduction to MVC 1.0 JPA Modeler What about JSF?

Introduction to MVC 1.0

Action-based MVC

Controller

Model View

Request

UpdateUpdate

Controller

Model View

Request

UpdateUpdate

Controller

Model View

Request

UpdateUpdate

Existing Java EE Technologies

Build MVC 1.0 on top of JAX-RS

Controllers

Controller

publicclassHelloController{

}

Controller

@Path(“hello”)publicclassHelloController{

}

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

}

Views

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

}

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

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

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

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

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

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

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

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

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

@GETpublicvoidhello(){}}

Models

Model@Named(“greeting”)@RequestScopedpublicclassGreeting{

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

publicvoidgetMessage(){returnmessage;}}

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

@GETpublicvoidhello(){}}

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

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

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>

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

@GETpublicvoidhello(){}}

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

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

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>

Validation

@Controller@Path("form")publicclassFormController{

@POSTpublicResponseformPost(@Valid@BeanParamFormDataBeanf){

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

Validation

ConstraintValidationException

@Min, @NotNulletc.

ValidationpublicclassFormViolationMapperimplementsExceptionMapper<ConstraintViolationException>{

publicResponsetoResponse(ConstraintViolationExceptione){

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

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

@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

Security

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

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

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>

DEMO

JPA Modeler

JPA Modeler

• Entity and Database mapping tool

• Java EE 8 source code generator

• Wires the technology together

DB Modeler

• Design schema transparently

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

• Generate SQL

• ORM knowledge not required

▪ 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

Let’s build an app

MVC Plugin for NetBeans

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

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

What About JSF?

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.)

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

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

Summary

@ivar_grimstadUGF6435 - JavaOne 2016

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

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

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