ViewController.java 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. package com.opengroupe.cloud.saas.web;
  2. import java.util.List;
  3. import org.springframework.beans.factory.annotation.Autowired;
  4. import org.springframework.stereotype.Controller;
  5. import org.springframework.ui.Model;
  6. import org.springframework.web.bind.annotation.RequestMapping;
  7. import org.springframework.web.bind.annotation.RequestParam;
  8. import com.fasterxml.jackson.core.JsonProcessingException;
  9. import com.fasterxml.jackson.databind.ObjectMapper;
  10. import com.opengroupe.cloud.saas.domain.Comment;
  11. import com.opengroupe.cloud.saas.service.CommentService;
  12. import com.opengroupe.cloud.saas.util.React;
  13. @Controller
  14. public class ViewController {
  15. @Autowired
  16. private CommentService service;
  17. @Autowired
  18. private ObjectMapper objectMapper;
  19. private React react = new React();
  20. @RequestMapping("/greeting")
  21. public String greeting(@RequestParam(value="name", required=false, defaultValue="World") String name, Model model) {
  22. model.addAttribute("name", name);
  23. return "greeting";
  24. }
  25. @RequestMapping("/index")
  26. public String index(Model model) throws JsonProcessingException {
  27. List<Comment> comments = service.getAll();
  28. String commentBox = react.renderCommentBox(comments);
  29. String data = objectMapper.writeValueAsString(comments);
  30. model.addAttribute("markup", commentBox);
  31. model.addAttribute("data", data);
  32. return "index";
  33. }
  34. }