Reputation: 9095
In my current spring-boot project, I have this thymeleaf code:
<th:block th:each="item : ${menu}">
...
<a th:href="@{/__${menu2}__/listagem}">
<i class="icon-asterisk"></i>
<span th:utext="${item}"></span>
</a>
...
<th:block>
where I am trying iterate the two lists (menu
and menu2
) in a single loop th:each
. For menu2
I try this:
${menu2[itemStat.index]}
but I am getting the error:
org.springframework.expression.spel.SpelEvaluationException: EL1012E:(pos 5): Cannot index into a null value
What is the right way to access this second list inside the loop?
UPDATE
controller:
@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();
}
ApplicationClasses:
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;
}
Upvotes: 0
Views: 3083
Reputation: 8434
Not having more details i'm making big assumptions when providing this answer.
<block th:each="item,itemStat : ${menu}">
<span th:text="*{menu2[__${itemStat .index}__].something}"></span> // assuming its a object that has parameter called somethin
</block>
Your menu 2 list i believe is empty hence the null too.
Upvotes: 3