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

Web frameworks in Java: 10 years of history!

Web frameworks in Java: 10 years of history!

From the Servlets/JSP to Struts, JSF and Spring @MVC: how did we get there?
(presentation from Nik Trevallyn Jones and Michael Isvy at the SingaSUG (Singapore Spring User Group).

Michael Isvy

May 08, 2014
Tweet

More Decks by Michael Isvy

Other Decks in Technology

Transcript

  1. 1 Agenda Ÿ  To all attendees with a mobile phone

    connection: please answer this survey: http://tinyurl.com/singasug Ÿ  Nik TJ and Michael I : web frameworks in Java Ÿ  Salah C. and Srikanth N : Spring MVC live coding session N
  2. 2 Nik Trevallyn-Jones Ÿ  Contract Consultant Ÿ  Been teaching and

    Consulting on Spring since 2008 Ÿ  Has Taught Spring to more than 1000 students in 6 years Ÿ  OpenSource Contributor to several Spring projects –  Spring HATEAOS, Spring ROO… M
  3. 3 Michael  Isvy Ÿ  Training Manager at Pivotal (APJ region)

    2008 - Spring - Tomcat 2009-2012 -  Spring -  Tomcat -  Cloud Foundry -  GemFire… 2013 onwards -  Spring, Cloud Foundry -  Tomcat, Redis, Hadoop -  GemFire N
  4. 6 Disclaimer: framework versus standard Ÿ  JSF is part of

    Java EE specification Ÿ  Technically JSF is not exactly a web framework but rather a specification and an implementation –  We’ve used the word “framework” here to keep things simple N
  5. 7 2000: Servlet/JSP public class MyServlet extends HttpServlet {" "

    " public void doGet(HttpServletRequest req, HttpServletResponse resp) {" …" } public void doPost(HttpServletRequest req, HttpServletResponse resp) {" … } " } Not a POJO Request params parsed manually Form validation done manually No I18N support … Verbose XML config M
  6. 8 2003: Apache Struts public class UserAction extends Action {"

    " " public void execute(ActionMapping mapping, ActionForm form, HttpServletRequest req, HttpServletResponse resp) {" …" } " } Not a POJO Request params parsed manually Form validation done manually Verbose XML config … public class UserForm extends ActionForm {" " private String name; public String getName() { return name; } public void seName(String name) { this.name = name; }" } No Internationalization support N
  7. 9 2003: Apache Struts Ÿ  Java community formed a consensus

    Ÿ  Quickly became the way to go for Java web frameworks Ÿ  So what happened next? 9 N
  8. 10 2004: JSF Ÿ  Struts was created by Craig McClanahan

    –  Also one of the key founders of Servlet, JSP, JSTL and Tomcat Ÿ  Craig wanted to create a component-oriented Web framework –  Event/Listener model M
  9. 11 2004: JSF Ÿ  Problem: view layer was good but

    some major limitations –  Form validation very limited –  Couldn’t disable JavaScript –  No support for “GET” methods… Ÿ  Specification versus framework –  For it to succeed, JSF should have been a framework, not a specification ▪  Frameworks can be updated every month ▪  Java EE Specs are updated once every 2 years M
  10. 12 2005: The JSF / Struts 1 & 2 story

    Original plan What happened Struts JSF Struts 2 Struts WebWork Struts 2 M
  11. 13 2005: Spring MVC without annotations Ÿ  Spring MVC was

    similar to Struts –  But more modern ▪  Easier to extend ▪  Well integrated for Dependency Injection public class UserController extends SimpleFormController { 
 private UserService service; public ModelAndView onSubmit(Object command) { //... } } N Not a POJO Request params parsed manually Form validation done manually Simpler XML config … No Internationalization support
  12. 14 2008: Spring @MVC Ÿ  Annotations are the way to

    go for Spring @MVC! public class UserController extends SimpleFormController { 
 private UserService service; public ModelAndView onSubmit(Object command) { //... } } @Controller public class UserController { @Autowired private UserService service; @RequestMapping(value="/users/", method=RequestMethod.POST) public ModelAndView createUser(User user) 
 { //... } } N Now POJO based!! No XML needed for controllers anymore!!
  13. 16 Let’s talk about CSS and Javascript Ÿ  Back in

    2008, there were plenty of small CSS frameworks –  None of them really did the job Ÿ  Now there is Bootstrap! M
  14. 17 What is Bootstrap? Ÿ  Originally called “Twitter Bootstrap” Ÿ 

    Available from 2011 Ÿ  Typography, forms, buttons, charts, navigation and other interface components Ÿ  Integrates well with jQuery M
  15. 19 Let’s talk about JavaScript Ÿ  Back in 2008, there

    were too many JavaScript frameworks Ÿ  Now there is jQuery! M
  16. 20 Spring @MVC and the View Layer Ÿ  Spring @MVC

    is neat and elegant on the controller side –  Form submission, Validation, File upload, testing… Ÿ  Spring @MVC is view-agnostic –  No much tooling for integration with CSS/JavaScript frameworks –  It’s your job to create the custom tags you need ▪  But that’s not hard! N
  17. 21 Form fields: Without custom tags <div class=“control-group” id=“${lastName}"> <label

    class="control-label">${lastNameLabel}</label> <div class="controls"> <form:input path="${name}"/> <span class="help-inline"> <form:errors path="${name}"/> </span> </div> </div> CSS div Label Form input Error message (if any) JSP M
  18. 22 Using custom tags Ÿ  First create a tag (or

    tagx) file <%@ taglib prefix="form" uri="http://www.spring…org/tags/form" %> <%@ attribute name="name" required="true" rtexprvalue="true" %> <%@ attribute name="label" required="true" rtexprvalue="true" %> <div class="control-group" id="${name}"> <label class="control-label">${label}</label> <div class="controls"> <form:input path="${name}"/> <span class="help-inline"> <form:errors path="${name}"/> </span> </div> </div> inputField.tag Custom tags are part of Java EE N
  19. 23 Using custom tags Ÿ  Custom tag call Folder which

    contains custom tags <html xmlns:custom="urn:jsptagdir:/WEB-INF/tags/html" …> … <custom:inputField name="firstName" label="Enter your first name" /> <custom:inputField name=”lastName" label="Enter your last name" /> </html> JSP file 1 line of code instead of 9!! N
  20. 24 Some tags already exist! Ÿ  Dandelion for DataTables (nice

    addition to Spring @MVC) –  dandelion.github.com <datatables:table data="${userList}" id="dataTable" > <datatables:column title="First Name" property="firstName" sortable="true" /> <datatables:column title="Last Name" property="lastName" sortable="true" /> </datatables:table> JSP file M
  21. 25 Dandelion is based on jQuery Datatables and Bootstrap Ÿ 

    Click, sort, scroll, search, next/previous… Ÿ  PDF and Excel exports… M
  22. 27 Conclusion Ÿ  Once  upon  a  2me,  Java  community  was

     unified  around  Struts!   Ÿ  JavaScript  and  CSS:  you  now  have  good  frameworks!   Ÿ  Spring  @MVC  now  is  the  most  popular  Web  framework  for  Java   Ÿ  Create  your  own  custom  tags  for  the  View  layer   Ÿ  Use  Dandelion  for  your  DataTables   N