深入Spring Boot (十二):集成JSP
标签:
SpringBoot
代码实践
话不多说,直接上代码。依赖管理pom.xml,需要添加servlet和嵌入式tomcat运行jsp需要的jar,详细依赖配置如下:
<?xml version="1.0" encoding="UTF-8"?><project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>springboot-jsp</groupId> <artifactId>test</artifactId> <version>1.0-SNAPSHOT</version> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.0.RELEASE</version> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <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> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> </dependency> </dependencies></project>
因为jsp资源的默认存放路径是webapp,所以需要新建webapp目录,示例是将jsp文件放到webapp/WEB-INF/jsp路径下,因此在webapp下新建WEB-INF目录,接着在WEN-INF目录下新建jsp目录,在jsp下新建hello.jsp,完整目录结构及代码如下:
image
<!DOCTYPE html><%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %><%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %><html lang="en"> <body> <br> Message: ${message} </body></html>
在resources目录下新建application.properties,在其中添加jsp资源配置,具体配置如下:
spring.mvc.view.prefix=/WEB-INF/jsp/ spring.mvc.view.suffix=.jsp
新建HelloController.java,用于处理http请求,具体代码如下:
@Controller@EnableAutoConfigurationpublic class HelloController { @RequestMapping("/hello") public String hello(Map<String, Object> map) { map.put("message", "HelloWorld"); return "hello"; } public static void main(String[] args) throws Exception { SpringApplication.run(HelloController.class, args); } }
作者:JavaQ
链接:https://www.jianshu.com/p/6ce747e08478
点击查看更多内容
为 TA 点赞
评论
共同学习,写下你的评论
评论加载中...
作者其他优质文章
正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦