标签: thymeleaf

thymeleaf - 脚本和 th:block

)有没有办法在“script”标签内使用“th:block”标签?像这样的东西:

<script type="text/javascript">
    <th:block>
        var test = 1;
    </th:block>
</script>
Run Code Online (Sandbox Code Playgroud)

javascript thymeleaf

0
推荐指数
1
解决办法
5728
查看次数

百里香叶。如何比较日期是未来的还是过去的?

我想检查 Employee.dateOfTermination 中的日期是否在日期“现在”之后。不幸的是,运行以下代码后,我没有得到任何数据:

<td> <div th:switch="${employee.dateOfTermination}"> 
                            <span th:case="'&lt; now'">CASE 1</span>
                            <span th:case="'&gt; now'" th:text="${#dates.format(employee.dateOfTermination, 'dd-MM-yyyy')}">CASE 2</span></div></td>
Run Code Online (Sandbox Code Playgroud)

问题来了,thymeleaf 的语法对我来说确实显得不适用而且恶心。我尝试使用 th:if 并解析 int 。

spring thymeleaf

0
推荐指数
1
解决办法
8369
查看次数

如何使用 Spring Web 应用程序显示 PDF 文件

我希望在页面上显示此文件,但此代码可直接下载

<a th:href="@{/pdf/Manjaro-User-Guide.pdf}">Show Pdf file</a>

我正在使用 Spring-Thymeleaf

谢谢!

spring-mvc thymeleaf spring-boot

0
推荐指数
1
解决办法
2568
查看次数

使用 Spring Thymeleaf 重定向到另一个页面

我想在按下“注册”按钮后从主页index.html(用户)重定向到他能够创建帐户的页面。然而,什么也没有发生。仅添加到地址:http://localhost:8080/DrinkShop注释在 /register 末尾

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>pl.myapp</groupId>
    <artifactId>DrinkShop</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>



    <name>DrinkShop</name>
    <description>Spring Application</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.3.RELEASE</version>
        <relativePath />
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>

        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
        </dependency>

        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-entitymanager</artifactId>
        </dependency>

        <dependency>
            <groupId>javax.xml.bind</groupId>
            <artifactId>jaxb-api</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</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>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope> …
Run Code Online (Sandbox Code Playgroud)

java redirect thymeleaf

0
推荐指数
1
解决办法
3万
查看次数

将 SpringBoot 从 2.0.6.RELEASE 更新到 2.1.0.RELEASE 后计算 SpringEL 表达式时出现异常

我有一个带有以下 pom.xml 的 SpringBoot 应用程序:

\n\n
...\n<parent>\n        <groupId>org.springframework.boot</groupId>\n        <artifactId>spring-boot-starter-parent</artifactId>\n        <version>2.0.6.RELEASE</version>\n        <relativePath/> \n    </parent>\n\n<!-- Spring Security -->\n        <dependency>\n            <groupId>org.thymeleaf.extras</groupId>\n            <artifactId>thymeleaf-extras-springsecurity4</artifactId>\n        </dependency>\n...\n
Run Code Online (Sandbox Code Playgroud)\n\n

并且运行良好。\n然后我将 SpringBoot 版本更新为

\n\n
...\n    <parent>\n        <groupId>org.springframework.boot</groupId>\n        <artifactId>spring-boot-starter-parent</artifactId>\n        <version>2.1.0.RELEASE</version>\n        <relativePath/> \n    </parent>\n\n        <dependency>\n            <groupId>org.thymeleaf.extras</groupId>\n            <artifactId>thymeleaf-extras-springsecurity4</artifactId>\n            <version>3.0.4.RELEASE</version> \n        </dependency>\n...\n
Run Code Online (Sandbox Code Playgroud)\n\n

我在 1 个模板中收到此错误:

\n\n
Exception evaluating SpringEL expression: "#authorization.expression(\'hasRole(\'\'ROLE_ADMIN\'\')\')" (template: "tdk/common/menu" - line 87, col 21)\n
Run Code Online (Sandbox Code Playgroud)\n\n

这里是模板:

\n\n
 <li th:if="${#authorization.expression(\'hasRole(\'\'ROLE_ADMIN\'\')\')}"    class="menu-principal pure-menu-item" th:classappend="${activeMenuItem == \xe2\x80\x98tdkMessages\'} ?  pure-menu-selected">\n                    <a href=\xe2\x80\x9c/tdk/list" class="pure-menu-link">\n                        <i class="fas fa-cloud-download-alt fa-lg fa-fw"></i>&nbsp; tdk\n                    </a>\n                </li>\n
Run Code Online (Sandbox Code Playgroud)\n

spring spring-mvc spring-el thymeleaf spring-boot

0
推荐指数
1
解决办法
850
查看次数

用户登录并重定向到主页后,如何隐藏登录链接并在主页导航栏中显示注销链接?

我正在开发一个基于 Spring boot 的 Web 应用程序,主页包含一个带有登录和注册链接的 boostrap 导航栏。

用户登录后(通过单击主页中的登录链接),他们将再次重定向到主页(除非他们在 Spring Security 启动 A&A 之前访问任何其他链接)。在重定向时,我想隐藏导航栏中的登录和注册链接,并在其位置显示注销链接。因为我想在应用程序的所有页面中插入导航栏。

我想知道当要求重定向到同一页面时执行此操作的最佳/标准方法是什么。

我能想到的一种解决方案是从 spring security 检查并获取 user-principal 对象,将其传递给 thymeleaf 模板,并检查 thymeleaf 模板的请求属性中是否存在 userprincipal 对象,这意味着用户已登录然后我可以隐藏(不渲染)登录和注册链接以及显示(渲染)注销链接。如果没有,则显示登录和注册链接并隐藏注销链接。我想知道,这是正确的方法吗?这对我来说就像是一种黑客攻击,因此想知道是否有任何标准方法可以做到这一点。

spring-mvc spring-security thymeleaf spring-boot bootstrap-4

0
推荐指数
1
解决办法
1718
查看次数

如何将 JSP 转换为 Thymeleaf?

我在使用 Thymeleaf 时遇到问题,我不知道该怎么做。

<!-- chart.jsp-->
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<script type="text/javascript">
window.onload = function() {

var dps = [[]];
var chart = new CanvasJS.Chart("chartContainer", {
    animationEnabled: true,
    exportEnabled: true,
    title: {
        text: "Simple Column Chart with Index Labels"
    },
    data: [{
        type: "column", //change type to bar, line, area, pie, etc
        //indexLabel: "{y}", //Shows y value on all Data Points
        indexLabelFontColor: …
Run Code Online (Sandbox Code Playgroud)

java model-view-controller spring jsp thymeleaf

0
推荐指数
1
解决办法
5949
查看次数

Spring Boot 和 Thymeleaf 在字段错误上返回 400 错误

我使用 Spring Boot 2.3.1 和 Thymeleaf 来创建一个简单的注册应用程序,以便用户可以创建模型并将其保存Member到数据库中。我正在使用该javax.validation框架来确保字段不为空,并且当模型有效时它可以正常工作,但是缺少字段的无效模型会将我踢出默认页面,并显示 400 错误代码error.html,而不是将用户带回我可以在注册表中显示字段级错误(例如,“需要用户名”)。

该模型很简单,如下所示:

@Entity
@Table(name="member")
public class Member {

    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE)
    private Long id;

    @Email
    @NotEmpty
    private String username;

    @NotEmpty
    @Column(name="password", nullable = false)
    private String password;
   
    // ... getters and setters
Run Code Online (Sandbox Code Playgroud)

控制器看起来像这样:

@PostMapping("/register")
public ModelAndView registerMember(HttpServletRequest request, HttpServletResponse response,
                                       @ModelAttribute @Valid Member member, ModelMap model, BindingResult result){
   log.info("Attempting to register a user");

   // some other error checking not related to javax.validation ...

   memberService.save(member);

   return …
Run Code Online (Sandbox Code Playgroud)

java validation spring thymeleaf spring-boot

0
推荐指数
1
解决办法
1653
查看次数

Spring Boot 与 Spring Security - 使用 SMS/PIN/TOTP 的两因素身份验证

我正在开发一个 Spring Boot 2.5.0 Web 应用程序,并使用 Thymeleaf 进行 Spring Security 表单登录。我正在寻找有关如何使用 Spring Security 表单登录实现两因素身份验证(2FA)的想法。

要求是当用户使用他的用户名和密码登录时。在登录表单中,如果用户名和密码验证成功,则应将短信代码发送到用户注册的手机号码,并应在另一个页面上要求用户输入短信代码。如果用户正确获取短信代码,他应该被转发到安全的应用程序页面。

在登录表单上,除了用户名和密码外,还要求用户输入验证码图像中的文本,该文本使用SimpleAuthenticationFilter扩展的验证UsernamePasswordAuthenticationFilter

这是当前的SecurityConfiguration

@Configuration
@EnableWebSecurity
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
    @Autowired
    private CustomUserDetailsServiceImpl userDetailsService;
    
    @Override
    protected void configure(HttpSecurity httpSecurity) throws Exception {
        httpSecurity
            .addFilterBefore(authenticationFilter(), UsernamePasswordAuthenticationFilter.class)    
            .authorizeRequests()
                .antMatchers(
                        "/favicon.ico",
                        "/webjars/**",
                        "/images/**",
                        "/css/**",
                        "/js/**",
                        "/login/**",
                        "/captcha/**",
                        "/public/**",
                        "/user/**").permitAll()
                .anyRequest().authenticated()
            .and().formLogin()
                .loginPage("/login")
                    .permitAll()
                .defaultSuccessUrl("/", true)
            .and().logout()
                .invalidateHttpSession(true)
                .clearAuthentication(true)
                .deleteCookies("JSESSONID")
                .logoutRequestMatcher(new AntPathRequestMatcher("/logout"))
                .logoutSuccessUrl("/login?logout")
                    .permitAll()
            .and().headers().frameOptions().sameOrigin()
            .and().sessionManagement()
                .maximumSessions(5)
                .sessionRegistry(sessionRegistry())
                .expiredUrl("/login?error=5");
    }

    public SimpleAuthenticationFilter authenticationFilter() throws …
Run Code Online (Sandbox Code Playgroud)

spring-security two-factor-authentication thymeleaf spring-boot

0
推荐指数
1
解决办法
1万
查看次数

如何根据用户显示不同的视图/HTML 页面?

我正在Web application使用Spring MVC构建一个简单的。

我想显示不同views的:

  • 管理员

  • 标准用户

我怎样才能做到这一点?我要求作为标准用户Administrators能够执行不同的功能并看到额外的等。buttons

管理员将被授予额外的特权/权限,可能使用数据库flag中的来标识oracle

作为参考,我还在应用程序中使用Spring DataThyme leaf 。

oracle spring administrator spring-mvc thymeleaf

-1
推荐指数
1
解决办法
2273
查看次数

从 Thymeleaf 读取变量时如何消除 JavaScript 中的双引号

我试图从我的控制器调用一个变量:

model.addAttribute("something",someService.getSomething()); /// output is a json string will be read by vue.js
Run Code Online (Sandbox Code Playgroud)

我的js文件嵌入到html中

<script th:inline="javascript" type="text/javascript">
  var something = [[${something}]];
  console.log(something);
</script>
Run Code Online (Sandbox Code Playgroud)

控制台的输出正是我所期望的。但是当我检查页面源时,输出是双引号的。

var something = "MY OUTPUT";
Run Code Online (Sandbox Code Playgroud)

我怎样才能让它不生成双引号?

javascript thymeleaf

-1
推荐指数
1
解决办法
1068
查看次数

如何在Java Spring启动中将MultipartFile照片作为Base64字符串插入数据库并将其从数据库显示在主屏幕上

如何在 Java Spring 启动中将 MultipartFile 照片作为 Base64 字符串插入数据库,并将其从数据库显示在主屏幕上。

base64 mybatis thymeleaf spring-boot multipartfile

-2
推荐指数
1
解决办法
9214
查看次数