简体   繁体   English

Thymeleaf定制处理器-表达式+静态文本

[英]Thymeleaf custom processor - expressions + static text

I am just starting to use thymeleaf for templating, and I am attempting to create a custom Processor for links 我刚刚开始使用thymeleaf进行模板制作,并且尝试为链接创建自定义处理器

I want to be able to use a tag such as the below example to create custom links in my template: 我希望能够使用以下示例的标记在模板中创建自定义链接:

<link rel="stylesheet" type="text/css" custom:href="/styles/main.css" />

The idea being that I have custom URLs on the site, so my custom processor will generate the correct URL for the relative link provided. 我的想法是我在网站上有自定义URL,因此我的自定义处理器将为提供的相对链接生成正确的URL。

The above works fine - however, now I want to be able to include variables insite the value being passed to the processor - so have followed the example in the thymeleaf docs , and in the Processor, where I generate the replacement URL I have added the StandardExpression stuff: 上面的方法工作正常-但是,现在,我希望能够在传递值传递给处理器的位置包含变量-因此, 在thymeleaf docs中以及在Processor中,我按照示例生成了替换URL,并在其中添加了StandardExpression的东西:

    @Override protected String getTargetAttributeValue( Arguments arguments, Element element, String attributeName ){
need this to see if it can be factored out
        final Configuration configuration = arguments.getConfiguration()
        final IStandardExpressionParser parser = StandardExpressions.getExpressionParser(configuration)
        final String attributeValue = element.getAttributeValue(attributeName)
        final IStandardExpression expression = parser.parseExpression(configuration, arguments, attributeValue)
        final String relativeUrl = (String) expression.execute(configuration, arguments)

        urlService.generateUrl( relativeUrl )
    }

I was hoping that with this addition I would be able to also add links as follows: 我希望通过此添加,我还可以添加如下链接:

<link rel="stylesheet" type="text/css" custom:href="/styles/${user.name}/main.css" />

(example not real, I'm not actually serving user based css :) - but you get the idea) (示例不是真实的,我实际上不是在为基于用户的CSS服务:)-但您知道了)

But that then breaks my first simple example, as the literal URL string is not an expression, to get around this I seem to have to also add single quotes to my URL so the expression parser knows its a string not a variable. 但这打破了我的第一个简单示例,因为文字URL字符串不是表达式,要解决这个问题,我似乎还必须在URL中添加单引号,以便表达式解析器知道其字符串而不是变量。

<link rel="stylesheet" type="text/css" custom:href="'/styles/main.css'" />

This seems really ugly, and probably error prone to have to repeat the single quotes in the simple cases (which will be the majority of the cases) - Is there a nicer way to do this? 这看起来确实很丑陋,而且在简单的情况下(多数情况下,多数情况下)必须重复单引号可能很容易出错-有更好的方法吗? I'm hoping that as the expressions stuff is all curly braces based, there should be a way for the parser to recognise that anything outside of an of the valid expressions can be treated as strings? 我希望表达式内容全部基于花括号,因此解析器应该有一种方法可以识别出有效表达式之外的任何内容都可以视为字符串?

(honestly, it has been a bit of a struggle to get my head around what is going on - the javadocs seem pretty sparse and there don't seem to be many examples - been a lot of trying to read the original source code to work out which classes I should use etc) (老实说,要弄清楚正在发生的事情有点费劲-javadocs看起来很稀疏,似乎没有很多例子-大量尝试阅读原始源代码来工作我应该使用哪些类等)

This answer is after a long time, but will leave it here for anyone who stumbles upon this question. 这个答案已经过了很长时间了,但是对于那些偶然发现这个问题的人来说,它将留在这里。

Yout don't need a custom attribute provessor. 您不需要自定义属性证明。 Thymeleaf has a built in feature that will satisfy your requirements, which is the Link URL output expression ( link to docs ). Thymeleaf具有可满足您要求的内置功能,即链接URL输出表达式( 链接到docs )。

To output a static url write it like this: 要输出静态网址,请像下面这样编写:

<link ... th:href="@{/styles/main.css}" />

As the docs say, when you put / in front, the output URL will be context relative. 正如文档所说,当您将/放在前面时,输出URL将是上下文相关的。 So, if your site is deployed at /mysite , the attribute th:href="@{/styles/main.css}" will be converted to href="/mysite/style/main.css" . 因此,如果您的站点部署在/mysite ,则属性th:href="@{/styles/main.css}"将转换为href="/mysite/style/main.css" If the site is deployed at root context, then the result will be href="/style/main.css" . 如果将站点部署在根上下文中,那么结果将是href="/style/main.css"

The Link URL expression also includes support to insert url parameters like this: 链接URL表达式还支持插入如下网址参数:

<link ... th:href="@{/styles/main.css(un=${user.name})}"/>

The parameters are specified inside parentheses at the end of the url path. 这些参数在url路径末尾的括号内指定。 You can put multiple parameters separated by comas. 您可以将多个参数以逗号分隔。 The result of the above will be href="mysite/styles/main.css?un=rick" , assuming user.name is rick . 假设user.namerick ,则上述结果将为href="mysite/styles/main.css?un=rick"

If you want to insert the parameter into the path then write the following: 如果要将参数插入路径,请输入以下内容:

<link ... th:href="@{/styles/{un}/main.css(un=${user.name})}"/>

The {un} will be replaced by the value of ${user.name} without any parameters appended. {un}将替换为${user.name}的值,而不会附加任何参数。 So the result is: 因此结果是:

<link ... href="mysite/styles/rick/main.css"/>

which is exactly what you need. 这正是您所需要的。

The syntax can seem complicated on first look. 语法乍一看似乎很复杂。 You should read the documentation carefully to understand it. 您应该仔细阅读文档以了解它。 The main point is that Thymeleaf has 3 types of curly braces: 要点是Thymeleaf具有3种花括号:

  • ${...} for "variable expressions" as Thymeleaf docs call them. ${...}代表Thymeleaf文档称之为“变量表达式”的变量。 Which means for Spring EL expressions in your case, or for OGNL espressions in the standard dialect 对于您的情况而言,这意味着使用Spring EL表达式,或者对于标准方言,这意味着使用OGNL表达式
  • #{...} is for localized message keys, and #{...}用于本地化的消息密钥,并且
  • @{...} is for Link URL output, which we covered here @{...}用于链接URL输出,我们在这里介绍

You should read about these, and the whole Thymeleaf expression language in the docs. 您应该在文档中阅读这些内容以及整个Thymeleaf表达语言。 The localized message output also has its own special features like the url output. 本地化的消息输出还具有自己的特殊功能,例如url输出。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM