根据官方示例(Secure Web Content),我必须使用表单和按钮,目的是使用Spring Security执行注销.有没有办法使用Thymeleaf链接而不是按钮?
我有一个Spring应用程序,需要构建对PDF生成的支持.我正在考虑将飞碟与Thymeleaf一起使用来渲染PDF.但是,我找不到有关将飞碟与Thymeleaf一起使用的更多信息.有没有其他人一起使用这些技术?
我有以下代码:
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta name="_csrf" th:content="${_csrf.token}"/>
<!-- default header name is X-CSRF-TOKEN -->
<meta name="_csrf_header" th:content="${_csrf.headerName}"/>
<title>Fileupload Test</title>
</head>
<body>
<p th:text="${msg}"></p>
<form action="#" th:action="@{/fileUpload}" method="post" enctype="multipart/form-data">
<input type="file" name="myFile"/>
<input type="submit"/>
</form>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
我收到错误HTTP 403:
在请求参数'_csrf'或标题'X-CSRF-TOKEN'上找到无效的CSRF令牌'null'
如果我使用此行,CSRF正在工作:
<form action="#" th:action="@{/fileUpload} + '?' + ${_csrf.parameterName} + '=' + ${_csrf.token}" method="post" enctype="multipart/form-data">
Run Code Online (Sandbox Code Playgroud)
但是如果我使用标题,我怎样才能实现工作CSRF?
我无法在UTF-8中显示我在messages.properties中收到的消息.
一个例子
<h1 id="logo">Electrónico</h1>
Run Code Online (Sandbox Code Playgroud)
这工作没问题但是当我尝试使用这样的消息源时
<h1 id="logo" th:text="#{titulo.electronico}">Electrónico</h1>
Run Code Online (Sandbox Code Playgroud)
我得到"Electr nico"而不是Electrónico
这是我的配置
application.properties
spring.messages.encoding=UTF-8
server.tomcat.uri-encoding=UTF-8
spring.http.encoding.charset=UTF-8
spring.thymeleaf.mode=HTML5
spring.thymeleaf.encoding=UTF-8
Run Code Online (Sandbox Code Playgroud)
的pom.xml
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<start-class>demo.Demo</start-class>
<java.version>1.7</java.version>
</properties>
Run Code Online (Sandbox Code Playgroud)
演示课
@SpringBootApplication
public class Demo {
public static void main(String[] args) {
SpringApplication.run(Demo.class, args);
}
}
Run Code Online (Sandbox Code Playgroud)
ServletInitializer.class
@Configuration
public class ServletInitializer extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(Demo.class);
}
@Bean
public ServletRegistrationBean h2servletRegistration() {
ServletRegistrationBean registration = new ServletRegistrationBean(new WebServlet());
registration.addUrlMappings("/console/*");
return registration;
}
@Bean
@Order(Ordered.HIGHEST_PRECEDENCE)
CharacterEncodingFilter characterEncodingFilter() {
CharacterEncodingFilter filter = …Run Code Online (Sandbox Code Playgroud) 我在Thymeleaf模板中有以下分隔的片段.
<ul class="nav nav-tabs">
<li role="presentation"><a href="/">Freight Invoices</a></li>
<li role="presentation"><a href="/processed">Processed Invoices</a></li>
<li role="presentation"><a href="/postingrules">Posting Rules</a></li>
<li role="presentation" class="active"><a href="/settings">Settings</a></li>
</ul>
Run Code Online (Sandbox Code Playgroud)
我想在主动导航元素中添加一个"活动"类 - 但在Thymyleaf中似乎很难实现.有什么建议?
我有CSS和Thymeleaf的问题.
在我的Spring启动应用程序中,我有这样的结构:
现在,我的html页面名为ErrorPage,css文件名为Layout.css,使用Thymeleaf,我在ErrorPage的头部:
<link href="../css/Layout.css" th:href="@{css/Layout.css}" type="text/css" />
Run Code Online (Sandbox Code Playgroud)
但这不起作用.
我究竟做错了什么?
我完全改变了这个问题,因为它的一部分在 Avnish的帮助下得到了回答!汤姆把我送到了正确的方向,谢谢汤姆!
我的问题是我不知道如何告诉Thymeleaf在编辑时预先选择对象元素.
让我演示给你看:

此解决方案有效:
<select class="form-control" id="parts" name="parts" multiple="multiple">
<option th:each="part : ${partsAtribute}"
th:selected="${servisAttribute.parts.contains(part)}"
th:value="${part.id}"
th:text="${part.name}">Part name</option>
</select>
Run Code Online (Sandbox Code Playgroud)
我试过这个:
<select class="form-control" th:field="*{parts}" multiple="multiple">
<option th:each="part : ${partsAtribute}"
th:field="*{parts}"
th:value="${part.id}"
th:text="${part.name}">Part name</option>
</select>
Run Code Online (Sandbox Code Playgroud)
不工作.我也试过这个:
<select class="form-control" th:field="*{{parts}}" multiple="multiple">
<option th:each="part : ${partsAtribute}"
th:field="*{parts}"
th:value="${part.id}"
th:text="${part.name}">Part name</option>
</select>
Run Code Online (Sandbox Code Playgroud)
也没用.我试过th:field="*{parts}"从选项标签中删除,结果相同..
如果我改变th:value到${part}它的工作原理,但随后它并没有发回的ID字符串如[2,4,5,6,...],但Part像实例[第@ 43b45j,部分@ we43y7,...] ... .
更新:我刚刚注意到,如果只选择了一个部分,这是有效的:
<select class="form-control" th:field="*{parts}" multiple="multiple">
<option th:each="part : ${partsAtribute}"
th:field="*{parts}"
th:value="${part.id}"
th:text="${part.name}">Part name</option>
</select>
Run Code Online (Sandbox Code Playgroud)
如果选择了多个部件,则不起作用......
嗨,我正在尝试按照一个简单的例子来做一个我在这个页面中找到的简单登录表单页面 http://docs.spring.io/autorepo/docs/spring-security/4.0.x/guides/form.html
问题是我每次尝试登录时都会收到此错误我收到此错误: Expected CSRF token not found. Has your session expired?
当我收到此错误时,我按下浏览器中的后退按钮并尝试第二次登录,当我这样做时,我收到此错误: HTTP 403 - Invalid CSRF Token 'null' was found on the request parameter '_csrf' or header 'X-CSRF-TOKEN'
在教程页面中是这条消息: We use Thymeleaf to automatically add the CSRF token to our form. If we were not using Thymleaf or Spring MVCs taglib we could also manually add the CSRF token using <input type="hidden" name="${_csrf.parameterName}" value="${_csrf.token}"/>
"所以因为我也在使用百里香,我没有把这个标签添加到我的页面"
我找到了另一个解决方案,它的工作原理,这个解决方案是将此添加到我的安全配置类.csrf().disable()这个解决方案有效,但我想这样做是为了禁用我的页面中的csrf保护,我不想禁用这种类型的保护.
这是我的security-config类:
@Configuration
@EnableWebSecurity
public class ConfigSecurity extends WebSecurityConfigurerAdapter {
@Autowired …Run Code Online (Sandbox Code Playgroud) 我在
http://example.com/some/page?p1=11
Run Code Online (Sandbox Code Playgroud)
我想在当前网址中添加一个参数,而不必重新定义它:
http://example.com/some/page?p1=11&p2=32
Run Code Online (Sandbox Code Playgroud)
有类似的东西:
<a th:href="@{?(p2=32)}">Click here</a>
Run Code Online (Sandbox Code Playgroud)
但上面的代码返回http://example.com/some/page?&p2=32(删除p1参数).
我怎么能用Thymeleaf做到这一点?
我正在使用Thymeleaf和spring mvc 4,但是当我想要打印列表大小时我遇到了问题
<tr th:each="u : ${users}" th:id="${u.username}" class="odd gradeX">
<th></th>
<td th:text="${u.username}"></td>
<td th:text="${u.email}"></td>
<td th:switch="${u.enabled}">
<span th:case="true" class="badge badge-primary">Acitf</span>
<span th:case="false" class="badge badge-danger">Désactivée</span>
<span th:case="*" class="badge badge-dark">Inconnue</span>
</td>
<td th:switch="${u.roles}">
<span th:case="ROLE_SUPER_ADMIN">Super ADmin</span>
<span th:case="ROLE_MANGER">Manager</span>
<span th:case="ROLE_SUPERVISEUR">Superviseur</span>
<span th:case="ROLE_USER">User</span>
<span th:case="*">Inconnue</span>
</td>
<td th:text="${#calendars.format(u.creationDate,'dd/MM/yyyy hh:mm')}"></td>
<td th:text="${#calendars.format(u.lastLogin,'dd/MM/yyyy hh:mm')}"></td>
**
<td th:text="${u.engines}"></td>
<td th:text="${u.subordonnes}"></td>
**
<td></td>
</tr>
Run Code Online (Sandbox Code Playgroud)
问题出在这里th:text="${u.engines}</td>.engines是我的User实体中的ArrayList .我尝试过th:size,th:list但它没有用.
任何人都可以帮助我
这是我的User实体:
@OneToMany(mappedBy …Run Code Online (Sandbox Code Playgroud) thymeleaf ×10
spring ×5
java ×4
spring-mvc ×4
security ×2
spring-boot ×2
csrf ×1
css3 ×1
edit ×1
jsp ×1
navigation ×1
option ×1
pdf ×1
selected ×1
utf-8 ×1