【问题标题】:Coexistence of both thymeleaf and jasper files in Spring Boot applicationSpring Boot 应用程序中 thymeleaf 和 jasper 文件共存
【发布时间】:2018-05-02 07:18:37
【问题描述】:

我试过,在一个项目中jasper和thymeleaf都可以,但是不能共存,因为我想用jsp必须注释掉spring-boot-starter-thymeleaf依赖的包,这样才能运行。寻找一种解决方案,以便 jasper 和 thymeleaf 可以共存。如果有人使用 servlet-context.xml (Mixing thymeleaf and jsp files in Spring Boot),我得到了关于 stackoverflow 的解决方案,其中 jasper 和 thymeleaf 共存。但我的要求是,如果我使用的是 spring-boot-starter-web,如何在 pom.xml 中包含这些属性。

【问题讨论】:

    标签: spring-boot jasper-reports thymeleaf spring-boot-maven-plugin spring-boot-starter


    【解决方案1】:

    我能够从 Spring Boot 中的嵌入式 jar 构建运行 HTML 和 JSP 页面。但是如果您想通过在命令提示符中复制 Jar 来独立运行它,那么您需要复制 JSP 页面文件夹结构,因为它不会在 jar 内容中,您需要稍微更改 pom 文件以便 jar 可以添加外部内容。

    第 1 步:添加 Thymeleaf 和 JSP 依赖项 将以下依赖项添加到您的 pom.xml 文件中

    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-thymeleaf</artifactId>
    </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>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-tomcat</artifactId>
      <scope>provided</scope>
    </dependency>
    

    第 2 步:项目结构和文件创建

    在源文件夹 src/main/resources 下创建文件夹模板,在该文件夹下创建子文件夹 thymeleaf。并创建一个html文件sample.html(say)

    <!DOCTYPE html>
    <html lang="en" xmlns:th="http://www.thymeleaf.org">
    <head>
      <title>Hello</title>
    </head>
    <body>
      THYMELEAF PAGE: <p th:text="${name}"></p>
    </body>
    </html>
    

    在 src/main/webapp/WEB-INF 创建子文件夹视图。在views下创建一个jsp文件,sample.jsp(say)

    <!DOCTYPE html>
    <html lang="en">
    <head>
      <title>Hello</title>
    </head>
    <body>
       JSP PAGE: Hello ${name}
    </body>
    </html>
    

    第 3 步:在您的 application.properties 中设置 thymeleaf 视图名称和用于内部视图解析的 JSP 配置。

    #tomcat-connection settings
    spring.datasource.tomcat.initialSize=20
    spring.datasource.tomcat.max-active=25
    #Jasper and thymeleaf configaration
    spring.view.prefix= /WEB-INF/
    spring.view.suffix= .jsp
    spring.view.view-names= views
    spring.thymeleaf.view-names= thymeleaf
    #Embedded Tomcat server 
    server.port = 8080
    #Enable Debug
    debug=true
    management.security.enabled=false
    

    第 4 步:创建用于服务 Thymeleaf 和 JSP 页面的控制器:

    package com.example.controller;
    
    import org.springframework.stereotype.Controller;
    import org.springframework.ui.Model;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RequestParam;
    
    @Controller
    public class TestController {
    
        @RequestMapping(value="/jasper", method=RequestMethod.GET)
        public String newjasper(Map<String, Object> m, String name){
            //System.out.print("-- INSIDE JSP CONTROLER ------");
            m.put("name", name);
            return "views/sample";
        }
    
        @RequestMapping(value="/thymeleaf", method=RequestMethod.GET)
        public String newthymeleaf(Map<String, Object> m, String name){
            //System.out.print("-- INSIDE HTML CONTROLER ------");
            m.put("name", name);
            return "thymeleaf/sample";
        } 
    
    }
    

    第 5 步:在某些情况下,您可能需要创建一个配置类 SpringConfig.class(例如)用于 JSP 页面的视图解析。但可选,我不在我的配置文件中使用它。

    import org.springframework.web.servlet.view.JstlView;
    
    @Configuration
    public class SpringConfig {
    @Value("${spring.view.prefix}")
    private String prefix;
    
    @Value("${spring.view.suffix}")
    private String suffix;
    
    @Value("${spring.view.view-names}")
    private String viewNames;
    
    @Bean
    InternalResourceViewResolver jspViewResolver() {
        final InternalResourceViewResolver viewResolver = new 
        InternalResourceViewResolver();
        viewResolver.setPrefix(prefix);
        viewResolver.setSuffix(suffix);
        viewResolver.setViewClass(JstlView.class);
        viewResolver.setViewNames(viewNames);
        return viewResolver;
     }
    }
    

    第 6 步:测试 jsp 和 html 的应用程序。

    当您在浏览器中点击此网址时:http://localhost:8080/thymeleaf?name=rohit。这将打开我们的 sample.html 文件,其参数名称位于页面中心,并使用此 url:http://localhost:8080/jasper?name=rohit 将打开 sample.jsp 页面,其参数名称位于页面中心。

    【讨论】:

      【解决方案2】:

      来自viewresover javadoc。

      指定一组名称模式,用于确定是否 控制器返回的视图名称将由此解析器解析 或不。

      在配置多个视图解析器的应用程序中——例如,一个 对于 Thymeleaf 和另一个用于 JSP+JSTL 遗留页面——,这个 属性确定视图何时被认为是由 此视图解析器以及 Spring 何时应该简单地询问下一个解析器 在链中——根据它的顺序——代替。

      指定的视图名称模式可以是完整的视图名称,但可以 还可以使用 * 通配符:“index.”、“user_”、“admin/*”等。

      另请注意,这些视图名称模式在应用之前会被检查 视图名称的任何前缀或后缀,因此它们不应包含 这些。因此,通常您会指定 orders/* 而不是 /WEB-INF/templates/orders/*.html。

      指定视图的名称——实际上是模式——不能被 这个视图解析器。

      这些模式可以用与 setViewNames(String []),但作为排除列表工作。 viewResolver.setViewNames(viewNames);

      【讨论】:

        猜你喜欢
        • 2016-11-23
        • 2019-03-12
        • 1970-01-01
        • 2021-03-28
        • 1970-01-01
        • 2023-03-22
        • 2018-07-02
        • 1970-01-01
        • 2017-07-16
        相关资源
        最近更新 更多