SpringBoot-整合JSP入门实例教程

     JSP也是模板的一种

       JSP在SpringBoot中虽然变得不再倡导使用,但是也经历了历史的验证,在公司大多数人员都使用jsp的情况下,还是继续使用比较稳妥,操作起来也比较方便,技术没有好与坏,能解决实际需求才是真理

1. 导入pom文件

 <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
        </dependency>
        <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>

 

2. 配置文件

# 指定目录 这个目录相对地址在src\main\webapp\下
spring.mvc.view.prefix: /WEB-INF/jsp/
# 指定文件格式
spring.mvc.view.suffix: .jsp

3.修改主启动类 继承-实现其一个方法 -完事 

/**
 * 主类修改继承自SpringBootServletInitializer
 */
@SpringBootApplication
public class SpringbootwebjspApplication extends SpringBootServletInitializer {

    /**
     * 实现父类方法
     * @param application
     * @return
     */
    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(SpringbootwebjspApplication.class);
    }

    public static void main(String[] args) {
        SpringApplication.run(SpringbootwebjspApplication.class, args);
    }
}

4.在main目录下新建一个目录-webapp\WEB-INF\jsp\ 并且新建一个jsp文件 

项目结构:
image

HelloWorld.jsp 文件代码

<!DOCTYPE html>
<html lang="en">
<body>
    <h1>Message: ${message}</h1>
    <h1>time: ${time}</h1>
</body>
</html>

5.HelloWorldController

/**
 * HelloWorldController层
 */
@Controller
public class HelloWorldController {

    private String message = "Hello World";

    @GetMapping("/")
    public String HelloWorld(Map<String, Object> model) {
        model.put("time", new Date());
        model.put("message", message);
        return "HelloWorld";
    }
}

6.启动,访问controller

image

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值