Upgrade to Pro — share decks privately, control downloads, hide ads and more …

MVC 1.0 - by Example @ Devoxx Morocco 2015

ivargrimstad
November 17, 2015

MVC 1.0 - by Example @ Devoxx Morocco 2015

MVC 1.0, as specified by JSR 371, is targeted for the upcoming Java EE 8 release.
In this session I will go through the fundamentals of this specification and explain the core concepts.

The session will include lots of code samples and tips to where to get started using this awesome technology.

ivargrimstad

November 17, 2015
Tweet

More Decks by ivargrimstad

Other Decks in Programming

Transcript

  1. #DevoxxMA #JSR371 @ivar_grimstad
    MVC 1.0 - by Example
    Ivar Grimstad
    Principal Consultant, Cybercom Sweden
    JCP Expert Group Member JSRs 368, 371, 375

    View Slide

  2. #DevoxxMA #JSR371 @ivar_grimstad
    @ivar_grimstad
    https://github.com/ivargrimstad
    https://www.linkedin.com/in/ivargrimstad
    http://lanyrd.com/profile/ivargrimstad/

    View Slide

  3. #DevoxxMA #JSR371 @ivar_grimstad
    Why?

    View Slide

  4. #DevoxxMA #JSR371 @ivar_grimstad
    https://java.net/downloads/javaee-spec/JavaEE8_Community_Survey_Results.pdf

    View Slide

  5. #DevoxxMA #JSR371 @ivar_grimstad
    http://glassfish.org/survey

    View Slide

  6. #DevoxxMA #JSR371 @ivar_grimstad
    JSR 371

    Expert Group
    JSR 371 Expert Group

    View Slide

  7. #DevoxxMA #JSR371 @ivar_grimstad
    Action-based MVC

    View Slide

  8. #DevoxxMA #JSR371 @ivar_grimstad
    Controller
    Model View
    Request
    Update
    Update

    View Slide

  9. #DevoxxMA #JSR371 @ivar_grimstad
    Controller
    Model View
    Request
    Update
    Update

    View Slide

  10. #DevoxxMA #JSR371 @ivar_grimstad
    Controller
    Model View
    Request
    Update
    Update

    View Slide

  11. #DevoxxMA #JSR371 @ivar_grimstad
    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

    View Slide

  12. #DevoxxMA #JSR371 @ivar_grimstad
    Existing Java EE Technologies

    View Slide

  13. #DevoxxMA #JSR371 @ivar_grimstad
    Key Decisions

    View Slide

  14. #DevoxxMA #JSR371 @ivar_grimstad
    Key Decisions

    View Slide

  15. #DevoxxMA #JSR371 @ivar_grimstad
    Build MVC 1.0 on top of JAX-RS

    View Slide

  16. #DevoxxMA #JSR371 @ivar_grimstad
    Controllers

    View Slide

  17. #DevoxxMA #JSR371 @ivar_grimstad
    Controller
    public class HelloController {
    }

    View Slide

  18. #DevoxxMA #JSR371 @ivar_grimstad
    Controller
    @Path(“hello”)
    public class HelloController {
    }

    View Slide

  19. #DevoxxMA #JSR371 @ivar_grimstad
    Controller
    @Controller
    @Path(“hello”)
    public class HelloController {
    }

    View Slide

  20. #DevoxxMA #JSR371 @ivar_grimstad
    Views

    View Slide

  21. #DevoxxMA #JSR371 @ivar_grimstad
    View
    @Controller
    @Path(“hello”)
    public class HelloController {
    }

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  25. #DevoxxMA #JSR371 @ivar_grimstad
    View
    @Controller
    @Path(“hello”)
    public class HelloController {
    @View(“hello.jsp”)
    @GET
    public void hello() {
    }
    }

    View Slide

  26. #DevoxxMA #JSR371 @ivar_grimstad
    View
    @View(“hello.jsp”)
    @Controller
    @Path(“hello”)
    public class HelloController {
    @GET
    public void hello() {
    }
    }

    View Slide

  27. #DevoxxMA #JSR371 @ivar_grimstad
    Models

    View Slide

  28. #DevoxxMA #JSR371 @ivar_grimstad
    Model
    @View(“hello.jsp”)
    @Controller
    @Path(“hello”)
    public class HelloController {
    @GET
    public void hello() {
    }
    }

    View Slide

  29. #DevoxxMA #JSR371 @ivar_grimstad
    Model
    @Named(“greeting”)
    @RequestScoped
    public class Greeting {
    private String message;
    public void setMessate(String message) {
    this.message = message;
    }
    public void getMessage() { return message; }
    }

    View Slide

  30. #DevoxxMA #JSR371 @ivar_grimstad
    Model
    @View(“hello.jsp”)
    @Controller
    @Path(“hello”)
    public class HelloController {
    @Inject
    private Greeting greeting;
    @GET
    public void hello() {
    greeting.setMessage(“Hello Devoxx!”);
    }
    }

    View Slide

  31. #DevoxxMA #JSR371 @ivar_grimstad
    Model
    pageEncoding="UTF-8"%>



    MVC 1.0 Hello Demo


    Hello ${greeting.message}


    View Slide

  32. #DevoxxMA #JSR371 @ivar_grimstad
    Model
    @View(“hello.jsp”)
    @Controller
    @Path(“hello”)
    public class HelloController {
    @GET
    public void hello() {
    }
    }

    View Slide

  33. #DevoxxMA #JSR371 @ivar_grimstad
    Model
    @View(“hello.jsp”)
    @Controller
    @Path(“hello”)
    public class HelloController {
    @Inject
    private Models model;
    @GET
    public void hello() {
    model.put(“message”, “Hello Devoxx!”);
    }
    }

    View Slide

  34. #DevoxxMA #JSR371 @ivar_grimstad
    Model
    pageEncoding="UTF-8"%>



    MVC 1.0 Hello Demo


    Hello ${message}


    View Slide

  35. #DevoxxMA #JSR371 @ivar_grimstad
    Validation

    View Slide

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

    View Slide

  37. #DevoxxMA #JSR371 @ivar_grimstad
    Validation
    public class FormViolationMapper implements
    ExceptionMapper {
    public Response toResponse(ConstraintViolationException e) {
    Set> s =e.getConstraintViolations();
    // process violations ...
    return Response.status(BAD_REQUEST)
    .entity(“error.jsp”).build();
    }
    }

    View Slide

  38. #DevoxxMA #JSR371 @ivar_grimstad
    @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();
    }
    }
    Validation
    Allows for MVC Error Handling

    View Slide

  39. #DevoxxMA #JSR371 @ivar_grimstad
    Show me the CODE !

    View Slide

  40. #DevoxxMA #JSR371 @ivar_grimstad
    Model
    ReservationCtl
    Update
    confirmation
    reservation
    OK
    Browser
    POST

    View Slide

  41. #DevoxxMA #JSR371 @ivar_grimstad
    Model
    ReservationCtl
    Access
    Update
    ConfirmationCtl
    GET
    confirmation
    reservation
    OK
    Redirect
    Browser
    POST

    View Slide

  42. #DevoxxMA #JSR371 @ivar_grimstad
    Scopes

    View Slide

  43. #DevoxxMA #JSR371 @ivar_grimstad
    @RedirectScoped
    @Named(“greeting”)
    @RedirectScoped
    public class Greeting {
    private String message;
    public void setMessate(String message) {
    this.message = message;
    }
    public void getMessage() { return message; }
    }

    View Slide

  44. #DevoxxMA #JSR371 @ivar_grimstad
    @Controller
    public class HelloController {
    @Inject
    private Greeting greeting;
    @POST
    @Path(“from”)
    public String hello() {
    greeting.setMessage(“Hello Devoxx!”);
    return “redirect:/to“;
    }
    }
    @RedirectScoped
    @RedirectScoped
    @RequestScoped

    View Slide

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

    View Slide

  46. #DevoxxMA #JSR371 @ivar_grimstad
    Security

    View Slide

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

    View Slide

  48. #DevoxxMA #JSR371 @ivar_grimstad
    Cross Site Request Forgery




    MVC 1.0 Hello Demo


    charset="utf-8">

    value="${mvc.csrf.token}"/>



    View Slide

  49. #DevoxxMA #JSR371 @ivar_grimstad
    View Engines

    View Slide

  50. #DevoxxMA #JSR371 @ivar_grimstad
    Bring Your Own View Engine

    View Slide

  51. #DevoxxMA #JSR371 @ivar_grimstad
    ViewEngine
    public interface ViewEngine {
    boolean supports(String view);
    void processView(ViewEngineContext context)
    throws ViewEngineException;
    }

    View Slide

  52. #DevoxxMA #JSR371 @ivar_grimstad
    View Engine Conflicts
    @Priority(OUTRAGEOSLY_HIGH)

    @ApplicationScoped

    public class FreemarkerViewEngine extends ViewEngineBase {

    }

    View Slide

  53. #DevoxxMA #JSR371 @ivar_grimstad
    Events

    View Slide

  54. #DevoxxMA #JSR371 @ivar_grimstad
    BeforeControllerEvent
    AfterControllerEvent
    BeforeProcessViewEvent
    AfterProcessViewEvent
    ControllerRedirectEvent

    View Slide

  55. #DevoxxMA #JSR371 @ivar_grimstad
    Events Example
    @ApplicationScoped
    public class EventObserver {
    void onBeforeController(@Observes BeforeControllerEvent e)
    {
    println("URI: " + e.getUriInfo().getRequestURI());
    }
    void onAfterController(@Observes AfterControllerEvent e) {
    println("Controller: " +
    e.getResourceInfo().getResourceMethod());
    }
    }

    View Slide

  56. #DevoxxMA #JSR371 @ivar_grimstad
    Summary

    View Slide

  57. #DevoxxMA #JSR371 @ivar_grimstad
    Project Page
    https://java.net/projects/mvc-spec
    Mailing List
    [email protected]
    Reference Implementation
    https://ozark.java.net

    View Slide

  58. #DevoxxMA #JSR371 @ivar_grimstad
    Samples
    https://github.com/ivargrimstad/mvc-samples
    Blog
    http://www.agilejava.eu/

    View Slide