1.springboot对jsp的支持
1.在pom.xml添加依赖
<!--配置servlet--> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> </dependency> <!--配置jsp jstl的支持--> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> </dependency> <!--添加对tomcat的支持--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> <scope>provided</scope> </dependency> <!--对jsp的支持--> <dependency> <groupId>org.apache.tomcat.embed</groupId> <artifactId>tomcat-embed-jasper</artifactId> </dependency>
2.在src/main下创建webapp目录
3.在webapp下创建index.jsp
<%-- Date: 2018/8/11 Author:Jianxiong Yi --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> </head> <body> ${msg} </body> </html>
4.在application.properties配置springmvc访问后缀
#配置springmvc spring.mvc.view.prefix=/ spring.mvc.view.suffix=.jsp
5.编写controller
package cn.gobon.controller; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; /** * @version V1.0 * @ClassName: ValidateActionJson * @Description: TODO * @Auther: Jianxiong Yi * @Date: 2018/8/11 12:58 */ @Controller public class JspController { @RequestMapping("/jspTest") public String jspTest(Model model) { model.addAttribute("msg","hello spring boot"); return "index"; } }
6.启动App.java进行测试
此时访问报404错误,这是因为springboot在idea开发经常出现的问题,解决的方法是在pom.xml配置resources来指定编译后的jsp页面路径
<build> <plugins> <!-- springboot打包插件 --> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> <resources> <!-- 指定编译后的目录,在target目录下 --> <resource> <directory>src/main/java</directory> <includes> <include>**/*.xml</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> </build>
7.此时访问就正常了