进入课堂领域 - 百里香,春天

时间:2018-05-17 10:01:36

标签: java html spring spring-boot thymeleaf

如何打印List中的所有值,这是我班级中的字段?

模型

@Component
public class ResponseObject {

   public List<Integer> iWantIt = null;

   //GETTERS SETTERS
}

查看

<div th:if="${T(com.example.test.model.ResponseObject).iWantIt } != null">
  <ul th:each="wanted : ${T(com.example.test.ResponseObject).iWantIt}">
    <li th:text="${wanted}"/>
  </ul>
</div>

我用整数设置这个列表,这部分肯定是有用的。

我得到Exception评估SpringEL表达式错误

  

引起:org.springframework.expression.spel.SpelEvaluationException:EL1008E:属性或字段&#39; iWantIt&#39;无法在类型&#39; com.example.test.model.ResponseObject&#39;的对象上找到 - 可能不公开或无效?

1 个答案:

答案 0 :(得分:0)

您需要在模型中添加ResponseObject。

在你的控制器中写这个

@Controller
public class HelloController {

    @Autowired
    ResponseObject responseObject
    @RequestMapping(value = "/hello", method = RequestMethod.GET)
    public String hello(ModelMap model) {
        model.addAttribute("responseObject", new ResponseObject());
        return "Hello World";
    }
}

然后按照以下方式访问百里香的模型

<div th:if="${responseObject.iWantIt != null}">
  <ul th:each="wanted : ${responseObject.iWantIt}">
    <li th:text="${wanted}"/>
  </ul>
</div>