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

MVC – Nachwuchs im Hause Java EE

MVC – Nachwuchs im Hause Java EE

Mathema Frühjahrscampus 2016 – 2016-04-08

Avatar for Florian Hirsch

Florian Hirsch

April 08, 2016

More Decks by Florian Hirsch

Other Decks in Programming

Transcript

  1. MVC – Java EE Nachwuchs – Florian Hirsch 1 Copyright

    © 2016 MATHEMA Software GmbH MVC – Nachwuchs im Hause Java EE Florian Hirsch / adorsys [email protected] twitter.com/lefloh / github.com/lefloh
  2. MVC – Java EE Nachwuchs – Florian Hirsch 2 Copyright

    © 2016 MATHEMA Software GmbH Let‘s build a Java EE Web Application!
  3. MVC – Java EE Nachwuchs – Florian Hirsch 3 Copyright

    © 2016 MATHEMA Software GmbH Servlets and JSPs?
  4. MVC – Java EE Nachwuchs – Florian Hirsch 4 Copyright

    © 2016 MATHEMA Software GmbH Portlets?
  5. MVC – Java EE Nachwuchs – Florian Hirsch 6 Copyright

    © 2016 MATHEMA Software GmbH JAX-RS?
  6. MVC – Java EE Nachwuchs – Florian Hirsch 7 Copyright

    © 2016 MATHEMA Software GmbH RESTful Websites?
  7. MVC – Java EE Nachwuchs – Florian Hirsch 8 Copyright

    © 2016 MATHEMA Software GmbH Java EE 8 Community Survey Results from the Java EE 8 Community Survey
  8. MVC – Java EE Nachwuchs – Florian Hirsch 9 Copyright

    © 2016 MATHEMA Software GmbH Goals n  Leverage existing Java EE technologies n  Integrate with CDI and Bean Validation n  Define a solid core to build MVC applications without necessarily supporting all the features in its first version n  Explore layering on top of JAX-RS for the purpose of re-using its matching and binding layers n  Provide built-in support for JSPs and Facelets view languages MVC Specification (Early Draft Review 2)
  9. MVC – Java EE Nachwuchs – Florian Hirsch 10 Copyright

    © 2016 MATHEMA Software GmbH Non Goals n  Define a new view (template) language and processor n  Support standalone implementations of MVC running outside of Java EE n  Support REST services not based on JAX-RS n  Provide built-in support for view languages that are not part of Java EE MVC Specification (Early Draft Review 2)
  10. MVC – Java EE Nachwuchs – Florian Hirsch 11 Copyright

    © 2016 MATHEMA Software GmbH Status Commits to Ozark (Reference Implementation)
  11. MVC – Java EE Nachwuchs – Florian Hirsch 12 Copyright

    © 2016 MATHEMA Software GmbH JSF Controller @Named @RequestScoped public class CustomerController { @Inject private CustomerVO vo; @Inject private CustomerDAO dao; public String showCustomer(long id) { Customer customer = dao.findCustomer(id); vo.setCustomer(customer); return "customer.xhtml"; } }
  12. MVC – Java EE Nachwuchs – Florian Hirsch 13 Copyright

    © 2016 MATHEMA Software GmbH AngularJS Controller app.controller('customerCtrl', function($scope, $http) { $scope.showCustomer = function(id) { $http.get('/api/customers/' + id).then(function(response) { $scope.customer = response.data; }); }; });
  13. MVC – Java EE Nachwuchs – Florian Hirsch 14 Copyright

    © 2016 MATHEMA Software GmbH AngularJS Controller + JAX-RS Resource app.controller('customerCtrl', function($scope, $http) { $scope.showCustomer = function(id) { $http.get('/api/customers/' + id).then(function(response) { $scope.customer = response.data; }); }; }); @Path("customers") public class CustomerController { @Inject private CustomerDAO dao; @Path("{id}") public Customer showCustomer(@PathParam("id") Long id) { return dao.findCustomer(id); } }
  14. MVC – Java EE Nachwuchs – Florian Hirsch 15 Copyright

    © 2016 MATHEMA Software GmbH MVC Controller @Controller @Path("customers") public class CustomerController { @Inject private Models models; @Inject private CustomerDAO dao; @Path("{id}") public String showCustomer(@PathParam("id") Long id) { Customer customer = dao.findCustomer(id); models.put("customer", customer); return "customer.jsp"; } }
  15. MVC – Java EE Nachwuchs – Florian Hirsch 16 Copyright

    © 2016 MATHEMA Software GmbH MVC Controller @Controller @Path("customers") public class CustomerController { @Inject private Models models; @Inject private CustomerDAO dao; @Path("{id}") public Response showCustomer(@PathParam("id") Long id) { Customer customer = dao.findCustomer(id); models.put("customer", customer); return Response.ok("customer.jsp").build(); } }
  16. MVC – Java EE Nachwuchs – Florian Hirsch 17 Copyright

    © 2016 MATHEMA Software GmbH MVC Controller @Controller @Path("customers") public class CustomerController { @Inject private Models models; @Inject private CustomerDAO dao; @Path("{id}") public Viewable showCustomer(@PathParam("id") Long id) { Customer customer = dao.findCustomer(id); models.put("customer", customer); return new Viewable("customer.jsp"); } }
  17. MVC – Java EE Nachwuchs – Florian Hirsch 18 Copyright

    © 2016 MATHEMA Software GmbH MVC Controller @Controller @Path("customers") public class CustomerController { @Inject private Models models; @Inject private CustomerDAO dao; @Path("{id}") @View("customer.jsp") public void showCustomer(@PathParam("id") Long id) { Customer customer = dao.findCustomer(id); models.put("customer", customer); } }
  18. MVC – Java EE Nachwuchs – Florian Hirsch 19 Copyright

    © 2016 MATHEMA Software GmbH MVC Controller (CDI) @Controller @Path("customers") public class CustomerController { @Inject private Customer customer; @Path("{id}") public String showCustomer(@PathParam("id") Long id) { return "customer.jsp"; } }
  19. MVC – Java EE Nachwuchs – Florian Hirsch 20 Copyright

    © 2016 MATHEMA Software GmbH JSF View <ui:composition xmlns="http://www.w3.org/1999/xhtml" xmlns:ui="http://java.sun.com/jsf/facelets" xmlns:h="http://java.sun.com/jsf/html" xmlns:f="http://java.sun.com/jsf/core" template="/resources/mainTemplate.xhtml"> <ui:param name="pageTitle" value="Customers" /> <ui:define name="main"> <h:dataTable id="customers" var="customer" value="#{customerController.allCustomers}"> <h:column> <f:facet name="header">Name</f:facet> <h:commandLink action="#{customerController.showCustomer(customer.id)}"> <h:outputText value="#{customer.name}" /> </h:commandLink> </h:column> </h:dataTable> </ui:define> </ui:composition>
  20. MVC – Java EE Nachwuchs – Florian Hirsch 21 Copyright

    © 2016 MATHEMA Software GmbH AngularJS View <!doctype html> <html lang="en"> <head> <title>Customers</title> </head> <body ng-app="myApp"> <table ng-controller="customerCtrl"> <tr> <th>Name</th> </tr> <tr ng-repeat="customer in customers"> <td ng-click="showCustomer(customer.id)"> {{ customer.name }} </td> </tr> </table> </body> </html>
  21. MVC – Java EE Nachwuchs – Florian Hirsch 22 Copyright

    © 2016 MATHEMA Software GmbH MVC View (JSP) <%@ page contentType="text/html; charset=utf-8" language="java" %> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <!doctype html> <html> <head> <title>Customers</title> </head> <body> <table> <tr> <th>Name</th> </tr> <c:forEach var="customer" items="${customers}"> <tr> <td><a href="${mvc.basePath}/customers/${customer.id}"> ${customer.name} </a></td> </tr> </c:forEach> </table> </body> </html>
  22. MVC – Java EE Nachwuchs – Florian Hirsch 23 Copyright

    © 2016 MATHEMA Software GmbH MVC View (Mustache) <!doctype html> <html> <head> <title>Customers</title> </head> <body> <body> <table> <tr> <th>Name</th> </tr> {{#customers}} <tr> <td><a href="{{mvc.basePath}}/customers/{{id}}">{{ name }}</a></td> </tr> {{/customers}} </table> </body> </html>
  23. MVC – Java EE Nachwuchs – Florian Hirsch 24 Copyright

    © 2016 MATHEMA Software GmbH Other ViewEngines n  AsciiDoc n  Freemarker n  Handlebars n  Jade n  JSR 223 n  Mustache n  StringTemplate n  Thymeleaf n  Velocity
  24. MVC – Java EE Nachwuchs – Florian Hirsch 25 Copyright

    © 2016 MATHEMA Software GmbH linkTo Method <%@ page contentType="text/html; charset=utf-8" language="java" %> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <!doctype html> <html> <head> <title>Customers</title> </head> <body> <table> <tr> <th>Name</th> </tr> <c:forEach var="customer" items="${customers}"> <tr> <td><a href="${mvc.linkTo('CustomerController', 'showCustomer', {'id': customer.id})}"> ${customer.name} </a></td> </tr> </c:forEach> </table> </body> </html>
  25. MVC – Java EE Nachwuchs – Florian Hirsch 26 Copyright

    © 2016 MATHEMA Software GmbH linkTo Controller <%@ page contentType="text/html; charset=utf-8" language="java" %> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <!doctype html> <html> <head> <title>Customers</title> </head> <body> <table> <tr> <th>Name</th> </tr> <c:forEach var="customer" items="${customers}"> <tr> <td><a href="${mvc.linkTo('show-customer', {'id': customer.id})}"> ${customer.name} </a></td> </tr> </c:forEach> </table> </body> </html>
  26. MVC – Java EE Nachwuchs – Florian Hirsch 27 Copyright

    © 2016 MATHEMA Software GmbH Content-Type @Controller @Path("something") @Produces("text/html") public class SomeController { ... } <%@ page contentType="text/html; charset=utf-8" language="java" %>
  27. MVC – Java EE Nachwuchs – Florian Hirsch 28 Copyright

    © 2016 MATHEMA Software GmbH Validation @Controller @Path("customers") public class ValidationController { @Inject private BindingResult br; @Inject private Errors errors; @POST @ValidateOnExecution(type = ExecutableType.NONE) public Response newCustomer(@Valid @BeanParam CustomerForm form) { if (br.isFailed()) { for (ValidationError error : br.getAllValidationErrors()) { errors.add(new Error(error.getParamName(), error.getMessage())); } return Response.status(BAD_REQUEST) .entity("customer-form.jsp").build(); } return Response.status(OK).entity("customer-view.jsp").build(); } }
  28. MVC – Java EE Nachwuchs – Florian Hirsch 29 Copyright

    © 2016 MATHEMA Software GmbH PR 1: ValidationError /** * <p>Represents a single validation error detected for a parameter. * A validation error always corresponds to exactly one * {@link ConstraintViolation}.</p> * * @author Christian Kaltepoth * @since 1.0 */ public interface ValidationError { String getParamName(); ConstraintViolation<?> getViolation(); String getMessage(); } https://github.com/mvc-spec/mvc-spec/pull/1
  29. MVC – Java EE Nachwuchs – Florian Hirsch 30 Copyright

    © 2016 MATHEMA Software GmbH PR 2: LocaleResolver /** * <p>Locale resolvers are used to determine the locale of the current * request and are discovered using CDI.</p> * * <p>The MVC implementation is required to resolve the locale for each * request following this algorithm:</p> * * ... * * @author Christian Kaltepoth */ public interface LocaleResolver { String DEFAULT_LOCALE = "javax.mvc.locale.LocaleResolver.defaultLocale"; Locale resolveLocale(LocaleResolverContext context); } https://github.com/mvc-spec/mvc-spec/pull/2
  30. MVC – Java EE Nachwuchs – Florian Hirsch 31 Copyright

    © 2016 MATHEMA Software GmbH PR 3: Servlet Dependency Remove Servlet dependencies from MVC API? https://github.com/mvc-spec/mvc-spec/pull/3
  31. MVC – Java EE Nachwuchs – Florian Hirsch 32 Copyright

    © 2016 MATHEMA Software GmbH PR 4: OSGI Module Package MVC 1.0 as OSGI bundle? https://github.com/mvc-spec/mvc-spec/pull/4
  32. MVC – Java EE Nachwuchs – Florian Hirsch 33 Copyright

    © 2016 MATHEMA Software GmbH Fragen? Vielen Dank! [email protected]