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
  2. #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
  3. #DevoxxMA #JSR371 @ivar_grimstad View @Controller @Path(“hello”) public class HelloController {

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

    @GET public Response hello() { return Response.status(OK).entity(“hello.jsp”).build(); } }
  5. #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; } }
  6. #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!”); } }
  7. #DevoxxMA #JSR371 @ivar_grimstad 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>
  8. #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!”); } }
  9. #DevoxxMA #JSR371 @ivar_grimstad 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>
  10. #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.
  11. #DevoxxMA #JSR371 @ivar_grimstad 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(); } }
  12. #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
  13. #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; } }
  14. #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
  15. #DevoxxMA #JSR371 @ivar_grimstad @Controller public class HelloController { @Inject private

    Greeting greeting; @POST @Path(“to”) public String hello() { return “here.jsp“; } } @RedirectScoped Same Bean
  16. #DevoxxMA #JSR371 @ivar_grimstad Cross Site Request Forgery @Controller @Path(“csrf”) public

    class HelloController { @CsrfValid @POST public Response post(@FormParam(“greeting") String greet { } }
  17. #DevoxxMA #JSR371 @ivar_grimstad 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>
  18. #DevoxxMA #JSR371 @ivar_grimstad ViewEngine public interface ViewEngine { boolean supports(String

    view); void processView(ViewEngineContext context) throws ViewEngineException; }
  19. #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()); } }