标签: thymeleaf

什么是获取Thymeleaf $ {pageContext.request.contextPath}的语法

我已经搜索了类似JSTL的语法${pageContext.request.contextPath},

我确实用了一个javascript代码来改变表单上的action属性来调用spring控制器上的edit方法,所以问题是下面的代码不能在不调用Context的情况下工作喜欢${pageContext.request.contextPath}/edit.html

<script th:inline="javascript">
    function edit() {
        document.getElementById("user_form").action = "/edit.html";
    }
</script>
Run Code Online (Sandbox Code Playgroud)

那么调用Thymeleaf上下文路径的语法是什么?

javascript spring-mvc thymeleaf

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

使用百里香叶的条件属性

我知道如何在jstl上的标记内创建条件属性:

<body <c:if test="${userCreated}"> onload="somejavascriptfunction()"</c:if> >
Run Code Online (Sandbox Code Playgroud)

但是我如何使用百里香?

的IndexController

@RequestMapping("/register")
public String register(UserEntity user, @RequestParam String repeatedPassword, RedirectAttributes redirectAttributes) {
    user.setAdmin(false);
    userFacade.create(user);
    redirectAttributes.addFlashAttribute(Constants.USER_CREATED, true);
    logger.info("Usuario criado");
    return "redirect:/login";
}
Run Code Online (Sandbox Code Playgroud)

到目前为止,我找到的唯一解决方案是这样做

<script type="text/javascript" th:if="${userCreated}">
  $(document).ready(function() {
     somejavascriptfunction()
  });
</script>
Run Code Online (Sandbox Code Playgroud)

但这似乎并不是最好的方法.那么如何为百里香叶上的属性创建一个if语句?

jsp thymeleaf

20
推荐指数
4
解决办法
2万
查看次数

Thymeleaf多个提交按钮在一个表单中

我有一个带有一个表单和两个按钮的HTML页面片段:

<form action="#" data-th-action="@{/action/edit}" data-th-object="${model}" method="post">
    <button type="submit" name="action" value="save">save</button>
    <button type="submit" name="action" value="cancel">cancel</button>
</form>
Run Code Online (Sandbox Code Playgroud)

和控制器:

@RequestMapping(value="/edit", method=RequestMethod.POST)
public ModelAndView edit(@ModelAttribute SomeModel model, 
        @RequestParam(value="action", required=true) String action) {

    if (action.equals("save")) {
        // do something here     
    }

    if (action.equals("cancel")) {
       // do another thing
    }
    return modelAndView;
}
Run Code Online (Sandbox Code Playgroud)

这项工作很好,但如果我有更多按钮,我必须添加更多if语句来检查action字符串.还有另一种方法可以为表单中的每个按钮创建一个动作吗?

java spring-mvc thymeleaf

20
推荐指数
3
解决办法
2万
查看次数

Thymeleaf包含和替换之间的区别?

两个Thymeleaf属性之间有什么区别:th:includeth:replace

thymeleaf

20
推荐指数
3
解决办法
2万
查看次数

Spring Boot Security + Thymeleaf:缺少IProcessorDialect类

我正在尝试将Thymeleaf安全方言(例如sec:authorize标签)集成到正常工作的Spring Boot + Spring Security应用程序中.

经过一些研究后,我发现激活它的解决方案是:

在POM文件中添加依赖项:

<dependency>
    <groupId>org.thymeleaf.extras</groupId>
    <artifactId>thymeleaf-extras-springsecurity4</artifactId>
    <version>3.0.0.RELEASE</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)

并在模板文件的顶部包含标记:

<html   xmlns:th="http://www.thymeleaf.org" lang="en"
        xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity4">
Run Code Online (Sandbox Code Playgroud)

到现在为止还挺好.找到依赖关系,标记中标记的标记.

但是,它们不会被考虑在内并出现在生成的最终HTML中.

由于Spring Boot自动配置中的一个问题没有启用,似乎有必要手动将SpringSecurityDialect Bean 添加到一个@Configuration类来启用它(StackOverflow上的几个问题已经解决了):

@Bean
    public SpringSecurityDialect securityDialect() {
        return new SpringSecurityDialect();
    }
Run Code Online (Sandbox Code Playgroud)

这就是导致问题的原因:当我将这个Bean添加到我的Spring Boot配置中时,它会引发异常,因为它找不到类org.thymeleaf.dialect.IProcessorDialect.这是错误:

> java.lang.IllegalStateException: Could not evaluate condition on
> org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration#propertySourcesPlaceholderConfigurer
> due to org/thymeleaf/dialect/IProcessorDialect not found. Make sure
> your own configuration does not rely on that class. This can also
> happen if you are @ComponentScanning a springframework package (e.g.
> …
Run Code Online (Sandbox Code Playgroud)

java spring spring-security thymeleaf spring-boot

20
推荐指数
1
解决办法
6648
查看次数

对Thymeleaf的XHTML Basic支持?

我正在尝试使用Spring 4.1.9和Thymeleaf 2.1.5来呈现XHTML Basic 1.1页面,其中包含以下序言:

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML Basic 1.1//EN"
    "http://www.w3.org/TR/xhtml-basic/xhtml-basic11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
Run Code Online (Sandbox Code Playgroud)

仅仅在模板中使用它不起作用,因为Thymeleaf不识别doctype.

org.thymeleaf.exceptions.TemplateProcessingException:与PUBLICID请求不支持的实体" - // W3C // DTD XHTML 1.1的基本// EN"和SYSTEMID" http://www.w3.org/TR/xhtml-basic/xhtml-basic11 .dtd ".确保您的方言提供了相应的org.thymeleaf.doctype.resolution.IDocTypeResolutionEntry实现(索引:1)


建立

我浏览了Thymeleaf 扩展文档源代码,并以此为出发点,定义了一个继承自SpringStandardDialect的新方言.我通过反复试验找出了丢失的模块,从w3.org下载并将它们添加到我项目的资源目录中:

XhtmlBasicDialect.java

import java.util.LinkedHashSet;
import java.util.Set;

import org.thymeleaf.doctype.DocTypeIdentifier;
import org.thymeleaf.doctype.resolution.ClassLoaderDocTypeResolutionEntry;
import org.thymeleaf.doctype.resolution.IDocTypeResolutionEntry;
import org.thymeleaf.spring4.dialect.SpringStandardDialect;

public class XhtmlBasicDialect extends SpringStandardDialect {
    private static final String DTD_STANDARD_PATH = "org/thymeleaf/dtd/standard/";

    private static final DocTypeIdentifier XHTML_BASIC_11_PUBLICID = DocTypeIdentifier.forValue("-//W3C//DTD XHTML Basic 1.1//EN");
    private static final DocTypeIdentifier XHTML_BASIC_11_SYSTEMID = …
Run Code Online (Sandbox Code Playgroud)

java spring thymeleaf

20
推荐指数
1
解决办法
794
查看次数

Thymeleaf:如何获取URL属性值

我找不到使用Thymeleaf从URL获取属性的任何解决方案.例如,对于URL:

somesite.com/login?error=true
Run Code Online (Sandbox Code Playgroud)

我需要获得"错误"属性值.我也在使用SpringMVC,如果它可能有用.

java spring templates thymeleaf

19
推荐指数
2
解决办法
2万
查看次数

sec:authorize和sec:身份验证注释不起作用

我有一个Spring + Thymeleaf项目,其中包含以下视图代码.

<!DOCTYPE html SYSTEM "http://www.thymeleaf.org/dtd/xhtml1-strict-thymeleaf-spring3-3.dtd">
<html
        xmlns="http://www.w3.org/1999/xhtml"
        xmlns:th="http://www.thymeleaf.org"
        xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3">

<head>
    <title>Contacts</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
</head>
<body>
<div id="content">
    <h1>Welcome to the site!</h1>
    <p th:if="${loginError}">Wrong user or password</p>
    <form th:action="@{/j_spring_security_check}" method="post">
        <label for="j_username">Email address</label>:
        <input type="text" id="j_username" name="j_username"/> <br/>
        <label for="j_password">Password</label>:
        <input type="password" id="j_password" name="j_password"/> <br/>
        <input type="submit" value="Log in"/>
    </form>
</div>

<div sec:authorize="isAuthenticated()">
    User: <span sec:authentication="name">miquel</span>
</div>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)

sec:authorize和sec:身份验证属性不能按预期工作 - 即使没有用户登录,也始终显示div,并且span始终显示为"miquel".

跟随我的控制器类的相关片段.

@RequestMapping(value = "/welcome.html") 
public String wellcome() { 
    Authentication auth = SecurityContextHolder.getContext().getAuthentication(); 
    System.out.println("username: " + …
Run Code Online (Sandbox Code Playgroud)

spring-mvc spring-security thymeleaf

19
推荐指数
3
解决办法
2万
查看次数

在onclick属性中使用thymeleaf变量

在我目前的spring-boot项目中,我有一个带有这个html代码的视图:

<button type="button" class="btn btn-primary" onclick="upload()" th:utext="#{modal.save}"></button>
Run Code Online (Sandbox Code Playgroud)

onclick属性中,对函数的调用upload()应该有一个参数,该值存储在thymeleaf变量中${gallery}.

任何人都可以告诉我如何在上面的命令中使用表达式?

我已经尝试过了:

  • th:onclick="upload(${gallery)"

  • th:attr="onclick=upload(${gallery)"

这些都没有奏效.

javascript spring onclick thymeleaf spring-boot

19
推荐指数
5
解决办法
3万
查看次数

Spring Boot - 重定向到不同的控制器方法

我是Spring Boot的新手.我正在使用SpringBoot和Thymeleaf创建一个非常基本的应用程序.在控制器中我有两种方法如下:

Method1 - 此方法显示数据库中的所有数据:

  @RequestMapping("/showData")
public String showData(Model model)
{
    model.addAttribute("Data", dataRepo.findAll());
    return "show_data";
}
Run Code Online (Sandbox Code Playgroud)

Method2 - 此方法将数据添加到数据库:

@RequestMapping(value = "/addData", method = RequestMethod.POST)
public String addData(@Valid Data data, BindingResult bindingResult, Model model) {
    if (bindingResult.hasErrors()) {
        return "add_data";
    }
    model.addAttribute("data", data);
    investmentTypeRepo.save(data);

    return "add_data.html";
}
Run Code Online (Sandbox Code Playgroud)

HTML文件与这些方法相对应,即show_data.html和add_data.html.

一旦addData方法完成,我想显示数据库中的所有数据.但是,上面的代码会将代码重定向到静态add_data.html页面,并且不会显示新添加的数据.我需要以某种方式调用控制器上的showData方法,因此我需要将用户重定向到/ showData URL.这可能吗?如果是这样,怎么办呢?

提前致谢.

controller thymeleaf spring-boot

19
推荐指数
2
解决办法
4万
查看次数