Slide 1

Slide 1 text

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

Slide 2

Slide 2 text

@ivar_grimstad https://github.com/ivargrimstad https://www.linkedin.com/in/ivargrimstad http://lanyrd.com/profile/ivargrimstad/

Slide 3

Slide 3 text

MVC

Slide 4

Slide 4 text

Why?

Slide 5

Slide 5 text

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

Slide 6

Slide 6 text

http://glassfish.org/survey

Slide 7

Slide 7 text

JSR 371 - MVC 1.0

Slide 8

Slide 8 text

JSR 371 Expert Group

Slide 9

Slide 9 text

Action-based MVC

Slide 10

Slide 10 text

Controller Model View Request Update Update Get

Slide 11

Slide 11 text

Controller Model View Request Update Update Get Component-based MVC

Slide 12

Slide 12 text

Controller Model View Request Update Update Get Action-based MVC

Slide 13

Slide 13 text

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

Slide 14

Slide 14 text

Existing Java EE Technologies

Slide 15

Slide 15 text

Key Decisions

Slide 16

Slide 16 text

Key Decisions

Slide 17

Slide 17 text

Build MVC 1.0 on top of JAX-RS

Slide 18

Slide 18 text

Controllers

Slide 19

Slide 19 text

public class HelloController { } Controller

Slide 20

Slide 20 text

@Path(“hello”) public class HelloController { } Controller

Slide 21

Slide 21 text

@Controller @Path(“hello”) public class HelloController { } Controller

Slide 22

Slide 22 text

Views

Slide 23

Slide 23 text

@Controller @Path(“hello”) public class HelloController { } View

Slide 24

Slide 24 text

@Controller @Path(“hello”) public class HelloController { @GET public String hello() { return “hello.jsp”; } } View

Slide 25

Slide 25 text

@Controller @Path(“hello”) public class HelloController { @GET public Viewable hello() { return new Viewable(“hello.jsp”); } } View

Slide 26

Slide 26 text

@Controller @Path(“hello”) public class HelloController { @GET public Response hello() { return Response.status(OK).entity(“hello.jsp”).build(); } } View

Slide 27

Slide 27 text

@Controller @Path(“hello”) public class HelloController { @View(“hello.jsp”) @GET public void hello() { } } View

Slide 28

Slide 28 text

@View(“hello.jsp”) @Controller @Path(“hello”) public class HelloController { @GET public void hello() { } } View

Slide 29

Slide 29 text

Models

Slide 30

Slide 30 text

@Named(“greeting”) @RequestScoped public class Greeting { private String message; public void setMessage(String message) { this.message = message; } public void getMessage() { return message; } } Model

Slide 31

Slide 31 text

@View(“hello.jsp”) @Controller @Path(“hello”) public class HelloController { @GET public void hello() { } } Model

Slide 32

Slide 32 text

@View(“hello.jsp”) @Controller @Path(“hello”) public class HelloController { @Inject private Greeting greeting; @GET public void hello() { greeting.setMessage(“Hello GIDS 2016!”); } } Model

Slide 33

Slide 33 text

<%@page contentType=“text/html" pageEncoding="UTF-8"%> MVC 1.0 Hello Demo

Hello ${greeting.message}

Model

Slide 34

Slide 34 text

@View(“hello.jsp”) @Controller @Path(“hello”) public class HelloController { @GET public void hello() { } } Model

Slide 35

Slide 35 text

@View(“hello.jsp”) @Controller @Path(“hello”) public class HelloController { @Inject private Models model; @GET public void hello() { model.put(“message”, “Hello GIDS 2016!”); } } Model

Slide 36

Slide 36 text

<%@page contentType=“text/html" pageEncoding="UTF-8"%> MVC 1.0 Hello Demo

Hello ${message}

Model

Slide 37

Slide 37 text

Validation

Slide 38

Slide 38 text

@Controller @Path("form") public class FormController { @POST public Response formPost(@Valid @BeanParam FormDataBean f) { return Response.status(OK).entity(“data.jsp”).build(); } } ConstraintValidationException @Min, @NotNull etc. Validation

Slide 39

Slide 39 text

public class FormViolationMapper implements ExceptionMapper { public Response toResponse(ConstraintViolationException e) { // process violations … return Response.status(BAD_REQUEST) .entity(“error.jsp”).build(); } } Validation

Slide 40

Slide 40 text

@Controller @Path("form") public class FormController { @Inject private BindingResult br; @POST public Response formPost(@Valid @BeanParam FormDataBean f) { if (br.isFailed()) { return Response.status(BAD_REQUEST) .entity(“error.jsp”).build(); } return Response.status(OK).entity(“data.jsp”).build(); } } Allows for MVC Error Handling Validation

Slide 41

Slide 41 text

Security

Slide 42

Slide 42 text

@Controller @Path(“csrf”) public class HelloController { @CsrfValid @POST public Response post(@FormParam(“greeting") String greet { } } Cross Site Request Forgery

Slide 43

Slide 43 text

<%@page contentType=“text/html" pageEncoding="UTF-8"%> MVC 1.0 Hello Demo Cross Site Request Forgery

Slide 44

Slide 44 text

Scopes

Slide 45

Slide 45 text

@Named(“greeting”) @RedirectScoped public class Greeting { private String message; public void setMessage(String message) { this.message = message; } public void getMessage() { return message; } } @RedirectScoped

Slide 46

Slide 46 text

@Controller public class HelloController { @Inject private Greeting greeting; @POST @Path(“from”) public String hello() { greeting.setMessage(“Hello GIDS 2016!”); return “redirect:to“; } } @RedirectScoped @RequestScoped @RedirectScoped

Slide 47

Slide 47 text

@Controller public class HelloController { @Inject private Greeting greeting; @POST @Path(“to”) public String hello() { return “here.jsp“; } } Same Bean @RedirectScoped

Slide 48

Slide 48 text

Tired of slides?

Slide 49

Slide 49 text

Show me the real CODE !

Slide 50

Slide 50 text

Model ReservationCtl Update confirmation reservation OK Browser POST

Slide 51

Slide 51 text

Model ReservationCtl Access Update ConfirmationC tl GET confirmation reservation OK Redirect Browser POST

Slide 52

Slide 52 text

View Engines

Slide 53

Slide 53 text

Bring Your Own View Engine

Slide 54

Slide 54 text

public interface ViewEngine { boolean supports(String view); void processView(ViewEngineContext context) throws ViewEngineException; } ViewEngine

Slide 55

Slide 55 text

@Priority(OUTRAGEOSLY_HIGH)
 @ApplicationScoped
 public class FreemarkerViewEngine extends ViewEngineBase { … } ViewEngine

Slide 56

Slide 56 text

Events

Slide 57

Slide 57 text

BeforeControllerEvent AfterControllerEvent BeforeProcessViewEvent AfterProcessViewEvent ControllerRedirectEvent

Slide 58

Slide 58 text

@ApplicationScoped public class EventObserver { void onBeforeController(@Observes BeforeControllerEvent e) { } void onAfterController(@Observes AfterControllerEvent e) { } } Events Example

Slide 59

Slide 59 text

Tool Support

Slide 60

Slide 60 text

JPA Modeler http://jpamodeler.github.io/ Gaurav Gupta @jGauravGupta

Slide 61

Slide 61 text

Summary

Slide 62

Slide 62 text

Action-based MVC

Slide 63

Slide 63 text

Existing Java EE Technologies

Slide 64

Slide 64 text

Build MVC 1.0 on top of JAX-RS

Slide 65

Slide 65 text

Project Page https://java.net/projects/mvc-spec GitHub https://github.com/mvc-spec Reference Implementation https://ozark.java.net

Slide 66

Slide 66 text

Samples https://github.com/ivargrimstad/mvc-samples Blog http://www.agilejava.eu/

Slide 67

Slide 67 text

@ivar_grimstad @greatindiandev #GIDS2016 cybercom.com