简体   繁体   中英

Display a String in a view returned by a Spring Boot Thymeleaf controller

I am struggling with my view, I need to display the return of my API function on my view:

Spring Controller Code

@Controller
public class mainController {

  @RequestMapping(value = {"/"}, method = RequestMethod.GET)
  public String index() throws IOException, GeneralSecurityException {
    DriveQuickstart drive = new DriveQuickstart("c:/temp/credentials.json");
    String res = drive.checkFile("cwg");

    return res;

drive.checkFile is an API function which returns a String .

I need to display it on my view index.html . thank you very mutch.

Update your method as below:

@RestController
public class mainController {

  @RequestMapping(value = "/", method = RequestMethod.GET)
  public String index() throws IOException, GeneralSecurityException {
    DriveQuickstart drive = new DriveQuickstart("c:/temp/credentials.json");
    String res = drive.checkFile("cwg");

    return res;
}

The modification i did is: replaced @Controller with @RestController and updated @RequestMapping

@GetMapping("/")
public String index(Model model) throws IOException, GeneralSecurityException {
        DriveQuickstart drive = new DriveQuickstart("c:/temp/credentials.json");
        model.addAttribute("drive", drive)
    return "/routeToTemplate";
    }

in thymeleaf

<div th:text="${drive}"></div>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM