标签: thymeleaf

在Thymeleaf怎么做if-else?

什么是在Thymeleaf做一个简单的if-else的最好方法?

我希望在Thymeleaf中实现与之相同的效果

<c:choose>
  <c:when test="${potentially_complex_expression}">
     <h2>Hello!</h2>
  </c:when>
  <c:otherwise>
     <span class="xxx">Something else</span>
  </c:otherwise>
</c:choose>
Run Code Online (Sandbox Code Playgroud)

在JSTL.

到目前为止我的想法:

<div th:with="condition=${potentially_complex_expression}" th:remove="tag">
    <h2 th:if="${condition}">Hello!</h2>
    <span th:unless="${condition}" class="xxx">Something else</span>
</div>
Run Code Online (Sandbox Code Playgroud)

我不想评估if两次.这就是我引入局部变量的原因else.

我仍然不喜欢同时使用potentially_complex_expressioncondition.

重要的是我使用了2个不同的html标签:让我们说th:if="${condition}th:unless="${condition}".

你能建议一个更好的方法来实现吗?

java jsp if-statement jstl thymeleaf

119
推荐指数
8
解决办法
24万
查看次数

在thymeleaf中使用data-*属性

我可以用thymeleaf设置data-*属性吗?

据我从thymeleaf文件中了解到,我尝试过:

<div th:data-el_id="${element.getId()}"> <!-- doesn't work -->

<div data-th-el_id="${element.getId()}"> <!-- doesn't work -->
Run Code Online (Sandbox Code Playgroud)

html thymeleaf

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

如何避免Spring MVC测试的"循环视图路径"异常

我的一个控制器中有以下代码:

@Controller
@RequestMapping("/preference")
public class PreferenceController {

    @RequestMapping(method = RequestMethod.GET, produces = "text/html")
    public String preference() {
        return "preference";
    }
}
Run Code Online (Sandbox Code Playgroud)

我只是尝试使用Spring MVC测试来测试它,如下所示:

@ContextConfiguration
@WebAppConfiguration
@RunWith(SpringJUnit4ClassRunner.class)
public class PreferenceControllerTest {

    @Autowired
    private WebApplicationContext ctx;

    private MockMvc mockMvc;
    @Before
    public void setup() {
        mockMvc = webAppContextSetup(ctx).build();
    }

    @Test
    public void circularViewPathIssue() throws Exception {
        mockMvc.perform(get("/preference"))
               .andDo(print());
    }
}
Run Code Online (Sandbox Code Playgroud)

我收到以下异常:

循环视图路径[preference]:将再次调度回当前处理程序URL [/ preference].检查您的ViewResolver设置!(提示:由于默认的视图名称生成,这可能是未指定视图的结果.)

我发现奇怪的是,当我加载包含模板和视图解析器的"完整"上下文配置时,它工作正常,如下所示:

<bean class="org.thymeleaf.templateresolver.ServletContextTemplateResolver" id="webTemplateResolver">
    <property name="prefix" value="WEB-INF/web-templates/" />
    <property name="suffix" value=".html" …
Run Code Online (Sandbox Code Playgroud)

spring spring-mvc circular-reference thymeleaf spring-mvc-test

96
推荐指数
10
解决办法
16万
查看次数

使用Thymeleaf从Spring模型设置javascript变量

如何从__CODE__模型中读取模型变量并将其设置为Javascript?

请注意,我正在使用__CODE__模板引擎.

春方:

@RequestMapping(value = "message", method = RequestMethod.GET)
public String messages(Model model) {
    model.addAttribute("message", "hello");
    return "index";
}
Run Code Online (Sandbox Code Playgroud)

客户端:

<script>
    ....
    var m = ${message}; // not working
    alert(m);
    ...
</script>
Run Code Online (Sandbox Code Playgroud)

javascript spring thymeleaf

96
推荐指数
7
解决办法
12万
查看次数

Thymeleaf:如何使用条件来动态添加/删除CSS类

通过使用Thymeleaf作为模板引擎,是否可以div使用th:if子句向/从一个简单的动态添加/删除CSS类?

通常,我可以使用条件子句如下:

<a href="lorem-ipsum.html" th:if="${condition}">Lorem Ipsum</a> 
Run Code Online (Sandbox Code Playgroud)

我们将创建一个指向lorem ipsum页面的链接,但仅限于条件子句为真.

我正在寻找不同的东西:我希望块始终可见,但根据情况可变类.

html css thymeleaf

86
推荐指数
5
解决办法
9万
查看次数

Thymeleaf:连接 - 无法解析为表达式

我在尝试在模板中连接多个值时遇到问题.根据Thymeleaf 在这里我应该只是能够+他们在一起......

4.6加强文本

文本,无论是文字还是评估变量或消息表达式的结果,都可以使用+运算符轻松连接:

th:text="'The name of the user is ' + ${user.name}"
Run Code Online (Sandbox Code Playgroud)

以下是我发现的工作示例:

<p th:text="${bean.field} + '!'">Static content</p>
Run Code Online (Sandbox Code Playgroud)

然而,这不是:

<p th:text="${bean.field} + '!' + ${bean.field}">Static content</p>
Run Code Online (Sandbox Code Playgroud)

从逻辑上讲,这应该有效,但不是,我做错了什么?


Maven的:

<dependency>
    <groupId>org.thymeleaf</groupId>
    <artifactId>thymeleaf-spring3</artifactId>
    <version>2.0.16</version>
    <scope>compile</scope>
</dependency>
Run Code Online (Sandbox Code Playgroud)

以下是我设置TemplateEngine和TemplateResolver的方法:

<!-- Spring config -->
<bean id="templateResolver" class="org.thymeleaf.templateresolver.ClassLoaderTemplateResolver">
    <property name="suffix" value=".html"/>
    <property name="templateMode" value="HTML5"/>
    <property name="characterEncoding" value="UTF-8"/>
    <property name="order" value="1"/>
</bean>
<bean id="templateEngine" class="org.thymeleaf.spring3.SpringTemplateEngine">
    <property name="templateResolver" ref="fileTemplateResolver"/>
    <property name="templateResolvers">
        <list>
            <ref bean="templateResolver"/>
        </list>
    </property>
Run Code Online (Sandbox Code Playgroud)

ThymeleafTemplatingService:

@Autowired private TemplateEngine templateEngine;
.....
String responseText = this.templateEngine.process(templateBean.getTemplateName(), …
Run Code Online (Sandbox Code Playgroud)

html java thymeleaf

67
推荐指数
4
解决办法
8万
查看次数

当值为null时使用Thymeleaf

我的数据库中有一些值,如果尚未输入,则可以为null.

但是当我在我的html中使用Thymeleaf时,它在解析空值时会出错.

有办法处理这个吗?

html null thymeleaf

61
推荐指数
7
解决办法
10万
查看次数

处理thymeleaf变量作为html代码而不是文本

我正在使用Thymeleaf来处理html模板,我知道如何从我的控制器中追加内联字符串,但现在我想在页面中附加一段html代码.

例如,让我留在我的java应用程序中:

String n="<span><i class=\"icon-leaf\"></i>"+str+"</span> <a href=\"\"></a>\n";

final WebContext ctx = new WebContext(request, response, 
                                      servletContext, request.getLocale());
ctx.setVariable("n",n);
Run Code Online (Sandbox Code Playgroud)

我需要在html页面中编写什么内容,以便将其替换为"n"变量并作为html代码处理而不是将其编码为文本?

java thymeleaf

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

Thymeleaf构造带变量的URL

我在控制器中设置了以下代码:

model.set("type", type);
Run Code Online (Sandbox Code Playgroud)

在thymeleaf视图中,我想构建一个带有动作URL的表单:

/mycontroller/{type}
Run Code Online (Sandbox Code Playgroud)

任何想法如何实现这一目标?我没有运气就阅读了百里香的文件.

java jsp thymeleaf

51
推荐指数
4
解决办法
7万
查看次数

Thymeleaf th:text - 放置文本而不删除HTML结构

我是百里香的新手,我尝试创建一个模板.我的问题是这段代码:

<h1 th:text="${header.title}" >
   title
   <small th:text="${header.subtitle}" >Subtitle</small>
</h1>
Run Code Online (Sandbox Code Playgroud)

我想得到这个输出:

<h1> TITLE <small> SUBTITLE</small> </h1>
Run Code Online (Sandbox Code Playgroud)

但这是真正的输出:

<h1> TITLE </h1>
Run Code Online (Sandbox Code Playgroud)

我怎么能这样做它不会删除"小"里面的东西?

提前致谢.

templates spring-mvc thymeleaf

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