SHARE
TWEET

Untitled

a guest Jun 25th, 2019 63 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. public class AthleteService {
  2.  
  3.     private ObjectMapper mapper = new ObjectMapper();
  4.     private File jsonFile = new File("jsondatabase/result.json");
  5.     private List<Athlete> resultList = new ArrayList<>();
  6.  
  7.     @ModelAttribute("results")
  8.     public List<Athlete> findResultByEventId(Integer id) {
  9.         try {
  10.             JsonNode root = mapper.readTree(jsonFile);
  11.             JsonNode results = root.get("event_" + id);
  12.                 for (JsonNode resultNode : results.get("results")) {
  13.                     Athlete athlete = new Athlete(resultNode.get("rank").asInt(), resultNode.get("country").asText(),
  14.                             resultNode.get("athlete").asText(), resultNode.get("result").asInt(),
  15.                             resultNode.get("points").asInt());
  16.                     resultList.add(athlete);
  17.             }
  18.         } catch (IOException e) {
  19.             e.printStackTrace();
  20.         }
  21.         return resultList;
  22.     }
  23.      
  24. @Controller
  25. public class PageController {
  26.     @Autowired
  27.     private AthleteService athleteService;
  28.  
  29.     @RequestMapping(value = "/result/{id}", method = RequestMethod.GET)
  30.     public String resultPage(@PathVariable("id") Integer id, Model model){
  31.         model.addAttribute("results", athleteService.findResultByEventId(id));
  32.         return "result";
  33.     }
  34.     }
  35.      
  36. <body>
  37. <div th:if="${not #lists.isEmpty(results)}">
  38.     <h2 th:text="${event}"> </h2>
  39. <table class="table-striped">
  40.     <thead>
  41.     <tr style="background-color: lightgrey">
  42.         <th>Rank</th>
  43.         <th>Country</th>
  44.         <th>Athlete</th>
  45.         <th>Time</th>
  46.         <th>Points</th>
  47.     </tr>
  48.     </thead>
  49.     <tbody>
  50.     <tr th:each="result: ${results}">
  51.         <td th:value="${result.rank}">Rank</td>
  52.         <td th:text="${result.country}">Country</td>
  53.         <td th:text="${result.athlete}">Athlete</td>
  54.         <td th:value="${result.time}">Time</td>
  55.         <td th:value="${result.points}">Points</td>
  56.     </tr>
  57.     </tbody>
  58. </table>
  59. </div>
  60. </body>
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top