有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java向Spring Boot应用程序添加页面(非常基本)

我有一个spring启动api。我的问题是如何在我的应用程序中创建页面

我在/resources/static/templates/index中创建了一个页面。html

当我到达/api/lol时,我只看到一个字符串表示索引,而不是页面

我该怎么做

@RequestMapping("/api")
@RestController
public class Controller {

    @Autowired
    JdbcTemplate jdbcTemplate;

    @GetMapping("/lol")
    String lol() {            
          return "index";
    }
}

共 (2) 个答案

  1. # 1 楼答案

    把你的索引放进去。html在src/main/resources/static

    然后在你的控制器里

    @RequestMapping("/api")
    @RestController
    public class Controller {
    
        @Autowired
        JdbcTemplate jdbcTemplate;
    
        @GetMapping("/lol")
        String lol() {            
              return "index.html";
        }
    }
    
  2. # 2 楼答案

    你用@RestController注释了你的类
    你想要的是一个MVC控制器,所以用@Controller替换它

    下面是documentation的一个例子:

    @Controller
    public class HelloController {
    
        @GetMapping("/hello")
        public String handle(Model model) {
            model.addAttribute("message", "Hello World!");
            return "index";
        }
    }