thymeleaf
https://www.thymeleaf.org/doc/tutorials/3.0/usingthymeleaf.html#iteration-basics
package com.walmart.services.tareweight.controller;
import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE;
import com.walmart.services.tareweight.model.TareWeight;
import com.walmart.services.tareweight.service.BulkDataService;
import com.walmart.services.tareweight.service.DataService;
import io.strati.metrics.annotation.Timed;
import io.strati.security.jaxrs.ws.rs.Path;
import java.util.Collection;
import java.util.List;
import javax.validation.Valid;
import org.bouncycastle.math.raw.Mod;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.http.HttpStatus;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;
/**
* Created by vn501cc on 6/25/2018.
*/
@RestController
public class TareWeightController {
@Autowired
DataService service;
@Autowired
BulkDataService bulkDataService;
@Autowired
Environment env;
//main page
@RequestMapping(value="/", method = RequestMethod.GET)
public ModelAndView welcome(){
ModelAndView modelAndView = new ModelAndView();
modelAndView.setViewName("home");
return modelAndView;
}
@GetMapping(value ="/{id}", produces = APPLICATION_JSON_VALUE)
@Timed(name = "GetTareWeightById", level1 = "HTTP", level2 = "GET", level3 = "TareWeightController")
public TareWeight getTareWeight(@PathVariable("id") String id) {
return service.findById(id)
.orElseThrow(() -> new TareWeightNotFoundException(id));
}
//check one tare weight
@RequestMapping(value="/getTareWeight", method=RequestMethod.GET)
public ModelAndView checkId(){
ModelAndView modelAndView = new ModelAndView();
TareWeight tareWeight = new TareWeight();
modelAndView.addObject("tareWeight", tareWeight );
modelAndView.setViewName("getTareWeight");
return modelAndView;
}
//show one tare weight
@RequestMapping(value = "/getTareWeight", method = RequestMethod.POST)
public ModelAndView showOne(@Valid TareWeight oldtareWeight){
ModelAndView modelAndView = new ModelAndView();
if(service.findById(oldtareWeight.getId()).isPresent()){
TareWeight tareWeight = service.findById(oldtareWeight.getId()).get();
modelAndView.addObject("tareWeightId", tareWeight.getId());
modelAndView.addObject("tareWeightWeight", tareWeight.getWeight());
modelAndView.addObject("tareWeightPercent", tareWeight.getPercent());
modelAndView.addObject("tareWeightVersion", tareWeight.getVersion());
modelAndView.addObject("tareWeightLock", tareWeight.getLock());
modelAndView.setViewName("showone");
return modelAndView;
} else{
modelAndView.addObject("errorMessage", "There isn't any tare weight data with this Id.");
modelAndView.setViewName("error");
return modelAndView;
}
}
@PostMapping(consumes = APPLICATION_JSON_VALUE, produces = APPLICATION_JSON_VALUE)
@ResponseStatus(HttpStatus.CREATED)
@Timed(name = "CreateTareWeight", level1 = "HTTP", level2 = "POST", level3 = "TareWeightController")
public TareWeight createTareWeight(@RequestBody TareWeight tareWeight) {
return service.createOrUpdate(tareWeight);
}
//create one tare weight data
@GetMapping(value = "/add")
public ModelAndView addTareWeight(){
ModelAndView modelAndView = new ModelAndView();
TareWeight tareWeight = new TareWeight();
modelAndView.addObject("tareWeight", tareWeight);
modelAndView.setViewName("add");
return modelAndView;
}
//create one tare weight data and save
@PostMapping(value = "/add")
public ModelAndView createTareWeight(@Valid TareWeight tareWeight, BindingResult bindingResult){
ModelAndView modelAndView = new ModelAndView();
if(service.findById(tareWeight.getId()).isPresent()){
modelAndView.addObject("errorMessage", "Oops! It seems this tare weight data has been added already. Please try another one.");
modelAndView.setViewName("error");
return modelAndView;
//bindingResult.rejectValue("id","There is already a same tare weight data");
}
if(bindingResult.hasErrors()){
modelAndView.setViewName("add");
} else{
service.createOrUpdate(tareWeight);
modelAndView.addObject("successMessage", "tare weight data has been added successfully!");
modelAndView.addObject("tareWeight", tareWeight);
modelAndView.setViewName("add");
}
return modelAndView;
}
@DeleteMapping(path = "/{id}")
@ResponseStatus(HttpStatus.NO_CONTENT)
@Timed(name = "DeleteTareWeight", level1 = "HTTP", level2 = "DELETE", level3 = "TareWeightController")
public void deleteTareWeight(@PathVariable("id") String id) {
service.delete(id);
}
@GetMapping(value= "/delete")
public ModelAndView delTareWeight(){
ModelAndView modelAndView = new ModelAndView();
TareWeight tareWeight = new TareWeight();
modelAndView.addObject("tareWeight", tareWeight);
modelAndView.setViewName("delete");
return modelAndView;
}
//delete page
@PostMapping(value = "/delete")
public ModelAndView deleteTareWeight(@Valid TareWeight tareWeight, BindingResult bindingResult){
ModelAndView modelAndView = new ModelAndView();
if(service.findById(tareWeight.getId()).isPresent()){
service.delete(tareWeight.getId());
modelAndView.addObject("successMessage", "The data is deleted.");
modelAndView.setViewName("delete");
return modelAndView;
}
if(bindingResult.hasErrors()){
modelAndView.setViewName("delete");
}
else{
modelAndView.addObject("errorMessage","There isn't any tare weight data with this Id.");
modelAndView.setViewName("error");
}
return modelAndView;
}
@PutMapping(path = "/{id}", produces = APPLICATION_JSON_VALUE, consumes = APPLICATION_JSON_VALUE)
@Timed(name = "UpdateTareWeight", level1 = "HTTP", level2 = "PUT", level3 = "TareWeightController")
public TareWeight updateTareWeight(@PathVariable("id") int id, @RequestBody TareWeight tareWeight) {
return service.createOrUpdate(tareWeight);
}
//update
@GetMapping(value="/update")
public ModelAndView upTareWeight(){
ModelAndView modelAndView = new ModelAndView();
TareWeight tareWeight = new TareWeight();
modelAndView.addObject("tareWeight", tareWeight);
modelAndView.setViewName("update");
return modelAndView;
}
@RequestMapping(value="/update", method=RequestMethod.POST)
public ModelAndView updateTareWeight(@Valid TareWeight tareWeight, BindingResult bindingResult){
ModelAndView modelAndView = new ModelAndView();
if(service.findById(tareWeight.getId()).isPresent()){
service.delete(tareWeight.getId());
service.createOrUpdate(tareWeight);
modelAndView.addObject("tareWeight", tareWeight);
modelAndView.addObject("successMessage", "The data has been updated!");
modelAndView.setViewName("update");
return modelAndView;
}else{
modelAndView.addObject("errorMessage", "Oops! It seems this tare weight data hasn't been added yet. Please update after creating.");
modelAndView.setViewName("error");
return modelAndView;
}
}
@GetMapping(path = "/all", produces = APPLICATION_JSON_VALUE)
@Timed(name = "GetAllTareWeights", level1 = "HTTP", level2 = "GET", level3 = "TareWeightController")
public Collection<TareWeight> getAllTareWeights() {
return service.findAll();
}
@GetMapping(path = "/loadfile", produces = APPLICATION_JSON_VALUE)
@Timed(name = "LoadTareWeightData", level1 = "HTTP", level2 = "GET", level3 = "TareWeightController")
public Collection<TareWeight> loadTareWeightData() {
bulkDataService.loadBulkDataFromFile();
return service.findAll();
}
@GetMapping(value="/all")
public ModelAndView showAll(){
ModelAndView modelAndView = new ModelAndView();
List<TareWeight> list = service.findAll();
modelAndView.addObject("list", list);
modelAndView.setViewName("showAll");
return modelAndView;
}
@ResponseStatus(HttpStatus.NOT_FOUND)
class TareWeightNotFoundException extends RuntimeException {
public TareWeightNotFoundException(String id) {
super("could not find tare weight '" + id + "'.");
}
}
//error page
@RequestMapping(value="/error", method = RequestMethod.GET)
public ModelAndView error(){
ModelAndView modelAndView = new ModelAndView();
modelAndView.setViewName("error");
return modelAndView;
}
}
ShowAll.html
<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml" xmlns:th = "http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Show one TareWeightData</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
<form th:action="@{/}" method="get">
<button class="btn" type="submit">Go to Welcome page</button>
</form>
<div class="panel-group" style="margin-top:50px">
<h3>Tare Weight Details</h3>
<table width="100%">
<tr>
<th>Id</th>
<th>weight</th>
<th>percent</th>
<th>version</th>
<th>lock</th>
</tr>
<tr th:each="tareWeight : ${list}">
<td th:utext="${tareWeight.id}"></td>
<td th:utext="${tareWeight.weight}"></td>
<td th:utext="${tareWeight.percent}"></td>
<td th:utext="${tareWeight.version}"></td>
<td th:utext="${tareWeight.lock!=null}?${tareWeight.lock}:'null'"></td>
</tr>
</table>
</div>
</div>
</body>
</html>
add.html
<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml" xmlns:th = "http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Add one TareWeightData</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body>
<div class = "container">
<div class="form-group" >
<form th:action="@{/}" method="get" style="margin-top:20px">
<button class="btn" type="submit" >Go to Welcome page</button>
</form>
</div>
<div class = "row">
<form autocomplete="off" action="#" th:action="@{/add}" th:object="${tareWeight}" method="post" class="form-horizontal" role="form">
<h3>tare weight creation form</h3>
<!--id-->
<div class="form-group">
<label th:if="${#fields.hasErrors('id')}" th:errors="*{id}" class="validation-message"></label>
<input type="text" th:field="*{id}" placeholder="Id" class="form-control" required="required" />
</div>
<!--weight-->
<div class="form-group">
<label th:if="${#fields.hasErrors('weight')}" th:errors="*{weight}" class="validation-message"></label>
<input type="text" th:field="*{weight}" placeholder="weight" class="form-control"/>
</div>
<!--percent-->
<div class="form-group">
<label th:if="${#fields.hasErrors('percent')}" th:errors="*{percent}" class="validation-message"></label>
<input type="text" th:field="*{percent}" placeholder="percent" class="form-control"/>
</div>
<!--version-->
<div class="form-group">
<label th:if="${#fields.hasErrors('version')}" th:errors="*{version}" class="validation-message"></label>
<input type="text" th:field="*{version}" placeholder="version" class="form-control"/>
</div>
<!--lock-->
<div class="form-group">
<label th:if="${#fields.hasErrors('lock')}" th:errors="*{lock}" class="validation-message"></label>
<input type="text" th:field="*{lock}" placeholder="null" class="form-control"/>
</div>
<div class="form-group">
<button type="submit" class="btn btn-primary btn-block">Create</button>
</div>
<span th:utext="${successMessage}"></span>
</form>
</div>
</div>
</body>
</html>
Last updated
Was this helpful?