【问题标题】:Populating Thymeleaf rows, with one of column referenced to another table(Database).Need another column of referenced Table to display填充 Thymeleaf 行,其中一个列引用另一个表(数据库)。需要引用表的另一列来显示
【发布时间】:2018-06-23 10:44:24
【问题描述】:

我是 springboot 的初学者。我遇到了一个情况。好吧,在我解释之前让我给出表结构。

费用

expensesId, categories,

类别

categoryId, categoryname

费用表是指具有categoryId的类别表。

我可以使用从控制器发送的expensesList 填充datatable 以成功查看。但我的问题是我得到columnid 填充但我希望用categoryname 填充该列。我想了一段时间可能的解决方案,我想出了愚蠢的想法,使用 If the condition in thymeleaf of mapping categoryId with categoryName 但我不想去那部分,因为我可以有更多的类别增长未来,代码难以维护。

我真的可以在这方面使用一些帮助。

Link to git project

expensespage link

expenses controller link

如果我在谷歌搜索这个问题时错过了一个可用的解决方案,请提供链接。

抱歉标题部分的英文不好,因为我无法进一步提出问题。

【问题讨论】:

  • 我可以知道拒绝投票的原因吗?因为我没有看到任何正当理由。我发布了问题,但没有引起注意我尝试了各种解决方案,我发布了我找到的解决方案,希望有人能从中受益,而且这个问题也不是重复的。

标签: java spring-boot thymeleaf


【解决方案1】:

我刚刚在每个费用对象中添加了包含 ID 和名称的类别对象 费用对象列表并传递给控制器​​。现在我可以直接在 View with Thymeleaf 中使用 CategoryName 如下

<td th:text="${expense.categories.categoryname}"></td>>

我在控制器中所做的更改。

之前

@RequestMapping(value = "/expenses")
    public String toExpensesPage(Model model) {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        User user = monitorService.getUserByUserName(authentication.getName()).get(0);
        List<Categories>  categoriesList = monitorService.getAllCategories();
        model.addAttribute("expenses",new Expenses());
        if(categoriesList != null) {
            model.addAttribute("categories",categoriesList);
        }
        List<Expenses> expensesList = monitorService.getAllExpensesActive(user.getUserid());
        model.addAttribute("expensesList",expensesList);
        return "common/expensespage";
    }

到。

@RequestMapping(value = "/expenses")
public String toExpensesPage(Model model) {
    Map<Integer,String> categoryMap = new HashMap<Integer, String>();
    Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
    User user = monitorService.getUserByUserName(authentication.getName()).get(0);
    /* getting Category List This contains both categoryId and categoryname */
    List<Categories>  categoriesList = monitorService.getAllCategories();
    /* Using hashmap to map id to name  so we dont need to iterate categoryList or call to database with category Id..*/
    for(int i = 0; i < categoriesList.size();i++){
        categoryMap.put(categoriesList.get(i).getCategoryid(),

categoriesList.get(i).getCategoryname());
        }
        model.addAttribute("expenses",new Expenses());
        if(categoriesList != null) {
            model.addAttribute("categories",categoriesList);
        }
        List<Expenses> expensesList = monitorService.getAllExpensesActive(user.getUserid());
        /*Assigning category object to expenses object*/
        for(int i = 0; i < expensesList.size();i++){
            Categories categories = new Categories();
            categories.setCategoryid(expensesList.get(i).getCategories().getCategoryid());
            categories.setCategoryname(categoryMap.get(expensesList.get(i).getCategories().getCategoryid()));
            expensesList.get(i).setCategories(categories);
        }
        model.addAttribute("expensesList",expensesList);
        return "common/expensespage";
    }

添加了评论的内部代码,说明我是如何做到的。希望有人觉得它有帮助。

【讨论】:

    猜你喜欢
    • 2020-01-11
    • 2017-05-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-29
    • 2010-12-24
    • 1970-01-01
    • 1970-01-01
    • 2016-05-28
    相关资源
    最近更新 更多