Spring Boot 初体验(11)添加JSP支持
Posted kuangyefeige
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Spring Boot 初体验(11)添加JSP支持相关的知识,希望对你有一定的参考价值。
第一步:配置 application.properties
######################################################## ### JSP的支持 ######################################################## # 页面默认前缀目录 spring.mvc.view.prefix=/WEB-INF/jsp/ # 响应页面默认后缀 spring.mvc.view.suffix=.jsp # 自定义属性,可以在Controller中读取 application.hello=Hello Angel From application
第二步:添加相关依赖
<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/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>SpringBoot_001</groupId> <artifactId>SpringBoot_001</artifactId> <packaging>war</packaging> <version>0.0.1-SNAPSHOT</version> <name>SpringBoot_001 Maven Webapp</name> <url>http://maven.apache.org</url> <!-- 继承父包 --> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.4.1.RELEASE</version> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <java.version>1.7</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.hibernate</groupId><!--解决只引用spring-boot-starter-web时启动缺少JPA validator的Bug --> <artifactId>hibernate-validator</artifactId> <version>5.3.0.Final</version> </dependency> <!-- https://mvnrepository.com/artifact/com.alibaba/fastjson --> <dependency><!-- fastjson依赖 --> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.47</version> </dependency> <dependency><!-- devtools依赖 --> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <optional>true</optional> <scope>true</scope> </dependency> <dependency><!-- oracle依赖 --> <groupId>com.oracle</groupId> <artifactId>ojdbc7</artifactId> <version>12.1.0.2</version> </dependency> <dependency><!-- data jpa配置信息 --> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <!-- thymeleaf的依赖: --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> <!-- freemarker依赖 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-freemarker</artifactId> </dependency> <!-- Jsp支持 --> <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> <dependency> <groupId>org.apache.tomcat.embed</groupId> <artifactId>tomcat-embed-jasper</artifactId> <scope>provided</scope> </dependency> </dependencies> <build> <finalName>SpringBoot_001</finalName> <plugins> <plugin><!-- devtools构建依赖 --> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <!-- <configuration> 如果devtools不起作用,加上此项配置 <fork>true</fork> </configuration> --> </plugin> </plugins> </build> </project>
第三步:写代码测试
controller:
package com.mt.controller; import java.util.Date; import java.util.Map; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; @Controller public class HelloJspController { @RequestMapping("/helloJsp") public String hello(Map<String, Object> map) { map.put("hello", new Date()); return "hello"; } }
JSP:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> helloJsp <hr> ${hello} </body> </html>
工程目录截图
以上是关于Spring Boot 初体验(11)添加JSP支持的主要内容,如果未能解决你的问题,请参考以下文章
Spring Boot 初体验(10)使用使用freemarker
215.Spring Boot+Spring Security:初体验
215.Spring Boot+Spring Security:初体验
小马哥-Java 微服务实践 - Spring Boot 系列-01Java 微服务实践 - Spring Boot 系列初体验