若依框架整合JSP

今天接到组长任务要求Springboot+JSP完成页面渲染,因为是用的若依框架,一上午零零散散的找了很多资料和视频,没有达到理想的结果,在下午终于弄出来了,于是整理出来供大家查看引用。

(单纯Springboot项目怎么建可参考其他人新建Springboot项目)

另外用到了很多 JSTL标签,建议参考以下网站

菜鸟教程:JSP 标准标签库(JSTL) | 菜鸟教程 (runoob.com)

  1. 在ruoyi-common下的pom.xml文件中加入相关依赖

        <!-- JSP解析依赖-->
        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-jasper</artifactId>
        </dependency>

        <!-- jstl 标准标签库 c标签库-->
        <dependency>
            <groupId>jstl</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>

2.修改applicaiton.yml文件

(其中prefix 指的是存放jsp文件的路径;suffix指的是后缀)

spring:
    mvc:
        view:
            prefix: /WEB-INF/views
            suffix: .jsp

3. 在ruoyi-framework 模块config 包下新建视图解析类

package com.ruoyi.framework.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.view.InternalResourceViewResolver;

/**
 * 在 WebMvcConfigurer 中配置视图解析器
 * 若依框架默认使用的视图解析器是 Thymeleaf,可以通过以下方式添加 JSP 视图解析器
 *
 * 配置中,InternalResourceViewResolver 是 SpringMVC 自带的一个视图解析器,用于解析 JSP 视图。
 * setPrefix() 方法指定 JSP 文件所在的目录,
 * setSuffix() 方法指定 JSP 文件的后缀名。
 * 在使用 JSP 视图时,可以将视图名称设置为 JSP 文件名,例如 return "index.jsp",无需指定完整的 JSP 文件路径。
 */


@Configuration
public class WebMvcConfig implements WebMvcConfigurer {

    @Bean
    public InternalResourceViewResolver jspViewResolver() {
        InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
        viewResolver.setPrefix("/WEB-INF/views/");
        viewResolver.setSuffix(".jsp");
        return viewResolver;
    }

}

4. 在ruoyi-admin模块 src/main下新建webapp文件夹

4.1 补充:

新建的webapp不是网络类型,需要进行修改,操作如下

打开File->Project Structure (或快捷键Ctrl+Alt+Shift+S)下

4.2 在webapp下新建WEB-INF文件夹,再新建views文件夹

5. 在views文件夹下新建index.jsp文件

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Jsp</title>
</head>
<body>
<h1>01.jsp</h1>
<h1>${msg}</h1>
</body>
</html>

6. 编写controller类

@Controller
public class HelloController {
    @GetMapping("/hello")
    public String hello(Model model){
        //参数1:将要用到jsp文件中的名字
        //参数2:查询到的内容或对象
        model.addAttribute( "msg","Hello Spring boot + jsp" );
        return "hello";
    }
}

7. 访问相关地址

http://localhost:8080/hello

8. 测试成功

  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值