美文网首页
springboot配置thymeleaf与jsp共存

springboot配置thymeleaf与jsp共存

作者: HXin33 | 来源:发表于2022-05-20 16:56 被阅读0次

简述

原项目是springboot 使用默认的thymeleaf模板
新需求需要同时支持JSP代码

原有thymeleaf代码目录 src/main/resources/templates
JSP模板目录 src/main/webapp/WEB-INF/view/jsp

添加JSP集成

  1. 在pom.xml中添加支持jsp的相关依赖
     <!-- 在JSP页面中使用taglib指定引入jstl标签库 -->
      <dependency>
          <groupId>javax.servlet</groupId>
          <artifactId>jstl</artifactId>
      </dependency>
      <!-- 使用jsp引擎,springboot内置tomcat没有此依赖 -->
      <dependency>
          <groupId>org.apache.tomcat.embed</groupId>
          <artifactId>tomcat-embed-jasper</artifactId>
      </dependency>
  1. 重写方法对JSP视图进行配置
@Configuration
public class ViewResolverConfiguration {



    @Configuration//用来定义 DispatcherServlet 应用上下文中的 bean
    @EnableWebMvc
    /*@ComponentScan("com.mall.web.controller.test")*///扫描控制器组件
    public class WebConfig extends WebMvcConfigurerAdapter/*这个适配器已过时*/{

        @Value("${my.profile}")
        private String my_profile;

        private  final String[] CLASSPATH_RESOURCE_LOCATIONS = {
                "classpath:/META-INF/resources/", "classpath:/resources/",
                "classpath:/static/", "classpath:/public/"};



        @Bean
        public ViewResolver viewResolver() {
            InternalResourceViewResolver resolver = new InternalResourceViewResolver();
            resolver.setPrefix("/WEB-INF/view/");
            resolver.setSuffix(".jsp");
            resolver.setViewNames("jsp/*");//当控制器返回的viewName符合规则时才使用这个视图解析器
            resolver.setOrder(0);//设置优先级,数值越小优先级越高
            return resolver;
        }

        @Override
        public void addResourceHandlers(ResourceHandlerRegistry registry) {
            if (!registry.hasMappingForPattern("/webjars/**")) {
                registry.addResourceHandler("/webjars/**").addResourceLocations(
                        "classpath:/META-INF/resources/webjars/");
            }
            if (!registry.hasMappingForPattern("/**")) {
                registry.addResourceHandler("/**")
                        .addResourceLocations(CLASSPATH_RESOURCE_LOCATIONS)
                        .addResourceLocations("file:"+my_profile);
                  registry.addResourceHandler("/ewebeditor/**").addResourceLocations("ewebeditor/");
            }
        }
    }
}
  1. 创建测试controller
@Controller
@RequestMapping("/test/jsp/")
public class TestJspController extends BaseController
{
    private String prefix = "system/config";

    @Autowired
    private ISysConfigService configService;

    @GetMapping("/index")
    public String config(Model model)
    {
        model.addAttribute("data","welcome jsp");
        return "jsp/test";
    }

    @RequestMapping("/get1")
    @ResponseBody
    public String get() {
        return "hello get1";
    }

}
  1. 在src/main/webapp/WEB-INF/view/jsp目录下创建test.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<% request.setAttribute("base", request.getContextPath()); %>

<html>
<body>
    ${data}
</body>

</html>
  1. maven编译设置复制src/main/webapp目录下所有文件到META-INF/resources/下
    不然启动时找不到文件
<build>
        <resources>
      
            <resource>
                <directory>src/main/resources</directory>
                <includes>
                    <include>**/*.*</include>
                </includes>
            </resource>
            <resource>
                <directory>src/main/webapp</directory>
                <targetPath>META-INF/resources/</targetPath>
                <includes>
                    <include>**/*.*</include>
                </includes>
                <excludes>
                    <exclude>ewebeditor/**/*.*</exclude>
                </excludes>
            </resource>
        </resources>
    </build>
  1. 测试结果
    http://localhost:8080/test/jsp/index
    image.png

相关问题

  1. IDEA启动springboot项目,使用getServletContext().getRealPath()得到的是临时文件的路径,有的文件无法不会自动复制;
    直接修改配置 Working directory: $MODULE_WORKING_DIR$
image.png
  1. 程序包不存在的问题, mvn idea:idea


    image.png

3 jaxb相关jar系统找不到指定的文件
com\sun\xml\bind\jaxb-impl\2.1\jaxb-api.jar (系统找不到指定的文件。)
com\sun\xml\bind\jaxb-impl\2.1\activation.jar (系统找不到指定的文件。)
com\sun\xml\bind\jaxb-impl\2.1\jsr173_1.0_api.jar (系统找不到指定的文件。)
com\sun\xml\bind\jaxb-impl\2.1\jaxb1-impl.jar (系统找不到指定的文件。)

解决办法:
.1》在maven本地仓库中找到对应的jaxb-impl包
.2》解压出来找到META-INF/MANIFEST.MF
.3》删除里面的Class-Path: jaxb-api.jar activation.jar jsr173_1.0_api.jar jaxb1-impl.
jar这行
.4》再打包这个jar

image.png
image.png

相关文章

网友评论

      本文标题:springboot配置thymeleaf与jsp共存

      本文链接:https://www.haomeiwen.com/subject/gpifprtx.html