MVC 1.0 - by Exampledevelopermarch.com/developersummit/2016/report/... · Spring MVC around since...

67
MVC 1.0 - by Example Ivar Grimstad Principal Consultant, Cybercom Sweden JCP Expert Group Member (JSRs 368, 371, 375)

Transcript of MVC 1.0 - by Exampledevelopermarch.com/developersummit/2016/report/... · Spring MVC around since...

Page 1: MVC 1.0 - by Exampledevelopermarch.com/developersummit/2016/report/... · Spring MVC around since 2005 “Real World” since 2008 (Spring 2.5 onward) today’s leader in Action-Land

MVC 1.0 - by Example

Ivar Grimstad Principal Consultant, Cybercom Sweden

JCP Expert Group Member (JSRs 368, 371, 375)

Page 2: MVC 1.0 - by Exampledevelopermarch.com/developersummit/2016/report/... · Spring MVC around since 2005 “Real World” since 2008 (Spring 2.5 onward) today’s leader in Action-Land

@ivar_grimstad

https://github.com/ivargrimstad

https://www.linkedin.com/in/ivargrimstad

http://lanyrd.com/profile/ivargrimstad/

Page 3: MVC 1.0 - by Exampledevelopermarch.com/developersummit/2016/report/... · Spring MVC around since 2005 “Real World” since 2008 (Spring 2.5 onward) today’s leader in Action-Land

MVC

Page 4: MVC 1.0 - by Exampledevelopermarch.com/developersummit/2016/report/... · Spring MVC around since 2005 “Real World” since 2008 (Spring 2.5 onward) today’s leader in Action-Land

Why?

Page 5: MVC 1.0 - by Exampledevelopermarch.com/developersummit/2016/report/... · Spring MVC around since 2005 “Real World” since 2008 (Spring 2.5 onward) today’s leader in Action-Land

https://java.net/downloads/javaee-spec/JavaEE8_Community_Survey_Results.pdf

Page 6: MVC 1.0 - by Exampledevelopermarch.com/developersummit/2016/report/... · Spring MVC around since 2005 “Real World” since 2008 (Spring 2.5 onward) today’s leader in Action-Land

http://glassfish.org/survey

Page 7: MVC 1.0 - by Exampledevelopermarch.com/developersummit/2016/report/... · Spring MVC around since 2005 “Real World” since 2008 (Spring 2.5 onward) today’s leader in Action-Land

JSR 371 - MVC 1.0

Page 8: MVC 1.0 - by Exampledevelopermarch.com/developersummit/2016/report/... · Spring MVC around since 2005 “Real World” since 2008 (Spring 2.5 onward) today’s leader in Action-Land

JSR 371 Expert Group

Page 9: MVC 1.0 - by Exampledevelopermarch.com/developersummit/2016/report/... · Spring MVC around since 2005 “Real World” since 2008 (Spring 2.5 onward) today’s leader in Action-Land

Action-based MVC

Page 10: MVC 1.0 - by Exampledevelopermarch.com/developersummit/2016/report/... · Spring MVC around since 2005 “Real World” since 2008 (Spring 2.5 onward) today’s leader in Action-Land

Controller

Model View

Request

UpdateUpdate

Get

Page 11: MVC 1.0 - by Exampledevelopermarch.com/developersummit/2016/report/... · Spring MVC around since 2005 “Real World” since 2008 (Spring 2.5 onward) today’s leader in Action-Land

Controller

Model View

Request

UpdateUpdate

Get

Component-based MVC

Page 12: MVC 1.0 - by Exampledevelopermarch.com/developersummit/2016/report/... · Spring MVC around since 2005 “Real World” since 2008 (Spring 2.5 onward) today’s leader in Action-Land

Controller

Model View

Request

UpdateUpdate

Get

Action-based MVC

Page 13: MVC 1.0 - by Exampledevelopermarch.com/developersummit/2016/report/... · Spring MVC around since 2005 “Real World” since 2008 (Spring 2.5 onward) today’s leader in Action-Land

Comparable Frameworks

Spring MVC

around since 2005

“Real World” since 2008 (Spring 2.5 onward)

today’s leader in Action-Land

Struts 2

around since 2004 (WebWork 2)

“Real World” since 2007 (Struts 2.0.9 onward)

still widely adopted

Page 14: MVC 1.0 - by Exampledevelopermarch.com/developersummit/2016/report/... · Spring MVC around since 2005 “Real World” since 2008 (Spring 2.5 onward) today’s leader in Action-Land

Existing Java EE Technologies

Page 15: MVC 1.0 - by Exampledevelopermarch.com/developersummit/2016/report/... · Spring MVC around since 2005 “Real World” since 2008 (Spring 2.5 onward) today’s leader in Action-Land

Key Decisions

Page 16: MVC 1.0 - by Exampledevelopermarch.com/developersummit/2016/report/... · Spring MVC around since 2005 “Real World” since 2008 (Spring 2.5 onward) today’s leader in Action-Land

Key Decisions

Page 17: MVC 1.0 - by Exampledevelopermarch.com/developersummit/2016/report/... · Spring MVC around since 2005 “Real World” since 2008 (Spring 2.5 onward) today’s leader in Action-Land

Build MVC 1.0 on top of JAX-RS

Page 18: MVC 1.0 - by Exampledevelopermarch.com/developersummit/2016/report/... · Spring MVC around since 2005 “Real World” since 2008 (Spring 2.5 onward) today’s leader in Action-Land

Controllers

Page 19: MVC 1.0 - by Exampledevelopermarch.com/developersummit/2016/report/... · Spring MVC around since 2005 “Real World” since 2008 (Spring 2.5 onward) today’s leader in Action-Land

publicclassHelloController{

}

Controller

Page 20: MVC 1.0 - by Exampledevelopermarch.com/developersummit/2016/report/... · Spring MVC around since 2005 “Real World” since 2008 (Spring 2.5 onward) today’s leader in Action-Land

@Path(“hello”)publicclassHelloController{

}

Controller

Page 21: MVC 1.0 - by Exampledevelopermarch.com/developersummit/2016/report/... · Spring MVC around since 2005 “Real World” since 2008 (Spring 2.5 onward) today’s leader in Action-Land

@Controller@Path(“hello”)publicclassHelloController{

}

Controller

Page 22: MVC 1.0 - by Exampledevelopermarch.com/developersummit/2016/report/... · Spring MVC around since 2005 “Real World” since 2008 (Spring 2.5 onward) today’s leader in Action-Land

Views

Page 23: MVC 1.0 - by Exampledevelopermarch.com/developersummit/2016/report/... · Spring MVC around since 2005 “Real World” since 2008 (Spring 2.5 onward) today’s leader in Action-Land

@Controller@Path(“hello”)publicclassHelloController{

}

View

Page 24: MVC 1.0 - by Exampledevelopermarch.com/developersummit/2016/report/... · Spring MVC around since 2005 “Real World” since 2008 (Spring 2.5 onward) today’s leader in Action-Land

@Controller@Path(“hello”)publicclassHelloController{

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

View

Page 25: MVC 1.0 - by Exampledevelopermarch.com/developersummit/2016/report/... · Spring MVC around since 2005 “Real World” since 2008 (Spring 2.5 onward) today’s leader in Action-Land

@Controller@Path(“hello”)publicclassHelloController{

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

View

Page 26: MVC 1.0 - by Exampledevelopermarch.com/developersummit/2016/report/... · Spring MVC around since 2005 “Real World” since 2008 (Spring 2.5 onward) today’s leader in Action-Land

@Controller@Path(“hello”)publicclassHelloController{

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

View

Page 27: MVC 1.0 - by Exampledevelopermarch.com/developersummit/2016/report/... · Spring MVC around since 2005 “Real World” since 2008 (Spring 2.5 onward) today’s leader in Action-Land

@Controller@Path(“hello”)publicclassHelloController{

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

View

Page 28: MVC 1.0 - by Exampledevelopermarch.com/developersummit/2016/report/... · Spring MVC around since 2005 “Real World” since 2008 (Spring 2.5 onward) today’s leader in Action-Land

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

@GETpublicvoidhello(){}}

View

Page 29: MVC 1.0 - by Exampledevelopermarch.com/developersummit/2016/report/... · Spring MVC around since 2005 “Real World” since 2008 (Spring 2.5 onward) today’s leader in Action-Land

Models

Page 30: MVC 1.0 - by Exampledevelopermarch.com/developersummit/2016/report/... · Spring MVC around since 2005 “Real World” since 2008 (Spring 2.5 onward) today’s leader in Action-Land

@Named(“greeting”)@RequestScopedpublicclassGreeting{

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

publicvoidgetMessage(){returnmessage;}}

Model

Page 31: MVC 1.0 - by Exampledevelopermarch.com/developersummit/2016/report/... · Spring MVC around since 2005 “Real World” since 2008 (Spring 2.5 onward) today’s leader in Action-Land

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

@GETpublicvoidhello(){}}

Model

Page 32: MVC 1.0 - by Exampledevelopermarch.com/developersummit/2016/report/... · Spring MVC around since 2005 “Real World” since 2008 (Spring 2.5 onward) today’s leader in Action-Land

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

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

Model

Page 33: MVC 1.0 - by Exampledevelopermarch.com/developersummit/2016/report/... · Spring MVC around since 2005 “Real World” since 2008 (Spring 2.5 onward) today’s leader in Action-Land

<%@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

Page 34: MVC 1.0 - by Exampledevelopermarch.com/developersummit/2016/report/... · Spring MVC around since 2005 “Real World” since 2008 (Spring 2.5 onward) today’s leader in Action-Land

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

@GETpublicvoidhello(){}}

Model

Page 35: MVC 1.0 - by Exampledevelopermarch.com/developersummit/2016/report/... · Spring MVC around since 2005 “Real World” since 2008 (Spring 2.5 onward) today’s leader in Action-Land

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

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

Model

Page 36: MVC 1.0 - by Exampledevelopermarch.com/developersummit/2016/report/... · Spring MVC around since 2005 “Real World” since 2008 (Spring 2.5 onward) today’s leader in Action-Land

<%@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>

Model

Page 37: MVC 1.0 - by Exampledevelopermarch.com/developersummit/2016/report/... · Spring MVC around since 2005 “Real World” since 2008 (Spring 2.5 onward) today’s leader in Action-Land

Validation

Page 38: MVC 1.0 - by Exampledevelopermarch.com/developersummit/2016/report/... · Spring MVC around since 2005 “Real World” since 2008 (Spring 2.5 onward) today’s leader in Action-Land

@Controller@Path("form")publicclassFormController{

@POSTpublicResponseformPost(@Valid@BeanParamFormDataBeanf){

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

ConstraintValidationException

@Min, @NotNulletc.

Validation

Page 39: MVC 1.0 - by Exampledevelopermarch.com/developersummit/2016/report/... · Spring MVC around since 2005 “Real World” since 2008 (Spring 2.5 onward) today’s leader in Action-Land

publicclassFormViolationMapperimplementsExceptionMapper<ConstraintViolationException>{

publicResponsetoResponse(ConstraintViolationExceptione){

//processviolations…

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

Validation

Page 40: MVC 1.0 - by Exampledevelopermarch.com/developersummit/2016/report/... · Spring MVC around since 2005 “Real World” since 2008 (Spring 2.5 onward) today’s leader in Action-Land

@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();}}

Allows for MVC Error Handling

Validation

Page 41: MVC 1.0 - by Exampledevelopermarch.com/developersummit/2016/report/... · Spring MVC around since 2005 “Real World” since 2008 (Spring 2.5 onward) today’s leader in Action-Land

Security

Page 42: MVC 1.0 - by Exampledevelopermarch.com/developersummit/2016/report/... · Spring MVC around since 2005 “Real World” since 2008 (Spring 2.5 onward) today’s leader in Action-Land

@Controller@Path(“csrf”)publicclassHelloController{

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

Cross Site Request Forgery

Page 43: MVC 1.0 - by Exampledevelopermarch.com/developersummit/2016/report/... · Spring MVC around since 2005 “Real World” since 2008 (Spring 2.5 onward) today’s leader in Action-Land

<%@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>

Cross Site Request Forgery

Page 44: MVC 1.0 - by Exampledevelopermarch.com/developersummit/2016/report/... · Spring MVC around since 2005 “Real World” since 2008 (Spring 2.5 onward) today’s leader in Action-Land

Scopes

Page 45: MVC 1.0 - by Exampledevelopermarch.com/developersummit/2016/report/... · Spring MVC around since 2005 “Real World” since 2008 (Spring 2.5 onward) today’s leader in Action-Land

@Named(“greeting”)@RedirectScopedpublicclassGreeting{

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

publicvoidgetMessage(){returnmessage;}}

@RedirectScoped

Page 46: MVC 1.0 - by Exampledevelopermarch.com/developersummit/2016/report/... · Spring MVC around since 2005 “Real World” since 2008 (Spring 2.5 onward) today’s leader in Action-Land

@ControllerpublicclassHelloController{

@InjectprivateGreetinggreeting;@POST@Path(“from”)publicStringhello(){greeting.setMessage(“HelloJavaLand!”);return“redirect:to“;}}

@RedirectScoped

@RequestScoped

@RedirectScoped

Page 47: MVC 1.0 - by Exampledevelopermarch.com/developersummit/2016/report/... · Spring MVC around since 2005 “Real World” since 2008 (Spring 2.5 onward) today’s leader in Action-Land

@ControllerpublicclassHelloController{

@InjectprivateGreetinggreeting;@POST@Path(“to”)publicStringhello(){return“here.jsp“;}}

Same Bean

@RedirectScoped

Page 48: MVC 1.0 - by Exampledevelopermarch.com/developersummit/2016/report/... · Spring MVC around since 2005 “Real World” since 2008 (Spring 2.5 onward) today’s leader in Action-Land

Tired of slides?

Page 49: MVC 1.0 - by Exampledevelopermarch.com/developersummit/2016/report/... · Spring MVC around since 2005 “Real World” since 2008 (Spring 2.5 onward) today’s leader in Action-Land

Show me the real CODE !

Page 50: MVC 1.0 - by Exampledevelopermarch.com/developersummit/2016/report/... · Spring MVC around since 2005 “Real World” since 2008 (Spring 2.5 onward) today’s leader in Action-Land

Model

ReservationCtlUpdate

confirmation

reservation

OK

Browser

POST

Page 51: MVC 1.0 - by Exampledevelopermarch.com/developersummit/2016/report/... · Spring MVC around since 2005 “Real World” since 2008 (Spring 2.5 onward) today’s leader in Action-Land

Model

ReservationCtl

Access

Update

ConfirmationCtl

GET

confirmation

reservation

OK

Redirect

Browser

POST

Page 52: MVC 1.0 - by Exampledevelopermarch.com/developersummit/2016/report/... · Spring MVC around since 2005 “Real World” since 2008 (Spring 2.5 onward) today’s leader in Action-Land

View Engines

Page 53: MVC 1.0 - by Exampledevelopermarch.com/developersummit/2016/report/... · Spring MVC around since 2005 “Real World” since 2008 (Spring 2.5 onward) today’s leader in Action-Land

Bring Your Own View Engine

Page 54: MVC 1.0 - by Exampledevelopermarch.com/developersummit/2016/report/... · Spring MVC around since 2005 “Real World” since 2008 (Spring 2.5 onward) today’s leader in Action-Land

publicinterfaceViewEngine{

booleansupports(Stringview);

voidprocessView(ViewEngineContextcontext)throwsViewEngineException;}

ViewEngine

Page 55: MVC 1.0 - by Exampledevelopermarch.com/developersummit/2016/report/... · Spring MVC around since 2005 “Real World” since 2008 (Spring 2.5 onward) today’s leader in Action-Land

@Priority(OUTRAGEOSLY_HIGH)@ApplicationScopedpublicclassFreemarkerViewEngineextendsViewEngineBase{…

}

ViewEngine

Page 56: MVC 1.0 - by Exampledevelopermarch.com/developersummit/2016/report/... · Spring MVC around since 2005 “Real World” since 2008 (Spring 2.5 onward) today’s leader in Action-Land

Events

Page 57: MVC 1.0 - by Exampledevelopermarch.com/developersummit/2016/report/... · Spring MVC around since 2005 “Real World” since 2008 (Spring 2.5 onward) today’s leader in Action-Land

BeforeControllerEventAfterControllerEvent

BeforeProcessViewEventAfterProcessViewEvent

ControllerRedirectEvent

Page 58: MVC 1.0 - by Exampledevelopermarch.com/developersummit/2016/report/... · Spring MVC around since 2005 “Real World” since 2008 (Spring 2.5 onward) today’s leader in Action-Land

@ApplicationScopedpublicclassEventObserver{

voidonBeforeController(@ObservesBeforeControllerEvente){}voidonAfterController(@ObservesAfterControllerEvente){}}

Events Example

Page 59: MVC 1.0 - by Exampledevelopermarch.com/developersummit/2016/report/... · Spring MVC around since 2005 “Real World” since 2008 (Spring 2.5 onward) today’s leader in Action-Land

Tool Support

Page 60: MVC 1.0 - by Exampledevelopermarch.com/developersummit/2016/report/... · Spring MVC around since 2005 “Real World” since 2008 (Spring 2.5 onward) today’s leader in Action-Land

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

Gaurav Gupta @jGauravGupta

Page 61: MVC 1.0 - by Exampledevelopermarch.com/developersummit/2016/report/... · Spring MVC around since 2005 “Real World” since 2008 (Spring 2.5 onward) today’s leader in Action-Land

Summary

Page 62: MVC 1.0 - by Exampledevelopermarch.com/developersummit/2016/report/... · Spring MVC around since 2005 “Real World” since 2008 (Spring 2.5 onward) today’s leader in Action-Land

Action-based MVC

Page 63: MVC 1.0 - by Exampledevelopermarch.com/developersummit/2016/report/... · Spring MVC around since 2005 “Real World” since 2008 (Spring 2.5 onward) today’s leader in Action-Land

Existing Java EE Technologies

Page 64: MVC 1.0 - by Exampledevelopermarch.com/developersummit/2016/report/... · Spring MVC around since 2005 “Real World” since 2008 (Spring 2.5 onward) today’s leader in Action-Land

Build MVC 1.0 on top of JAX-RS

Page 65: MVC 1.0 - by Exampledevelopermarch.com/developersummit/2016/report/... · Spring MVC around since 2005 “Real World” since 2008 (Spring 2.5 onward) today’s leader in Action-Land

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

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

Reference Implementation https://ozark.java.net

Page 66: MVC 1.0 - by Exampledevelopermarch.com/developersummit/2016/report/... · Spring MVC around since 2005 “Real World” since 2008 (Spring 2.5 onward) today’s leader in Action-Land

Samples https://github.com/ivargrimstad/mvc-samples

Blog http://www.agilejava.eu/

Page 67: MVC 1.0 - by Exampledevelopermarch.com/developersummit/2016/report/... · Spring MVC around since 2005 “Real World” since 2008 (Spring 2.5 onward) today’s leader in Action-Land

@ivar_grimstad@greatindiandev #GIDS2016

cybercom.com