Spring Boot整合JSP

Spring Boot整合JSP

jsp:Java Server Page,是java提供的一种动态网页技术,底层是servlet,可以直接在HTML中插入java代码

  • JSP底层原理

JSP是一种中间层组件,开发者可以在这个组件中将java代码与HTML代码进行整合,由JSP引擎将组件转换为Servlet,再把开发者自定义在组件中的混合代码翻译成Servlet的相应语句,输出给客户端。

1,创建基于maven的web项目

</parent>
  <dependencies>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
      <groupId>org.apache.tomcat.embed</groupId>
      <artifactId>tomcat-embed-jasper</artifactId>
      <version>9.0.19</version>
    </dependency>
  </dependencies>

2.handler

package com.southwind.comtroller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping("/hello")
public class HelloHandler {
    @GetMapping("/index")
    public ModelAndView index(){
        ModelAndView modelAndView =new ModelAndView();
        modelAndView.setViewName("index");
        modelAndView.addObject("mess","hello,Spring Boot!");
        return modelAndView;
    }
}

3.jsp

<%--
  Created by IntelliJ IDEA.
  User: DELL
  Date: 2023/2/20
  Time: 16:44
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@page isELIgnored="false" %>
<html>
<head>
    <title>Title</title>

</head>
<body>
${mess}
</body>
</html>

4.Application.yml中配置视图解析器.

server:
  port: 8081
spring:
  mvc:
    view:
      profix: /
      suffix: .jsp

5.Application启动类

package com.southwind.comtroller;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.stereotype.Component;

@SpringBootApplication

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

posted on 2023-02-20 17:42  张铁蛋666  阅读(112)  评论(0编辑  收藏  举报

导航