java - Spring 启动: How to convert thymeleaf to jsp

标签 java spring jsp spring-boot thymeleaf

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

这是我的代码:

Controller

@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;
}

thymeleaf

<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>

应用程序属性

enter image description here

enter image description here

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

/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

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

最佳答案

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

"<%@ taglib prefix="c"uri="http://java.sun.com/jsp/jSTL/core"%> "

不带引号

关于java - Spring 启动: How to convert thymeleaf to jsp,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52913656/

相关文章:

java - jQuery timepicker 插件不适用于 struts2-jquery

java - Assetmanager 和 Skin 处理 Font(/Skin)

java - 在 OpenCMS 网站上更改域名

java - 在数组中查找匹配元素

java - 如何防止在模板中使用环境变量?

java - struts2 spring项目运行在jetty和tomcat上

java - Spring MVC Controller 中的硬编码@RequestMapping URL

java - 设置查询获取的结果的格式以供显示

java - 等效于速度模板的 DisplayTag?

java - CircularQueue 程序出现错误