欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Spring Boot 整合Jsp

程序员文章站 2022-05-26 09:57:38
...

前提是Spring Boot工程已搭建完成,如果工程没有搭建,可以看我这篇博客。http://blog.csdn.net/u010889616/article/details/79561808

(1)添加gradle依赖

compile group: 'javax.servlet.jsp.jstl', name: 'jstl', version: '1.2'

runtime ("org.apache.tomcat.embed:tomcat-embed-jasper")


(2)配置Jsp页面路径

在application.properties文件,添加jsp路径

# 页面默认前缀目录
spring.mvc.view.prefix=/WEB-INF/jsp/

# 响应页面默认后缀
spring.mvc.view.suffix=.jsp


(3)新建index.jsp页面


Spring Boot 整合Jsp


页面内容如下

<%--
  Created by IntelliJ IDEA.
  User: wzj
  Date: 2016/10/8
  Time: 21:24
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Index</title>
</head>
<body>

    <div>你好</div>

    
</body>
</html>

(4)在Controller配置对于url

设置/index对应的页面的名称为index

package com.wzj.demo.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

/**
 * Created by wzj on 2018/3/14.
 */
@RestController
public class WelcomeController
{
    /**
     * 首页
     * @return 测试
     */
    @RequestMapping(value = "/welcome")
    @ResponseBody
    public String welcome()
    {
        return "Hello World";
    }

    @RequestMapping(value = "/index")
    public ModelAndView index(ModelAndView view)
    {
        //设置jsp名字
        view.setViewName("index");

        //传递数据
        view.addObject("name","张三");

        return view;
    }
}

(5)启动Spring Boot,浏览器输入http://127.0.0.1:8080/index

Spring Boot 整合Jsp

Github链接:https://github.com/HelloKittyNII/SpringBoot/tree/master/SpringBootDemo





相关标签: SpringBoot Jsp