【问题标题】:thymeleaf block is not show when condition of th:if is true当 th:if 的条件为真时,不显示百里香叶块
【发布时间】:2018-10-05 10:33:29
【问题描述】:

我这里有块:

<th:block th:if= "${not #lists.isEmpty(listGuide)}">
                <table class="table" style="margin-left: 100px" >
                    <thead>
                        <tr>
                            <td><b>Tên</b></td>
                            <td><b>Địa chỉ</b></td>
                            <td><b>Phone</b></td>
                            <td><b>Kinh nghiệm</b></td>
                        </tr>
                    </thead>
                </table>
                </th:block>

控制器是

@PostMapping("/operator/search")
public String searchGuide(HttpServletRequest request, Model model) {
            String location = request.getParameter(Constant.PARAMETER_LOCATION);
            String language = request.getParameter(Constant.PARAMETER_LANGUAGE);
            String gender = request.getParameter(Constant.PARAMETER_GENDER);
            String type = request.getParameter(Constant.PARAMETER_TYPE);
            model.addAttribute("listGuide",
                    operatorService.findGuide(location, gender, type, language));
            log.info("size list guide search: "
                    + operatorService.findGuide(location, gender, type, language).size());
            return Constant.VIEW_REDIRECT_FIND_GUIDE;
}

此日志“大小列表指南搜索:19901”但块不显示。为什么?有人帮我吗?

更新 VIEW_REDIRECT_FIND_GUIDE 是

public static final String VIEW_REDIRECT_FIND_GUIDE = "redirect:/operator/searchguide";

/operator/searchguide 的控制器是:

public String findGuide(HttpServletRequest request, Model model) {
            Principal principal = request.getUserPrincipal();
            User user = userDetailService.findByUsername(principal.getName());
            Operator operator = operatorService.findByUserId(user.getId());
            model.addAttribute(Constant.ENTITIY_OPERATOR, operator);
            model.addAttribute(Constant.ENTITIY_LIST_LANGUAE, (List<Language>) languageService.findAll());
            model.addAttribute(Constant.ENTITIY_LIST_LOCATION, (List<Location>) locationService.findAll());
            return "find-guider";

【问题讨论】:

  • 在 HTML 中打印尺寸以在 else 块 ${#lists.size(listGuide)} 中查看。如果您重定向到此页面,则可能必须使用 Flash Scope。
  • 是的,Constant.VIEW_REDIRECT_FIND_GUIDE的值是多少?
  • @bphilipnyc 我已经更新了
  • @VenuDuggireddy:查看我的更新,我尝试使用 ${#lists.size(listGuide)} 但它错误:无法获取 null 的列表大小

标签: java html thymeleaf


【解决方案1】:

正如我在之前的评论中所说,您必须使用 Flash Scope,因为您要重定向到一个页面。试试下面的方法

@PostMapping("/operator/search")
public String searchGuide(HttpServletRequest request, RedirectAttributes attributes) {
            String location = request.getParameter(Constant.PARAMETER_LOCATION);
            String language = request.getParameter(Constant.PARAMETER_LANGUAGE);
            String gender = request.getParameter(Constant.PARAMETER_GENDER);
            String type = request.getParameter(Constant.PARAMETER_TYPE);
            //model.addAttribute("listGuide", operatorService.findGuide(location, gender, type, language));
            attributes.addFlashAttribute("listGuide",peratorService.findGuide(location, gender, type, language));
            log.info("size list guide search: "
                    + operatorService.findGuide(location, gender, type, language).size());
            return Constant.VIEW_REDIRECT_FIND_GUIDE;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-12
    • 2016-08-14
    • 2015-11-29
    • 1970-01-01
    相关资源
    最近更新 更多