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.

B489790e1a844284d7cd1fa2cd6e021f?s=128

ivargrimstad

April 28, 2016
Tweet

Transcript

  1. 1.

    MVC 1.0 - by Example Ivar Grimstad Principal Consultant, Cybercom

    Sweden JCP Expert Group Member (JSRs 368, 371, 375)
  2. 3.

    MVC

  3. 4.
  4. 13.

    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
  5. 22.
  6. 26.

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

    { return Response.status(OK).entity(“hello.jsp”).build(); } } View
  7. 29.
  8. 30.

    @Named(“greeting”) @RequestScoped public class Greeting { private String message; public

    void setMessage(String message) { this.message = message; } public void getMessage() { return message; } } Model
  9. 32.

    @View(“hello.jsp”) @Controller @Path(“hello”) public class HelloController { @Inject private Greeting

    greeting; @GET public void hello() { greeting.setMessage(“Hello GIDS 2016!”); } } Model
  10. 33.

    <%@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
  11. 35.

    @View(“hello.jsp”) @Controller @Path(“hello”) public class HelloController { @Inject private Models

    model; @GET public void hello() { model.put(“message”, “Hello GIDS 2016!”); } } Model
  12. 36.

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

    @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
  14. 39.

    public class FormViolationMapper implements ExceptionMapper<ConstraintViolationException> { public Response toResponse(ConstraintViolationException e)

    { // process violations … return Response.status(BAD_REQUEST) .entity(“error.jsp”).build(); } } Validation
  15. 40.

    @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
  16. 41.
  17. 42.

    @Controller @Path(“csrf”) public class HelloController { @CsrfValid @POST public Response

    post(@FormParam(“greeting") String greet { } } Cross Site Request Forgery
  18. 43.

    <%@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
  19. 44.
  20. 45.

    @Named(“greeting”) @RedirectScoped public class Greeting { private String message; public

    void setMessage(String message) { this.message = message; } public void getMessage() { return message; } } @RedirectScoped
  21. 46.

    @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
  22. 47.

    @Controller public class HelloController { @Inject private Greeting greeting; @POST

    @Path(“to”) public String hello() { return “here.jsp“; } } Same Bean @RedirectScoped
  23. 56.
  24. 58.

    @ApplicationScoped public class EventObserver { void onBeforeController(@Observes BeforeControllerEvent e) {

    } void onAfterController(@Observes AfterControllerEvent e) { } } Events Example
  25. 61.