为什么我无法在Thymeleaf中引用我的嵌套类?

huangapple go评论68阅读模式
英文:

Why can't I reference my nested class in thymeleaf?

问题

${T(com.my.packages.L1.L2.L3.MyEnum).E1} 这段代码在Thymeleaf模板中无法正常工作,会导致以下错误:

EL1005E: 无法找到类型(com.my.packages.L1.L2.L3.MyEnum)

我尝试了一步步的方式来调查是否能引用任何类型。首先,我尝试引用 L1

${T(com.my.packages.L1).E1}

这次我得到了一个错误,告诉我 L1 没有一个名为 E1 的字段。所以结论是,Thymeleaf 成功引用了 L2

我尝试了相同的方法来引用 L2,结果也“成功”了,与引用 L1 的情况相同。

然后我尝试引用 L3,但Thymeleaf再次无法引用该类型。

Thymeleaf是否有某种关于嵌套类的“最大深度”限制?还是我需要在这种情况下使用一种替代的语法?在线查找这个问题非常困难,因为我找到的每篇文章都只展示了上面的语法,这种语法原则上有效,但不考虑嵌套类。

我知道我可能可以将这些类取消嵌套以解决问题,但由于其他原因,我不应该更改结构,因此这不是一个理想的选项。

英文:

I have a nested class structure with multiple levels like this:

public class L1 {
  public static class L2 {
    public static class L3 {
      public enum MyEnum {
        E1,
        E2,
        E3
      }
    }
  }
}

I want to reference MyEnum in my Thymeleaf template. What I expect from the usual syntax is that this should work:

${T(com.my.packages.L1.L2.L3.MyEnum).E1}

However, this does not work and gives me the following error:

> EL1005E: Type cannot be found (com.my.packages.L1.L2.L3.MyEnum)

To investigate I went step by step, checking whether I could reference any type like that at all. So, I simply tried to reference L1 first.

${T(com.my.packages.L1).E1}

This time I got an error telling me that L1 does not have a field E1. So the conclusion here is that Thymeleaf successfully referenced L2.

I tried the same thing with L2 and that "worked" as well. Same as with L1.

Then I tried L3 and Thymeleaf again failed to reference that type.

Does Thymeleaf have some sort of "maximum depth" when it comes to nested classes? Or do I need some alternative syntax in that case? Looking for this problem online is very difficult because every article I find simply shows me the above syntax, which works in principle, and doesn't consider nested classes.

I know I could probably just un-nest the classes and be done with it, but I'm not supposed to change the structure for other reasons, so that's a sub-optimal option.

答案1

得分: 2

当使用嵌套类时,您需要使用$作为分隔符,而不是.符号。.符号用于包(package),而$用于嵌套类。

如果您执行以下代码:System.out.println(L1.L2.L3.MyEnum.class.getName()); 输出将类似于以下内容:

your.package.L1$L2$L3$MyEnum

要在表达式中使用枚举,您必须遵循相同的命名模式。

${T(com.my.packages.L1$L2$L3$MyEnum).E1}${T(com.my.packages.L1$L2$L3.MyEnum).E1} 应该能完成任务。

英文:

When using nested classes you need to use $ as the separator and not the . notation. The . is for package while the $ is used for nested classes.

If you would do a System.out.println(L1.L2.L3.MyEnum.class.getName()); The output would be something like the following

your.package.L1$L2$L3$MyEnum

To use the enum in an expression you have to follow the same naming pattern.

${T(com.my.packages.L1$L2$L3$MyEnum).E1} or ${T(com.my.packages.L1$L2$L3.MyEnum).E1} should do the trick.

huangapple
  • 本文由 发表于 2023年2月23日 21:04:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/75545221.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定