springBoot(3)---整合jsp

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/sinat_23490433/article/details/89163956

本章介绍springBoot整合jsp,springBoot的视图层不推荐使用jsp,一般使用freemarker或thymeleaf

第一步、我们会发现新建的项目中没有webapp,没有WEB-INF,也没有web.xml等,如下图:

第二步、在src/main下新建webapp文件夹(注意webapp和resources、java是同级目录)

然后在webapp下新建WEB-INF文件夹,然后在WEB-INF下新建文件夹jsp,如下图:

第三步、鼠标移到项目,然后按ctrl+shift+alt+s快捷键,弹出如下界面

扫描二维码关注公众号,回复: 5864422 查看本文章

点击+号,添加web,显示如下

添加完后,如下:

第四步、我们会发现jsp文件夹下依然无法新建jsp页面,作如下修改即可

第五步、在jsp文件夹下新建hello.jsp,如下:

第六步、在application.properties中新增配置如下:

server.port=8080

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

  spring.mvc.view.suffix=.jsp

第七步、在com.cn.controller下新建HelloController,代码如下:

package com.cn.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Controller   
public class HelloController {
    @RequestMapping("/hello")
    public String hello(Model model){
        model.addAttribute("message","hello springBoot!");
        return "hello";
    }
}

第八步、测试

http://localhost:8080/hello

 

问题:出现${message}无法解析el表达式,此时需要在pom.xml中引入相关依赖包即可解决

<!-- 添加jsp依赖 -->
<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>

 

 

猜你喜欢

转载自blog.csdn.net/sinat_23490433/article/details/89163956
今日推荐