标签: thymeleaf

Spring MVC:如何在Thymeleaf中获取当前URL

我正在使用Thymeleaf模板引擎和Spring Web MVC,我在当前网址的帮助下创建url时遇到困难.有没有办法在Thymeleaf HTML文件中获取最新信息?例如:假设我的浏览器地址栏中的当前网址为:

http://localhost:8080/project/web/category/mobiles

现在我想做一个像这样的网址 http://localhost:8080/project/web/category/mobiles/store/samsung

要么

http://localhost:8080/project/web/category/mobiles?min_price=10&max_price=100.

所以我的代码看起来像这样

<a th:with="currentUrl='http://localhost:8080/project/web/category/mobiles'" 
   th:href="@{__${currentUrl}__/__${store.name}__}">
    Click to More Result
</a>
Run Code Online (Sandbox Code Playgroud)

这里我使用currentUrl变量与硬编码的URL,所以我想要一些解决方案相同.硬编码的值不会每次都有效,因为我有动态类别.

我尝试了相同的url,但它不适合我.

<a th:href="@{/store/__${store.name}__}">Click to More</a>

//will produce: http://localhost:8080/project/web/store/samsung
//I want: http://localhost:8080/project/web/category/mobiles/store/samsung
Run Code Online (Sandbox Code Playgroud)

请看看,如果我做错了,请告诉我.

spring-mvc thymeleaf

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

Thymleaf - PUBLICID请求不支持的实体"null"

当使用Thymleaf和Spring MVC时,我遇到了这个只打印这个堆栈跟踪的问题.

例外

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is
org.thymeleaf.exceptions.TemplateProcessingException: Unsupported entity requested with PUBLICID "null" and SYSTEMID "http://www.thymeleaf.org/dtd/xhtml1-strict-thymeleaf-4.dtd". Make sure a corresponding org.thymeleaf.doctype.resolution.IDocTypeResolutionEntry implementation is provided by you dialect
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:973)
org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:852)
javax.servlet.http.HttpServlet.service(HttpServlet.java:620)
org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:837)
javax.servlet.http.HttpServlet.service(HttpServlet.java:727) 
root cause
org.thymeleaf.exceptions.TemplateProcessingException: Unsupported entity requested with PUBLICID "null" and SYSTEMID "http://www.thymeleaf.org/dtd/xhtml1-strict-thymeleaf-4.dtd". Make sure a corresponding org.thymeleaf.doctype.resolution.IDocTypeResolutionEntry implementation is provided by you dialect
org.thymeleaf.templateparser.EntityResolver.resolveEntity(EntityResolver.java:72)
org.thymeleaf.templateparser.xmlsax.AbstractNonValidatingSAXTemplateParser$XmlSAXHandler.resolveEntity(AbstractNonValidatingSAXTemplateParser.java:708)
org.thymeleaf.templateparser.xmlsax.AbstractNonValidatingSAXTemplateParser$XmlSAXHandler.resolveEntity(AbstractNonValidatingSAXTemplateParser.java:716)
com.sun.org.apache.xerces.internal.util.EntityResolver2Wrapper.resolveEntity(EntityResolver2Wrapper.java:173)
com.sun.org.apache.xerces.internal.impl.XMLEntityManager.resolveEntityAsPerStax(XMLEntityManager.java:1018)
com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl$DTDDriver.dispatch(XMLDocumentScannerImpl.java:1190)
com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl$DTDDriver.next(XMLDocumentScannerImpl.java:1088)
com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl$PrologDriver.next(XMLDocumentScannerImpl.java:1001)
com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(XMLDocumentScannerImpl.java:647)
com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:513)
com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:815)
com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:744)
com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:128)
com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1208)
com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(SAXParserImpl.java:543)
javax.xml.parsers.SAXParser.parse(SAXParser.java:395)
org.thymeleaf.templateparser.xmlsax.AbstractNonValidatingSAXTemplateParser.doParse(AbstractNonValidatingSAXTemplateParser.java:209)
org.thymeleaf.templateparser.xmlsax.AbstractNonValidatingSAXTemplateParser.parseTemplateUsingPool(AbstractNonValidatingSAXTemplateParser.java:133)
org.thymeleaf.templateparser.xmlsax.AbstractNonValidatingSAXTemplateParser.parseTemplate(AbstractNonValidatingSAXTemplateParser.java:116)
org.thymeleaf.TemplateRepository.getTemplate(TemplateRepository.java:277)
org.thymeleaf.TemplateEngine.process(TemplateEngine.java:1104) …
Run Code Online (Sandbox Code Playgroud)

spring spring-mvc thymeleaf

18
推荐指数
2
解决办法
5234
查看次数

如何在Thymeleaf和Spring Boot中显示消息?

我创建了一个使用Thymeleaf作为模板引擎的Spring Boot Web应用程序.我配置了MessageSource在子文件夹中查找消息:

@Bean
public MessageSource messageSource() {
    final ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();

    messageSource.setBasename("i18n/messages");
    messageSource.setFallbackToSystemLocale(false);
    messageSource.setCacheSeconds(0);

    return messageSource;
}
Run Code Online (Sandbox Code Playgroud)

在这个文件夹中,我创建了messages_de.properties包含内容的文件ticket.type.BUG=Fehler.在我的模板中,我尝试显示如下文本:

<p th:text="#{ticket.type.BUG}">BUG</p>
Run Code Online (Sandbox Code Playgroud)

但是当呈现页面时,我得到以下内容:

<p>??ticket.type.BUG_de_DE??</p>
Run Code Online (Sandbox Code Playgroud)

我错过了什么?我是否必须配置任何其他bean?

PS:

在"服务器端"我可以使用获取消息MessageSource#getMessage("ticket.type.BUG", null, Locale.GERMANY).

spring spring-mvc thymeleaf spring-boot

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

Thymeleaf的循环中的计数器

有没有办法在没有列表的情况下在Thymeleaf中进行循环?

我想基本上将这个片段转换为Thymeleaf:

<jsp:useBean id="now" class="java.util.Date" />
<fmt:formatDate var="year" value="${now}" pattern="yyyy" />
<c:forEach var="i" begin="0" end="99">
    <form:option value="${year-i}" />
</c:forEach>
</form:select>
Run Code Online (Sandbox Code Playgroud)

- 更新 -

我已经决定这是我想要的方式,但我不确定springEL语法:

<option th:each="i : ${#numbers.sequence( 1, 100)}" th:value="#{ T(java.util.Date).getYear() - $i }">1</option>
Run Code Online (Sandbox Code Playgroud)

thymeleaf

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

如何使用百里香叶传递两个对象?

我的问题如下:

我有2个不同的对象,我要从单个表单中填充.

使用1个对象,我只需要在newFoo.html中执行:

<form th:object="${foo} th:action="@{/foo}" method="post">
    <input type="text" th:field="*{name}"/>
    <button type="submit">Go</button>
</form>
Run Code Online (Sandbox Code Playgroud)

并在FooController中:

@RequestMapping(value = "/foo/new", method = RequestMethod.GET) 
public String newFoo(final Foo foo, Model model) { 
    return "newFoo"; 
} 

@RequestMapping(value = "/foo/new", method = RequestMethod.POST) 
public String saveFoo(final Foo foo, final BindingResult bindingResult, Model model) { 
    fooService.save(foo); 
    return "redirect:/foo/new"; 
} 
Run Code Online (Sandbox Code Playgroud)

假设我有另一个带有"status"变量的对象栏.如何传递该对象以便我可以在同一表单中提交输入?

喜欢:

<form th:object="${foo} && ${bar}" th:action="@{/foo}" method="post">
    <input type="text" th:field="*{name}"/>
    <input type="text" th:field="*{status}"/>
    <button type="submit">Go</button>
</form>
Run Code Online (Sandbox Code Playgroud)

到目前为止,我尝试使用th:object中的fieldset,这不起作用,我试图将两个:对象放在表单中,这也不起作用.

我找到的唯一方法是构建一个包含这两个对象的其他对象,然后传递它.这很好,但我不能创造那种对象,这是无稽之谈(即使它有效).

当然,对象并不像Foo和Bar那么简单,否则我会合并这两个.但那不是我能做的.

甚至可以传递两个这样的对象来在表单中使用吗?

谢谢.

html java spring thymeleaf

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

同时使用Thymeleaf和JSP

我正在使用JSP + JSTL,但我很无聊c:if,c:choose,...

所以,我希望我的JSP页面都使用JSP和Thymeleaf进行渲染(我计划尽快删除所有JSTL).我正在使用Spring MVC框架:

<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/WEB-INF/pages/" />
    <property name="suffix" value=".jsp" />
    <property name="order" value="1" />
</bean>
<!-- Thymeleaf -->
<bean id="templateResolver" class="org.thymeleaf.templateresolver.ServletContextTemplateResolver">
    <property name="prefix" value="/WEB-INF/pages/" />
    <property name="suffix" value=".html" />
    <property name="templateMode" value="HTML5" />
</bean>
<bean id="templateEngine" class="org.thymeleaf.spring4.SpringTemplateEngine">
    <property name="templateResolver" ref="templateResolver" />
</bean>
<bean class="org.thymeleaf.spring4.view.ThymeleafViewResolver">
    <property name="templateEngine" ref="templateEngine" />
    <property name="order" value="2" />
</bean> 
Run Code Online (Sandbox Code Playgroud)

在我的控制器中,我只是在没有扩展的情况下返回jsp.

return "folder/page";
Run Code Online (Sandbox Code Playgroud)

我的JSP页面可以首先使用JSP解析器,然后使用Thymeleaf解析器进行渲染吗?如果有,怎么样?

链接JSP和Thymeleaf似乎非常复杂.所以,我想对JSP文件使用内部解析器,对HTML文件使用Thymeleaf模板解析器.我该怎么做?

spring jsp spring-mvc thymeleaf

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

Spring MVC + Thymeleaf:向所有模板的上下文添加变量

如何在模板上下文中添加"全局"变量(如用户名)?

目前,我将这些显式设置为我的TemplateController中的每个ModelAndView对象.

java spring-mvc thymeleaf

17
推荐指数
3
解决办法
6812
查看次数

Thymeleaf的头和头衔

我是一个Thymeleaf初学者.我从一个共同的布局页面开始:

片段/的layout.html

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head th:fragment="headerFragment">
    <title>Template title</title>
    <!-- metas, link and scripts -->
</head>
<body>
<div class="container">
    Some text
</div>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)

还有一个内容页面:

page.html中

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head th:include="fragments/layout :: headerFragment">
    <title>Page title</title>
    <!-- metas, link and scripts -->
</head>
<body>
<div class="container">
    Some text
</div>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)

当我渲染页面时,标题始终来自模板,而不是来自页面.在Thymeleaf中是否可以使用metas,脚本和样式共同(在HEAD标签中)但是每页标题?

thymeleaf

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

Spring Boot验证消息未解析

我无法解析验证消息.

我一直在搜索和阅读网络和SO几个小时,我想将问题与自定义弹簧验证错误的明确答案联系起来

我确实有一个MessageSourcebean定义并且messages.properties正确读取,因为我也使用它来显示常规文本th:text="#{some.prop.name},这确实很好用.只是验证错误不会以它应该的方式工作.我确定这是一个我忽略的愚蠢错误......验证本身工作正常.

约束:

@NotEmpty(message="{validation.mail.notEmpty}")
@Email()
private String mail;
Run Code Online (Sandbox Code Playgroud)

messages.properties:

# Validation
validation.mail.notEmpty=The mail must not be empty!
Run Code Online (Sandbox Code Playgroud)

模板部分:

<span th:if="${#fields.hasErrors('mail')}" th:errors="*{mail}"></span>
Run Code Online (Sandbox Code Playgroud)

显示的文字:

{validation.mail.notEmpty}
Run Code Online (Sandbox Code Playgroud)

我尝试了很多变化,都没有成功.

@NotEmpty(message="validation.mail.notEmpty")
@NotEmpty(message="#{validation.mail.notEmpty}")
Run Code Online (Sandbox Code Playgroud)

只显示消息字符串的确切值,无需解析.

<span th:if="${#fields.hasErrors('mail')}" th:errors="${mail}"></span>
<span th:if="${#fields.hasErrors('mail')}" th:errors="#{mail}"></span>
<span th:if="${#fields.hasErrors('mail')}" th:errors="#{*{mail}}"></span>
<span th:if="${#fields.hasErrors('mail')}" th:errors="#{__*{mail}__}"></span>
Run Code Online (Sandbox Code Playgroud)

会导致错误.


编辑:

经过调试,我偶然发现了这个问题:

类: org.springframework.context.support.MessageSourceSupport

方法: formatMessage(String msg, Object[] args, Locale locale)

将被召唤

formatMessage("{validation.mail.notEmpty}", null, locale /*German Locale*/)

它会遇到 if (messageFormat == INVALID_MESSAGE_FORMAT) {

所以...我的消息格式不正确.这超出了我的范围/知识.谁知道这意味着什么?

java spring thymeleaf spring-boot

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

SpringBoot - UTF-8在messages.properties中不起作用

有什么问题?

我无法在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)

spring utf-8 thymeleaf

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

标签 统计

thymeleaf ×10

spring ×6

spring-mvc ×5

java ×3

spring-boot ×2

html ×1

jsp ×1

utf-8 ×1