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

MVC 1.0 - by Example @ GIDS 2016

MVC 1.0 - by Example @ GIDS 2016

Model-view-controller, or MVC for short, is a common pattern in web frameworks. MVC frameworks can be categorized as action-based or component-based. In a component-based framework, the HTTP requests are grouped and typically handled by framework components with little or no interaction with application code. In an action-based framework, on the other hand, the HTTP requests are routed to controllers and turned into actions by application code.

The Java EE platform does not currently have support for action based MVC, but this is about to change. MVC 1.0, as specified by JSR 371, is targeted for the upcoming Java EE 8 release.

This session provides an introduction to the action-based MVC 1.0 API and will include lots of code samples and tips to where to get started using this awesome technology.

ivargrimstad

April 28, 2016
Tweet

More Decks by ivargrimstad

Other Decks in Programming

Transcript

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  4. http://glassfish.org/survey

    View full-size slide

  5. JSR 371 - MVC 1.0

    View full-size slide

  6. JSR 371 Expert Group

    View full-size slide

  7. Action-based MVC

    View full-size slide

  8. Controller
    Model View
    Request
    Update
    Update
    Get

    View full-size slide

  9. Controller
    Model View
    Request
    Update
    Update
    Get
    Component-based MVC

    View full-size slide

  10. Controller
    Model View
    Request
    Update
    Update
    Get
    Action-based MVC

    View full-size slide

  11. 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 full-size slide

  12. Existing Java EE
    Technologies

    View full-size slide

  13. Key Decisions

    View full-size slide

  14. Key Decisions

    View full-size slide

  15. Build MVC 1.0 on top of
    JAX-RS

    View full-size slide

  16. public class HelloController {
    }
    Controller

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  28. <%@page contentType=“text/html" pageEncoding="UTF-8"%>



    MVC 1.0 Hello Demo


    Hello ${greeting.message}


    Model

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  31. <%@page contentType=“text/html" pageEncoding="UTF-8"%>



    MVC 1.0 Hello Demo


    Hello ${message}


    Model

    View full-size slide

  32. @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

    View full-size slide

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

    View full-size slide

  34. @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

    View full-size slide

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

    View full-size slide

  36. <%@page contentType=“text/html" pageEncoding="UTF-8"%>



    MVC 1.0 Hello Demo


    charset="utf-8">

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



    Cross Site Request Forgery

    View full-size slide

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

    View full-size slide

  38. @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

    View full-size slide

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

    View full-size slide

  40. Tired of slides?

    View full-size slide

  41. Show me the real
    CODE !

    View full-size slide

  42. Model
    ReservationCtl
    Update
    confirmation
    reservation
    OK
    Browser
    POST

    View full-size slide

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

    View full-size slide

  44. View Engines

    View full-size slide

  45. Bring Your Own View
    Engine

    View full-size slide

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

    View full-size slide

  47. @Priority(OUTRAGEOSLY_HIGH)

    @ApplicationScoped

    public class FreemarkerViewEngine extends ViewEngineBase {

    }
    ViewEngine

    View full-size slide

  48. BeforeControllerEvent
    AfterControllerEvent
    BeforeProcessViewEvent
    AfterProcessViewEvent
    ControllerRedirectEvent

    View full-size slide

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

    View full-size slide

  50. Tool Support

    View full-size slide

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

    View full-size slide

  52. Action-based MVC

    View full-size slide

  53. Existing Java EE
    Technologies

    View full-size slide

  54. Build MVC 1.0 on top of
    JAX-RS

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  57. @ivar_grimstad
    @greatindiandev #GIDS2016
    cybercom.com

    View full-size slide