一步一步学springboot (三)使用jsp页面(视图)

来源:互联网 发布:大数据来源 编辑:程序博客网 时间:2024/06/05 03:42

springboot的Controller使用起来和springmvc一样。所以springmvc能做的事情都能做。下面我们一起配置一下使用jsp视图。

1.在pom,xml中添加依赖jar:

<!-- jsp 依赖 start -->        <dependency>      <groupId>org.apache.tomcat.embed</groupId>      <artifactId>tomcat-embed-jasper</artifactId>      <scope>provided</scope>  </dependency>  <!-- servlet 依赖 -->  <dependency>      <groupId>javax.servlet</groupId>      <artifactId>javax.servlet-api</artifactId>      <scope>provided</scope>  </dependency>  <!-- 添加 JSTL 支持 -->  <dependency>      <groupId>javax.servlet</groupId>      <artifactId>jstl</artifactId>  </dependency> <!-- jsp 依赖 end -->

2.在src/main/resources目录下新建application.properties文件并添加

spring.mvc.view.prefix=/WEB-INF/pages/
spring.mvc.view.suffix=.jsp

3.类和jsp如图:


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>${msg}</body></html>

4.测试,访问http://localhost:8080/demo3,即可

0 0