开发喵星球

若依同时集成jsp和thymeleaf(六十四)

对应目前来说,新项目一般很少会使用jsp这项技术,但是老项目可能是有jsp开发的,这时候可能就会有这么一个需求,就是将老项目的代码移植到新框架上。本篇教程主要介绍,如何整合jsp和thymeleaf同时存在。经过测试,已完美整合。

一、添加jsp依赖

在ruoyi-framework/pom.xml文件中添加jsp依赖

需要将ruoyi-admin/pom.xml文件中的spring-boot-starter-thymeleaf依赖先删除。


<!--jsp--> <!-- SpringBoot集成thymeleaf模板 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</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> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> </dependency> <!--jsp-->

二、增加配置文件

在ruoyi-admin/src/main/resources/application.yml,否则启动会报错

# Spring配置
spring:
  main:
    allow-bean-definition-overriding: true

三、增加配置java类

(1)WebResourceConfig.java
ruoyi-framework/src/main/java/com/ruoyi/framework/config/WebResourceConfig.java
“`java
package com.ruoyi.framework.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

@Configuration
public class WebResourceConfig extends WebMvcConfigurerAdapter {

@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
    //配置静态资源处理
    registry.addResourceHandler("/**")
            .addResourceLocations("resources/", "static/", "public/",
                    "META-INF/resources/")
            .addResourceLocations("classpath:resources/", "classpath:static/",
                    "classpath:public/", "classpath:META-INF/resources/")
            .addResourceLocations("file:///tmp/webapps/");
}

}

(2)JspConfig.java
ruoyi-framework/src/main/java/com/ruoyi/framework/config/JspConfig.java
```java
package com.ruoyi.framework.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.accept.ContentNegotiationManager;
import org.springframework.web.context.request.RequestContextListener;
import org.springframework.web.servlet.View;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.view.ContentNegotiatingViewResolver;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import org.springframework.web.servlet.view.json.MappingJackson2JsonView;
import org.thymeleaf.spring5.SpringTemplateEngine;
import org.thymeleaf.spring5.templateresolver.SpringResourceTemplateResolver;
import org.thymeleaf.spring5.view.ThymeleafViewResolver;
import org.thymeleaf.templatemode.TemplateMode;
import org.thymeleaf.templateresolver.ClassLoaderTemplateResolver;
import org.thymeleaf.templateresolver.ITemplateResolver;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

@Configuration
public class JspConfig {

    @Bean
    public RequestContextListener requestContextListenerBean() {
        return new RequestContextListener();
    }

    @Bean
    public ViewResolver contentNegotiatingViewResolver(ContentNegotiationManager manager) {
        ContentNegotiatingViewResolver resolver = new ContentNegotiatingViewResolver();
        resolver.setContentNegotiationManager(manager);
        List<ViewResolver> resolvers = new ArrayList<ViewResolver>();
        resolvers.add(jsonViewResolver());
        resolvers.add(jspViewResolver());
        resolvers.add(thymeleafViewResolver());
        resolver.setViewResolvers(resolvers);
        return resolver;
    }

    @Bean
    public ViewResolver jsonViewResolver() {
        return new ViewResolver() {
            @Override
            public View resolveViewName(String viewName, Locale locale) throws Exception {
                MappingJackson2JsonView view = new MappingJackson2JsonView();
                view.setPrettyPrint(true);
                return view;
            }
        };
    }


    @Bean
    public ViewResolver jspViewResolver() {
        InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
        viewResolver.setPrefix("/WEB-INF/jsp/");
        viewResolver.setSuffix(".jsp");
        viewResolver.setContentType("text/html");
        viewResolver.setOrder(2);
        return viewResolver;
    }

    @Bean
    public ViewResolver thymeleafViewResolver() {

        ThymeleafViewResolver viewResolver = new ThymeleafViewResolver();

        viewResolver.setTemplateEngine(thymeleafTemplateEngine());
        viewResolver.setCharacterEncoding("UTF-8");
        viewResolver.setOrder(0);
        List<String> list = new ArrayList<>();
        list.add("demo/**");
        list.add("error/**");
        list.add("monitor/**");
        list.add("system/**");
        list.add("tool/**");
        list.add("include");
        list.add("index");
        list.add("index-topnav");
        list.add("lock");
        list.add("login");
        list.add("main");
        list.add("main_v1");
        list.add("register");
        list.add("skin");
        String[] viewNames = list.stream().toArray(String[]::new);
        viewResolver.setViewNames(viewNames);
        return viewResolver;
    }

    @Bean
    public SpringTemplateEngine thymeleafTemplateEngine() {
        SpringTemplateEngine templateEngine = new SpringTemplateEngine();
        templateEngine.setTemplateResolver(thymeleafTemplateResolver());
        templateEngine.setEnableSpringELCompiler(true);
        return templateEngine;
    }

    @Bean
    public SpringResourceTemplateResolver springResourceTemplateResolver() {
        return new SpringResourceTemplateResolver();
    }

    @Bean
    public ITemplateResolver thymeleafTemplateResolver() {
        ClassLoaderTemplateResolver templateResolver = new ClassLoaderTemplateResolver();
        templateResolver.setPrefix("templates/");
        templateResolver.setCacheable(false);
        templateResolver.setSuffix(".html");
        templateResolver.setTemplateMode(TemplateMode.HTML);
        templateResolver.setCharacterEncoding("UTF-8");
        return templateResolver;
    }
}

四、增加JSP测试文件

(1)HelloJspController.java
ruoyi-admin/src/main/java/com/ruoyi/jsp/HelloJspController.java

package com.ruoyi.jsp;

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

@RequestMapping("/hello")
@Controller
public class HelloJspController {

    @GetMapping("/jsp")
    public String hello(Model model) {
        model.addAttribute("msg", "若依集成JSP成功");
        return "jsp_index";
    }
}

(2)jsp_index.jsp
ruoyi-admin/src/main/resources/META-INF/resources/WEB-INF/jsp/jsp_index.jsp

需要自己新建目录:META-INF/resources/WEB-INF

<%@ page contentType="text/html;charset=utf-8" language="java" %>
<html>
<head>
    <title></title>
</head>
<body>
<h3>{msg}</h3>
</body>
</html>

(3)web.xml
ruoyi-admin/src/main/resources/META-INF/resources/WEB-INF/web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
  <display-name>若依管理系统</display-name>
  <welcome-file-list>
        <welcome-file>/index</welcome-file>
    </welcome-file-list>
</web-app>

五、启动项目验证结果
访问Thymeleaf页面

访问JSP页面

   
分类:Java/OOP 作者:无限繁荣, 吴蓉 发表于:2023-12-11 14:04:11 阅读量:121
<<   >>


powered by kaifamiao