大家好,我是 程序员码递夫。
1、问题
我们用java 开发web应用时,通常都有一个欢迎页(或者叫默认页),即用户访问网站时首先看到的页面,通常输入完域名后,浏览器就能呈现。
如果我们在网站根目录,存放有index.html ,
那么我们在浏览器打开网站,就可以看到index.html 的内容,如:
我的问题是,我的网站不想用 index.html 作为我的 默认页,我想要 index.jsp 。 于是我把 index.html 删除掉。留了个index.jsp ,
再次访问网站,却出现了错误页面。为什么会这样呢?
2、问题分析
我们看一下 springboot 对欢迎页的代码处理。
在 spring-boot-autoconfigure.jar 包里的 WebProperties 源代码里默认 会 在以下4个目录,查找资源文件(如index.html)
分别是
“classpath:/META-INF/resources/”,
“classpath:/resources/”,
“classpath:/static/”,
“classpath:/public/”
在 spring-boot-autoconfigure.jar 包里 WebMvcAutoConfiguration 类里有自动配置欢迎页的处理,
getWelcomePage 函数,首先在 上面的4个路径下查找 index.html 资源,如果找不到,则在 网站应用的根目录下继续查找index.html 资源,
在WelcomePageHandlerMapping 中对查找的结果进行判断处理
if (welcomePage != null && "/**".equals(staticPathPattern)) {
logger.info("Adding welcome page: " + welcomePage);
this.setRootViewName("forward:index.html");
} else if (this.welcomeTemplateExists(templateAvailabilityProviders, applicationContext)) {
logger.info("Adding welcome page template: index");
this.setRootViewName("index");
}
在这段代码中,如何欢迎页index.html资源没有找到,则查找 模版引擎是否配置了index,
springboot 搜索5个模板引擎,分别是 freemarker, mustache, groovy,thymeleaf, jsp
其中 jsp 模板处理 如下
public boolean isTemplateAvailable(String view, Environment environment, ClassLoader classLoader, ResourceLoader resourceLoader) {
if (ClassUtils.isPresent("org.apache.jasper.compiler.JspConfig", classLoader)) {
String resourceName = this.getResourceName(view, environment);
if (resourceLoader.getResource(resourceName).exists()) {
return true;
}
try {
return (new File("src/main/webapp", resourceName)).exists();
} catch (AccessControlException var7) {
}
}
return false;
}
private String getResourceName(String view, Environment environment) {
String prefix = environment.getProperty("spring.mvc.view.prefix", "");
String suffix = environment.getProperty("spring.mvc.view.suffix", "");
return prefix + view + suffix;
}
分析源代码,可看到,如果你 没有 配置 spring.mvc.view.prefix 和 spring.mvc.view.suffix, 通常是装载不了jsp 文件的。
其资源装载顺序是 前面提到的 “classpath:/META-INF/resources/”, “classpath:/resources/”, “classpath:/static/”, “classpath:/public/”, “/”, 5个目录,如何都不找到,则 搜索"src/main/webapp" 目录,如果还是找不到就会显示 Whitelabel Error Page 错误页。
看到这里我们应该知道了,要想 欢迎页用jsp显示,必须满足以下条件:
1)欢迎页的名称必须是 index.jsp
2)index.jsp必须放置在正确的路径下
3)application.yml 必须 配置有 spring.mvc.view.suffix 即jsp后缀
此外还有1个条件就是
4)POM.xml 需要配置 加载相关的 jsp 依赖库
3、问题解决
3.1、修改application.yml ,加入相关配置
如果你的 欢迎页 不在 网站应用的根目录,则还要 配置 prefix 指出相应的目录位置。
3.2、修改pom.xml,加入相关配置
<!-- Spring Boot Starter Web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- Spring Boot Starter Tomcat (for JSP rendering) -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<!-- JSP API -->
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>javax.servlet.jsp-api</artifactId>
<version>2.3.3</version>
<scope>provided</scope>
</dependency>
<!-- Tomcat JSP Engine -->
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
3.3、运行结果
打开网页,终于看到我们默认的
4、如果经过以上修改你还没有解决问题
尝试加一个 Controller,并把 “/” 映射到你要指向的欢迎页中
本文结束,希望对你有帮助。