首先这是个项目已经成功启动的标志,只是无法根据地址访问到指定的资源。
出现的原因可能是:
- Controller无法被扫描到。SpringBoot自动扫描启动类的同包或者其子包下的注解,需要将项目结构改成这样的或者在启动类中加Component注解。
@SpringBootApplication @ComponentScan("controller")//扫描Controller包以及其子包下的注解 public class Tmm { public static void main(String[] args) { SpringApplication.run(Tmm.class,args); } }
@SpringBootApplication = (默认属性)@Configuration + @EnableAutoConfiguration + @ComponentScan。由于这些注解一般都是一起使用,spring boot提供了一个统一的注解@SpringBootApplication。测试Controller是否被扫描到只需要在return前在控制台输出一下就可以测试。
- 进入Controller访问不到指定html文件。首先检查是否引入thymeleaf依赖
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency>
@ConfigurationProperties(prefix = "spring.thymeleaf") public class ThymeleafProperties { private static final Charset DEFAULT_ENCODING = Charset.forName("UTF-8"); private static final MimeType DEFAULT_CONTENT_TYPE = MimeType.valueOf("text/html"); public static final String DEFAULT_PREFIX = "classpath:/templates/"; public static final String DEFAULT_SUFFIX = ".html";
SpringBoot访问html和jsp页面需要引入这个依赖。依赖引入之后SpringBoot会默认到类路径(resources)下的templates中扫描。 thymeleaf模板引擎会将视扫描的前缀设置为类路径下的templates,后缀为.html