繁体   English   中英

在Spring Boot Thymeleaf控制器返回的视图中显示字符串

[英]Display a String in a view returned by a Spring Boot Thymeleaf controller

我在视图中挣扎,我需要在视图上显示API函数的返回值:

弹簧控制器代码

@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是一个API函数,它返回String

我需要在视图index.html上显示它。 非常感谢你。

如下更新您的方法:

@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;
}

我所做的修改是:用@RestController替换@Controller并更新@RequestMapping

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

在百里香

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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