springboot 入门(二) 整合jsp页面

1.新建springboot工程,默认选择springboot 2.0,添加webapp文件夹并指定web resource


2.修改springboot的配置文件

#指定端口号
server.port=8888
#指定视图解析路径前缀
spring.mvc.view.prefix=/WEB-INF/jsp/
#指定视图解析后缀
spring.mvc.view.suffix=.jsp

3.添加springboot 整合jsp的依赖

<!--添加springboot整合jsp插件-->
<dependency>
   <groupId>org.apache.tomcat.embed</groupId>
   <artifactId>tomcat-embed-jasper</artifactId>
   <!--<scope>provided</scope> 不需要指定scope-->
</dependency>

4.新建controller类,实现jsp页面跳转处理

/**
 * 自定义controller实现自定义jsp页面的跳转
 */
@Controller
public class IndexController {
    @RequestMapping("/page")
    public ModelAndView index(){
        System.out.println("请求进来了...");
        ModelAndView model = new ModelAndView("index");
        model.addObject("time",new Date());
        return model;
    }
}

5.jsp页面及效果

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>jsp页面</title>
</head>
<body>
    <h1>${time }</h1>
</body>
</html>

6.注意

这里有点疑注意,因为springboot有三种启动方式,此处是使用main启动才需要配置整合jsp的依赖,如果是使用mvn spring-boot:run的命令就不需要配置依赖

7.github地址:springboot整合jsp

猜你喜欢

转载自blog.csdn.net/u010005147/article/details/79750106