这个问题也是困扰了我好久,各种找解决办法,终于成功了!所以记录下来,希望也能帮到各位。
集成步骤
1. 在pom.xml文件中配置依赖项
<!-- tomcat 依赖包 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
</dependency>
<!-- JSTL (JSP standard Tag Library) JSP 标准标签库 -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
2. 补全目录结构
框起来的是需要我们自己创建的文件目目录结构
注意:创建的webapp项目要和java目录在同一级
在创建完结构以后我的是需要自己手动配置一下web环境的,如果你的能够自己配置那就更好。步骤如下:
1)
2)
这个是添加web.xml的小加号点开的样子。检查是不是当前项目的web.xml路径。
这个是添加路径的小加号点开的样子。这个貌似直接ok就好了。
3. 在application.properties配置文件中配置前缀和后缀
spring.mvc.view.prefix=/WEB-INF/views/
spring.mvc.view.suffix=.jsp
4. 运行
运行不能用main方法直接运行了,需要使用命令的方式运行。命令为:mvn spring-boot:run
有三种运行方式:
1)用cmd方式运行,需要进入当前项目所在的文件目录,再用命令运行。
2)用idea的maven管理小窗口执行。
点击上面图片标记的数字3步骤后输入命令。然后回车就可以啦。
3)也是用idea的maven管理小窗口执行。
我运行成功的网页界面:
如果还是不成功,可以试一下在pom.xml文件中的build标签里加入以下路径配置。
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/**</include>
</includes>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.*</include>
</includes>
</resource>
<resource>
<directory>src/main/webapp</directory>
<targetPath>META-INF/resources</targetPath>
<includes>
<include>**/*.*</include>
</includes>
</resource>
</resources>
最后附上我的controller的代码
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class JSPController {
@RequestMapping("/index")
public String index(Model model){
model.addAttribute("msg","spring boot 集成 jsp");
return "index";
}
}