@@ -0,0 +1,1912 @@
+package de.asw.web2.demo;%0A%0Aimport static java.util.Arrays.asList;%0A%0Aimport java.math.BigDecimal;%0Aimport java.util.List;%0A%0Aimport org.springframework.http.ResponseEntity;%0Aimport org.springframework.util.MultiValueMap;%0Aimport org.springframework.web.bind.annotation.GetMapping;%0Aimport org.springframework.web.bind.annotation.PostMapping;%0Aimport org.springframework.web.bind.annotation.RequestBody;%0Aimport org.springframework.web.bind.annotation.RestController;%0A%0A@RestController%0Aclass SimpleWebCalculator %7B%0A%0A%09/**%0A%09 * List's available operations.%0A%09 * %0A%09 * %7B@code curl http://localhost:8080/calc/ops %7D%0A%09 * %0A%09 * @param formData%0A%09 * @return%0A%09 */%0A%09@GetMapping(%22/calc/ops%22)%0A%09List%3CString%3E listSupportedOperations() %7B%0A%09%09return asList(%22add:(x + y)%22, %22subtract:(x - y)%22, %22multiply:(x * y)%22, %22divide:(x / y)%22, %22pow:(x %5E y)%22);%0A%09%7D%0A%0A%09/**%0A%09 * Computes the result for the given operations and operands.%0A%09 * %0A%09 * %7B@code curl -d %22x=2&y=10&op=pow%22 http://localhost:8080/calc %7D%0A%09 * %0A%09 * @see #listSupportedOperations() for the list of supported op-codes.%0A%09 * @param formData%0A%09 * @return%0A%09 */%0A%09@PostMapping(%22/calc%22)%0A%09ResponseEntity%3CString%3E calc(@RequestBody MultiValueMap%3CString, String%3E formData) %7B%0A%0A%09%09Object result;%0A%09%09try %7B%0A%0A%09%09%09BigDecimal x = new BigDecimal(formData.getFirst(%22x%22));%0A%09%09%09BigDecimal y = new BigDecimal(formData.getFirst(%22y%22));%0A%09%09%09String op = formData.getFirst(%22op%22);%0A%0A%09%09%09switch (op) %7B%0A%09%09%09case %22add%22:%0A%09%09%09%09result = x.add(y);%0A%09%09%09%09break;%0A%09%09%09case %22subtract%22:%0A%09%09%09%09result = x.subtract(y);%0A%09%09%09%09break;%0A%09%09%09case %22multiply%22:%0A%09%09%09%09result = x.multiply(y);%0A%09%09%09%09break;%0A%09%09%09case %22divide%22:%0A%09%09%09%09result = x.divide(y);%0A%09%09%09%09break;%0A%09%09%09case %22pow%22:%0A%09%09%09%09result = x.pow(y.intValue());%0A%09%09%09%09break;%0A%09%09%09default:%0A%09%09%09%09return ResponseEntity.badRequest().body(%22Error: don't know how to: %22 + op);%0A%09%09%09%7D%0A%0A%09%09%7D catch (NumberFormatException nfe) %7B%0A%09%09%09return ResponseEntity.badRequest().body(nfe.toString());%0A%09%09%7D%0A%0A%09%09return ResponseEntity.ok(result.toString());%0A%09%7D%0A%7D%0A

Add simple calculator for HTML Form examples.