SpringBoot基础-如何在springboot中使用jsp
2018-04-02 20:31
597 查看
视图支持
Springboot的默认视图支持是Thymeleaf,但是Thymeleaf我们不熟悉,我们熟悉的还是jsp。 所以本知识点讲解如何让Springboot支持 jsp。
基于前面的知识点
本知识点前面的基础上进行修改
pom.xml
增加对JSP支持
application.properties
在src目录下增加 application.properties文件,用于视图重定向jsp文件的位置
HelloController
修改HelloController,把本来的@RestController 改为@Controller。
这时返回"hello"就不再是字符串,而是根据application.properties 中的视图重定向,到/WEB-INF/jsp目录下去寻找hello.jsp文件
hello.jsp
在main目录下,新建-> webapp/WEB-INF/jsp 目录。
随后新建hello.jsp 文件,在其中使用EL 表达式显示放在HelloController的model中的当前时间。
启动测试
测试地址是:
Springboot的默认视图支持是Thymeleaf,但是Thymeleaf我们不熟悉,我们熟悉的还是jsp。 所以本知识点讲解如何让Springboot支持 jsp。
基于前面的知识点
本知识点前面的基础上进行修改
pom.xml
增加对JSP支持
<?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>com.how2java</groupId> <artifactId>springboot</artifactId> <version>0.0.1-SNAPSHOT</version> <name>springboot</name> <description>springboot</description> <packaging>war</packaging> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.9.RELEASE</version> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web&l 4000 t;/artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> <scope>provided</scope> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <!-- servlet依赖. --> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <scope>provided</scope> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> </dependency> <!-- tomcat的支持.--> <dependency> <groupId>org.apache.tomcat.embed</groupId> <artifactId>tomcat-embed-jasper</artifactId> <scope>provided</scope> </dependency> </dependencies> <properties> <java.version>1.8</java.version> </properties> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
application.properties
在src目录下增加 application.properties文件,用于视图重定向jsp文件的位置
spring.mvc.view.prefix=/WEB-INF/jsp/ spring.mvc.view.suffix=.jsp
HelloController
修改HelloController,把本来的@RestController 改为@Controller。
这时返回"hello"就不再是字符串,而是根据application.properties 中的视图重定向,到/WEB-INF/jsp目录下去寻找hello.jsp文件
@Controller public class HelloController { @RequestMapping("/hello") public String hello(Model m) { m.addAttribute("now", DateFormat.getDateTimeInstance().format(new Date())); return "hello"; } }
hello.jsp
在main目录下,新建-> webapp/WEB-INF/jsp 目录。
随后新建hello.jsp 文件,在其中使用EL 表达式显示放在HelloController的model中的当前时间。
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> Hi JSP. 现在时间是 ${now}
启动测试
测试地址是:
http://127.0.0.1:8080/hello
相关文章推荐
- 如何在SpringBoot中使用JSP ?但强烈不推荐,果断改Themeleaf吧
- 如何在SpringBoot中使用JSP ?但强烈不推荐,果断改Themeleaf吧
- 如何在SpringBoot中使用JSP ?但强烈不推荐,果断改Themeleaf吧
- JSP设计模式基础:View Helper模式——学习如何使用View Helper模式使得Model数据适应表现层的需要(2)
- maven中使用springboot返回jsp和json数据
- 市长信箱邮件查询服务: 使用SpringBoot搭建基础
- Spring-Boot中如何使用多线程处理任务
- Spring学习之使用标签来标记资源(@Component、@Repository、 @Service和@Controller)以及使用方式(包含如何在jsp中使用)
- JSP设计模式基础:View Helper模式——学习如何使用View Helper模式使得Model数据适应表现层的需要(3)
- 如何在Spring Boot项目使用参数校验
- Spring-Boot中如何使用多线程处理任务
- 如何在Spring Boot项目使用参数校验
- JSP设计模式基础:View Helper模式——学习如何使用View Helper模式使得Model数据适应表现层的需要(2)
- 在springboot项目中使用jsp视图
- spring-boot+mybatis开发实战:如何在spring-boot中使用myabtis持久层框架
- Spring学习使用标签来标记资源(@Component、@Repository、 @Service和@Controller)和用法(包括如何jsp正在使用)
- ssh整合系列博客——(3)在jsp中如何使用spring管理的bean
- java /Spring /jsp 使用input标签 file 如何上传多个文件
- spring-boot+mybatis开发实战:如何在spring-boot中使用myabtis持久层框架
- Spring Boot基础教程8-web应用开发-模板引擎jsp