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

MVC 1.0 - by Example @ JavaDay Minsk 2016

MVC 1.0 - by Example @ JavaDay Minsk 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

June 11, 2016
Tweet

More Decks by ivargrimstad

Other Decks in Programming

Transcript

  1. @ivar_grimstad JavaDay Minsk 2016 - #JSR371 MVC 1.0 - by

    Example Ivar Grimstad Principal Consultant, Cybercom Sweden
  2. @ivar_grimstad JavaDay Minsk 2016 - #JSR371 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
  3. @ivar_grimstad JavaDay Minsk 2016 - #JSR371 View @Controller @Path(“hello”) public

    class HelloController { @GET public String hello() { return “hello.jsp”; } }
  4. @ivar_grimstad JavaDay Minsk 2016 - #JSR371 View @Controller @Path(“hello”) public

    class HelloController { @GET public Viewable hello() { return new Viewable(“hello.jsp”); } }
  5. @ivar_grimstad JavaDay Minsk 2016 - #JSR371 View @Controller @Path(“hello”) public

    class HelloController { @GET public Response hello() { return Response.status(OK).entity(“hello.jsp”).build(); } }
  6. @ivar_grimstad JavaDay Minsk 2016 - #JSR371 View @Controller @Path(“hello”) public

    class HelloController { @View(“hello.jsp”) @GET public void hello() { } }
  7. @ivar_grimstad JavaDay Minsk 2016 - #JSR371 Model @Named(“greeting”) @RequestScoped public

    class Greeting { private String message; public void setMessage(String message) { this.message = message; } public void getMessage() { return message; } }
  8. @ivar_grimstad JavaDay Minsk 2016 - #JSR371 Model @View(“hello.jsp”) @Controller @Path(“hello”)

    public class HelloController { @Inject private Greeting greeting; @GET public void hello() { greeting.setMessage(“Hello Minsk!”); } }
  9. @ivar_grimstad JavaDay Minsk 2016 - #JSR371 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>
  10. @ivar_grimstad JavaDay Minsk 2016 - #JSR371 Model @View(“hello.jsp”) @Controller @Path(“hello”)

    public class HelloController { @Inject private Models model; @GET public void hello() { model.put(“message”, “Hello Minsk!”); } }
  11. @ivar_grimstad JavaDay Minsk 2016 - #JSR371 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>
  12. @ivar_grimstad JavaDay Minsk 2016 - #JSR371 @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.
  13. @ivar_grimstad JavaDay Minsk 2016 - #JSR371 Validation public class FormViolationMapper

    implements ExceptionMapper<ConstraintViolationException> { public Response toResponse(ConstraintViolationException e) { Set<ConstraintViolation<?>> s= e.getConstraintViolations(); // process violations ... return Response.status(BAD_REQUEST) .entity(“error.jsp”).build(); } }
  14. @ivar_grimstad JavaDay Minsk 2016 - #JSR371 @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
  15. @ivar_grimstad JavaDay Minsk 2016 - #JSR371 ViewEngine public interface ViewEngine

    { boolean supports(String view); void processView(ViewEngineContext context) throws ViewEngineException; }
  16. @ivar_grimstad JavaDay Minsk 2016 - #JSR371 View Engine Conflicts @Priority(OUTRAGEOSLY_HIGH)


    @ApplicationScoped
 public class FreemarkerViewEngine extends ViewEngineBase { … }
  17. @ivar_grimstad JavaDay Minsk 2016 - #JSR371 Controller Model View Request

    BeforeControllerEvent AfterControllerEvent BeforeProcessViewEvent AfterProcessViewEvent ControllerRedirectEvent
  18. @ivar_grimstad JavaDay Minsk 2016 - #JSR371 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()); } }
  19. @ivar_grimstad JavaDay Minsk 2016 - #JSR371 Cross Site Request Forgery

    @Controller @Path(“csrf”) public class HelloController { @CsrfValid @POST public Response post(@FormParam(“greeting") String greet { } }
  20. @ivar_grimstad JavaDay Minsk 2016 - #JSR371 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>
  21. @ivar_grimstad JavaDay Minsk 2016 - #JSR371 Model ReservationCtl Access Update

    ConfirmationCtl GET confirmation reservation OK Redirect Browser POST
  22. @ivar_grimstad JavaDay Minsk 2016 - #JSR371 @RedirectScoped @Named(“greeting”) @RedirectScoped public

    class Greeting { private String message; public void setMessage(String message) { this.message = message; } public void getMessage() { return message; } }
  23. @ivar_grimstad JavaDay Minsk 2016 - #JSR371 @Controller public class HelloController

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

    { @Inject private Greeting greeting; @POST @Path(“to”) public String hello() { return “here.jsp“; } } @RedirectScoped Same Bean
  25. @ivar_grimstad JavaDay Minsk 2016 - #JSR371 MVC 1.0 Logical View

    https://github.com/GeertjanWielenga/BasicMVCSupport Geertjan Wielenga @GeertjanW
  26. @ivar_grimstad JavaDay Minsk 2016 - #JSR371 Project Page https://java.net/projects/mvc-spec GitHub

    https://github.com/mvc-spec Reference Implementation https://ozark.java.net