Slide 1

Slide 1 text

Case study Classic MVC with separated front-end Piotr Lewandowski @piotlr

Slide 2

Slide 2 text

Back-end Java 8 Spring 4 + Spring boot Apache POI Thymeleaf Gradle Front-end bootstrap require.js npm bower gulp

Slide 3

Slide 3 text

Java 8

Slide 4

Slide 4 text

Java 7 way Java 8 way List humans = ... Collections.sort(humans, new Comparator() { @Override public int compare(Human h1, Human h2) return h1.getName().compareTo(h2.getName()); } } List humans = ... Collections.sort(humans, (Human h1, Human h2) ­> { h1.getName().compareTo(h2.getName()) });

Slide 5

Slide 5 text

Streams API List persons = ...; Stream tenPersonsOver18 = persons.stream() .filter(p ­> p.getAge() > 18) .limit(10); boolean isEmptyRow = csvRowEntries.stream() .allMatch(str ­> str.isEmpty()); if (isEmptyRow) { return null; }

Slide 6

Slide 6 text

Streams API // set id, location employees.stream() .filter(e ­> e.getId().equals(id)) .findFirst() .ifPresent(e ­> { e.setLocation(location); }); public List getErrors() { return parseErrors.stream() .map(pe ­> pe.getMessage()) .collect(Collectors.toList()); }

Slide 7

Slide 7 text

Streams in tests // arrange // ... List divergenceReport; List missingEmployees; // act divergenceReport = service.getDivergenceReport(); missingEmployees = divergenceReport.stream() .map(MissingEmployee::getId) .collect(Collectors.toList()); // assert assertThat(missingEmployees.contains("Robert Smith"), is(true));

Slide 8

Slide 8 text

Confusions Closure Custom lambdas

Slide 9

Slide 9 text

Closures int i = 0; persons.forEach(person ­> { if ( person == null) { i++; // Error: Variable must be final or effective final } ... });

Slide 10

Slide 10 text

Custom lamdas public class Main { public static void runCalc( ??? calc) { // ??? } public static void main(String[] args) { BigDecimal a = new BigDecimal(); runCalc(a ­> a.multiply(a)); } }

Slide 11

Slide 11 text

Custom lambdas Scala def runCalc(calc: (BigInt => BigInt)) { // ??? } Java interface MyCalcLambda { BigInteger run(BigInteger input); } public static void runCalc(MyCalcLambda calc) { // ??? } public static void main(String[] args) { runCalc(a ­> a.multiply(a)); }

Slide 12

Slide 12 text

Runing custom lambdas Scala def runCalc(calc: (BigInt => BigInt)) { System.out.println(calc(10)) } Java public static void runCalc(MyCalcLambda calc) { System.out.println(calc(BigInteger.TEN)); }

Slide 13

Slide 13 text

Spring 4 with Spring boot Long story short

Slide 14

Slide 14 text

Configuration public class ServletInitializer extends SpringBootServletInitializer { @Override protected SpringApplicationBuilder configure(SpringApplicationBuilder application) { return application.sources(Application.class); } } @Configuration @ComponentScan @EnableAutoConfiguration public class Application extends WebMvcConfigurerAdapter { public static void main(String[] args) { SpringApplication.run(Application.class, args); } }

Slide 15

Slide 15 text

start.spring.io

Slide 16

Slide 16 text

Thymeleaf

Slide 17

Slide 17 text

Primitive Maps Lists i18n config constans loops conditionals formats modules What can we put? What can we do?

Slide 18

Slide 18 text

Name Cost Hours Project ID 1000.0 353

Slide 19

Slide 19 text

HTML5 way /*/­­> /*/­­> ... ... ...

Slide 20

Slide 20 text

Gradle

Slide 21

Slide 21 text

Worries Do I need to learn groovy? Can I still use my dependencies? If my IDE supports gradle?

Slide 22

Slide 22 text

gradle hello world apply plugin: 'java' sourceCompatibility = 1.8 jar { baseName = 'demo' version = '0.0.1­SNAPSHOT' } repositories { mavenCentral() } dependencies { compile("org.springframework.boot:spring­boot­starter­web") compile("org.springframework.boot:spring­boot­starter­thymeleaf") testCompile("org.springframework.boot:spring­boot­starter­test") }

Slide 23

Slide 23 text

repositories { maven { credentials { username 'user' password 'password' } url "http://10.10.0.1/your­maven­repo" } } Custom repostiories

Slide 24

Slide 24 text

So why gradle? Clean Efficient Easily expandable with plugins

Slide 25

Slide 25 text

Front-end

Slide 26

Slide 26 text

Building front-end Modularity Dependency managment Production-ready Tests

Slide 27

Slide 27 text

Cons Many, many tools Frequent changes Pros Many, many tools Frequent changes

Slide 28

Slide 28 text

Front-end structure src ├── css ├── js dist ├── lib ├── css ├── js gulpfile.js bower.json package.json

Slide 29

Slide 29 text

Build tools demo

Slide 30

Slide 30 text

Building process What are doing when we type `gradle build`

Slide 31

Slide 31 text

gradle build 1. Compille front-end 1. Download tools with NPM 2. Download dependencies with bower 3. Build front-end with gulp 4. Pack into JAR 2. Compile the rest with front-end as a dependency

Slide 32

Slide 32 text

Questions

Slide 33

Slide 33 text

Thanks