Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/336.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 当按键发生变化时,如何使用Thymeleaf Spring方言访问地图?_Java_Spring_Thymeleaf_Spring El - Fatal编程技术网

Java 当按键发生变化时,如何使用Thymeleaf Spring方言访问地图?

Java 当按键发生变化时,如何使用Thymeleaf Spring方言访问地图?,java,spring,thymeleaf,spring-el,Java,Spring,Thymeleaf,Spring El,我正在使用Thymeleaf的Spring方言迭代一些数据,即使用SpEL。文档描述了如何使用数字或带引号的字符串访问数组、列表和地图: <p th:text="${map['key']}"></p> <p th:text="${list[0]}"></p> 但是,这里的键/索引必须是常量,在这种情况下这对我没有帮助,因为我迭代了多个键(甚至不是字符串)。我尝试过两种变体: <ul> <li th:each="ke

我正在使用Thymeleaf的Spring方言迭代一些数据,即使用SpEL。文档描述了如何使用数字或带引号的字符串访问数组、列表和地图:

<p th:text="${map['key']}"></p>
<p th:text="${list[0]}"></p>

但是,这里的键/索引必须是常量,在这种情况下这对我没有帮助,因为我迭代了多个键(甚至不是字符串)。我尝试过两种变体:

<ul>
    <li th:each="key : ${map}">
//SpelParseException: EL1043E:(pos 41): Unexpected token. Expected 'rsquare(])' but was 'lcurly({)'
        <p th:text="${otherMap[${key}]}"></p>
//ConverterNotFoundException: No converter found capable of converting from type java.lang.String to type com.example.MyClass
        <p th:text="${otherMap[key]}"></p>
    </li>
</ul>
  • //Spelparsexception:EL1043E:(位置41):意外令牌。应为'rsquare(]'),但为'lcurly({)'

    //ConverterNotFoundException:未找到能够从java.lang.String类型转换为com.example.MyClass类型的转换器

第二个让我相信SpEL试图在这里使用
key
作为字符串
“key”
,但随后断言
otherMap
不使用字符串键


有没有办法访问
otherMap
的元素?

对我有效的语法是这样的(注意
#th:object
):

虽然SpEL试图将
${otherMap[*{id}]}
(这当然不同,但我还是尝试了)解析为乘法,但它正确地解释了
#对象
符号

据我所知,
#object
是特定于Thymeleaf的,因此如果在SpEL的其他用例中存在类似的问题,这可能在那里不起作用。

注意

您必须使用双下划线
{key}}{code>对其进行预处理,并将其包括在下一级处理中

解决方案

这会解决你的问题

<li th:each="key : ${map}">
    <p th:text="${otherMap[__${key}__]}"></p>
</li>
  • 考虑到你的控制器看起来像这样

    @RequestMapping("/...")
    public String index(Model model) {
    
        Map<String, String> otherMap = new HashMap<>();
        otherMap.put("one", "1111");
        otherMap.put("two", "2222");
        ....
    
        List<String> keys = new ArrayList<>();
        keys.add("one");
        keys.add("two");
        ...
    
        model.addAttribute("otherMap", otherMap);
        model.addAttribute("map", keys);
    
        return "<view name>";
    }
    
    @RequestMapping(“/…”)
    公共字符串索引(模型){
    Map otherMap=newhashmap();
    其他地图。放置(“一”、“1111”);
    其他地图。放在(“二”,“2222”);
    ....
    列表键=新的ArrayList();
    密钥。添加(“一”);
    添加(“两个”);
    ...
    model.addAttribute(“otherMap”,otherMap);
    model.addAttribute(“映射”,键);
    返回“”;
    }
    
    谢谢!我不太清楚,他来自Freemarker。
    @RequestMapping("/...")
    public String index(Model model) {
    
        Map<String, String> otherMap = new HashMap<>();
        otherMap.put("one", "1111");
        otherMap.put("two", "2222");
        ....
    
        List<String> keys = new ArrayList<>();
        keys.add("one");
        keys.add("two");
        ...
    
        model.addAttribute("otherMap", otherMap);
        model.addAttribute("map", keys);
    
        return "<view name>";
    }