与 thymeleaf 并行迭代两个列表
Iterating two list in parallel with thymeleaf
在我当前的 spring-boot 项目中,我有这个 thymeleaf 代码:
<th:block th:each="item : ${menu}">
...
<a th:href="@{/__${menu2}__/listagem}">
<i class="icon-asterisk"></i>
<span th:utext="${item}"></span>
</a>
...
<th:block>
我尝试在单个循环中迭代两个列表(menu
和 menu2
)th:each
。对于 menu2
我试试这个:
${menu2[itemStat.index]}
但我收到错误消息:
org.springframework.expression.spel.SpelEvaluationException: EL1012E:(pos 5): Cannot index into a null value
在循环中访问第二个列表的正确方法是什么?
更新
控制器:
@ModelAttribute("menu")
public List<String> menu() throws Exception {
return ApplicationClasses.lista_classes_projeto();
}
@ModelAttribute("menu2")
public List<Class<?>> menu2() throws Exception {
return ApplicationClasses.lista_classes_projeto_2();
}
应用程序类:
public static List<String> lista_classes_projeto() throws ClassNotFoundException {
Locale currentLocale = Locale.getDefault();
ResourceBundle messages = ResourceBundle.getBundle("messages", currentLocale);
List<String> lista = new ArrayList<String>();
ClassPathScanningCandidateComponentProvider scanner = new ClassPathScanningCandidateComponentProvider(false);
scanner.addIncludeFilter(new AnnotationTypeFilter(Form.class));
for (BeanDefinition bd : scanner.findCandidateComponents("com.spring.loja.model")) {
Class<?> clazz = Class.forName(bd.getBeanClassName());
lista.add(messages.getString(clazz.getSimpleName()));
}
return lista;
}
public static List<Class<?>> lista_classes_projeto_2() throws ClassNotFoundException {
List<Class<?>> lista = new ArrayList<Class<?>>();
ClassPathScanningCandidateComponentProvider scanner = new ClassPathScanningCandidateComponentProvider(false);
scanner.addIncludeFilter(new AnnotationTypeFilter(Form.class));
for (BeanDefinition bd : scanner.findCandidateComponents("com.spring.loja.model")) {
Class<?> clazz = Class.forName(bd.getBeanClassName());
lista.add(clazz);
}
return lista;
}
没有更多细节,我在提供这个答案时做出了很大的假设。
<block th:each="item,itemStat : ${menu}">
<span th:text="*{menu2[__${itemStat .index}__].something}"></span> // assuming its a object that has parameter called somethin
</block>
我认为您的菜单 2 列表是空的,因此也是空的。
在我当前的 spring-boot 项目中,我有这个 thymeleaf 代码:
<th:block th:each="item : ${menu}">
...
<a th:href="@{/__${menu2}__/listagem}">
<i class="icon-asterisk"></i>
<span th:utext="${item}"></span>
</a>
...
<th:block>
我尝试在单个循环中迭代两个列表(menu
和 menu2
)th:each
。对于 menu2
我试试这个:
${menu2[itemStat.index]}
但我收到错误消息:
org.springframework.expression.spel.SpelEvaluationException: EL1012E:(pos 5): Cannot index into a null value
在循环中访问第二个列表的正确方法是什么?
更新
控制器:
@ModelAttribute("menu") public List<String> menu() throws Exception { return ApplicationClasses.lista_classes_projeto(); } @ModelAttribute("menu2") public List<Class<?>> menu2() throws Exception { return ApplicationClasses.lista_classes_projeto_2(); }
应用程序类:
public static List<String> lista_classes_projeto() throws ClassNotFoundException { Locale currentLocale = Locale.getDefault(); ResourceBundle messages = ResourceBundle.getBundle("messages", currentLocale); List<String> lista = new ArrayList<String>(); ClassPathScanningCandidateComponentProvider scanner = new ClassPathScanningCandidateComponentProvider(false); scanner.addIncludeFilter(new AnnotationTypeFilter(Form.class)); for (BeanDefinition bd : scanner.findCandidateComponents("com.spring.loja.model")) { Class<?> clazz = Class.forName(bd.getBeanClassName()); lista.add(messages.getString(clazz.getSimpleName())); } return lista; } public static List<Class<?>> lista_classes_projeto_2() throws ClassNotFoundException { List<Class<?>> lista = new ArrayList<Class<?>>(); ClassPathScanningCandidateComponentProvider scanner = new ClassPathScanningCandidateComponentProvider(false); scanner.addIncludeFilter(new AnnotationTypeFilter(Form.class)); for (BeanDefinition bd : scanner.findCandidateComponents("com.spring.loja.model")) { Class<?> clazz = Class.forName(bd.getBeanClassName()); lista.add(clazz); } return lista; }
没有更多细节,我在提供这个答案时做出了很大的假设。
<block th:each="item,itemStat : ${menu}">
<span th:text="*{menu2[__${itemStat .index}__].something}"></span> // assuming its a object that has parameter called somethin
</block>
我认为您的菜单 2 列表是空的,因此也是空的。