spring boot thymeleaf和jsp可以共存吗

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了spring boot thymeleaf和jsp可以共存吗相关的知识,希望对你有一定的参考价值。

参考技术A thymeleaf 与 jsp 同时使用 for spring mvc

spring-mvc.xml

[html] view plain copy
<!-- **************************************************************** -->
<!-- THYMELEAF-SPECIFIC ARTIFACTS -->
<!-- TemplateResolver <- TemplateEngine <- ViewResolver -->
<!-- **************************************************************** -->

<bean id="templateResolver"
class="org.thymeleaf.templateresolver.ServletContextTemplateResolver">
<property name="prefix" value="/WEB-INF/" />
<property name="suffix" value=".html" />
<property name="templateMode" value="HTML5" />
<!-- Template cache is true by default. Set to false if you want -->
<!-- templates to be automatically updated when modified. -->
<property name="cacheable" value="# systemProperties['thymeleaf.cacheable'] == 'false' ? false : true " />
<property name="characterEncoding" value="utf-8"/>
</bean>

<bean class="org.thymeleaf.spring4.view.ThymeleafViewResolver">
<property name="templateEngine">
<bean class="org.thymeleaf.spring4.SpringTemplateEngine">
<property name="templateResolver" ref="templateResolver" />
</bean>
</property>
<property name="viewNames" value="thymeleaf/*"/>
<property name="order" value="0"/>
</bean>

<!-- jsp resolver -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/"/>
<property name="suffix" value=".jsp"/>
<property name="viewNames" value="jsps/*"/>
<property name="order" value="1"/>
<property name="cache" value="# systemProperties['jsp.cacheable'] == 'false' ? false : true "/>
</bean>
加两个属性,

<property name="viewNames" value="thymeleaf/*"/>
<property name="viewNames" value="jsps/*"/>
ViewResolver 会根据 viewName 选择不同处理器。

另外开发时禁用红缓存,每次重新解析模板,改动立即生效。

<property name="cacheable" value="# systemProperties['thymeleaf.cacheable'] == 'false' ? false : true " />本回答被提问者采纳

Spring boot:如何将thymeleaf转换为jsp

【中文标题】Spring boot:如何将thymeleaf转换为jsp【英文标题】:Spring boot: How to convert thymeleaf to jsp 【发布时间】:2019-03-25 14:27:33 【问题描述】:

我最近学习了一个使用 Spring Boot 的身份验证和授权教程。它工作正常,我想将它集成到我现有的项目中。我遵循的教程是用 thymeleaf 编写的,我当前的项目是用 jsp 编写的。我尝试在线搜索,但它给了我错误。

这是我的代码:

控制器

@RequestMapping(value =  "/","/welcome" , method = RequestMethod.GET)
public ModelAndView test() 
    ModelAndView model = new ModelAndView("welcomePage");
    model.addObject("title","Welcome");
    model.addObject("message","This is welcome page");
    return model;

百里香

<a th:if="$#request.userPrincipal != null" th:href="@/logout">Logout</a>

JSP

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://java.sun.com/jstl/core" prefix="c" %>

<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>

    <div>
        <a href="/">Home</a> | &nbsp; 
        <a href="/userInfo">User Info </a> | &nbsp; 
        <a href="/admin">Admin</a> | &nbsp; 

        <c:if test="$pageContext.request.userPrincipal != null">
            <h2>Logout</h2> 
            <br>
        </c:if>


        $pageContext.request.userPrincipal



    </div>

</body>
</html>

pom.xml

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-jdbc</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-security</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.security</groupId>
        <artifactId>spring-security-test</artifactId>
        <scope>test</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>
    <dependency>
        <groupId>jstl</groupId>
        <artifactId>jstl</artifactId>
        <version>1.2</version>
    </dependency>

application.properties

这是我遇到的问题之一。当我尝试运行它时,它给了我这个错误。

/WEB-INF/jsp/_menu.jsp (line: [18], column: [2]) According to TLD or attribute directive in tag file, attribute [test] does not accept any expressions

这是我第一次使用它。希望你能帮助我。非常感谢!!

【问题讨论】:

请发布您的 pom.xml 文件和您的 jsp 页面 According to TLD or attribute directive in tag file, attribute items does not accept any expressions的可能重复 @AvijitBarua 完成 @user7294900 我已经尝试过了,但它不起作用 您在_menu jsp 页面中出现错误,而不是在welcomePage 中! 【参考方案1】:

在你的jsp中使用这个而不是你拥有的:

" "

不带引号

【讨论】:

以上是关于spring boot thymeleaf和jsp可以共存吗的主要内容,如果未能解决你的问题,请参考以下文章

Spring boot:如何将thymeleaf转换为jsp

Thymeleaf 或 JSP:Spring Boot 哪个更好?

Spring boot 使用了 thymeleaf模板后,再配置jsp就不好用了,难道使用了thymeleaf 不能用jsp了?

如何使用 Thymeleaf 或 JSP 在 Spring Boot 中启用目录列表

Thymeleaf 模板 在spring boot 中的引用和应用

spring boot使用thymeleaf