繁体   English   中英

hotw 将一个fragment的动态数据加载到thymeleaf项目中的另一个HTML页面

[英]Hotw to load the dynamic data of a fragment to another HTML page in thymeleaf project

我有一个带有切换和折叠组件的项目的动态加载版本(动态 URL)的侧边栏导航,当我单击展开按钮时,它将显示 URL 列表。 当我点击侧边栏中的任何 URL 时,索引页面将再次显示基于从侧边栏中选择的版本的动态 URL 数据。 当我点击索引页面上的 URL 时,它会重定向到另一个页面,但侧边栏中没有数据。

    I want the dynamic data loaded in fragment to be passed to all the pages using thymeleaf?
    
 Here is an example:
        
 nav.HTML
    
    
    <div th:fragment="sidebar2">
            <div class="container">
                <nav id="sidebarMenu"
                    class="col-md-3 col-lg-2 d-md-block sidebar collapse bg-light">
                    <div class="sidebar-sticky pt-3">
                        <ul class="nav flex-column" id="accordionSidebar">
                            <li class="nav-item"><a type="button" id="collapse"
                                data-toggle="collapse" data-target="#collapseExample"
                                aria-expanded="false" aria-controls="collapseExample"> <span
                                    class="menu-title">PI Versions</span> 
                            </a>
    
                                <div class="collapse" id="collapseExample">
                                    <ul class="nav flex-column sub-menu" id="collapseExample2">
                                         <li th:each="color : ${coloring}"><a
                                            th:href="@{/toList(color=${color})}" th:text="${color}"> ></a></li>  
                                    </ul>
                                </div></li>
                        </ul>
                    </div>
    </nav>
    </div>
    </div>
    
    
Here is my index.html:

    <div th:replace="fragments/nav :: sidebar2"></div>
<main role="main" class="container">

    <div class="starter-template">
        <h1>Spring Boot Web Thymeleaf Example</h1>
        <h2>
            <span th:text="'Hello, ' + ${message}"></span>
        </h2>
    </div>

    <ol>
        <li th:each="color2 : ${colors}" th:text="${color2}"></li>
    </ol>
    
    <span th:with="convertedList=${#lists.toList(colors)}">
    converted list size: <span th:text="${#lists.size(convertedList)}"/></span>
</span>

这是我的java控制器代码:

@Controller 公共类 HomeController {

@RequestMapping("/")
public ModelAndView usingToList(Model model) {
    List<String> colors = new ArrayList<>();
    colors.add("green");
    colors.add("yellow");
    colors.add("red");
    colors.add("blue");
    model.addAttribute("colors", colors);
    
    List<String> colors2 = new ArrayList<>();
    colors2.add("pinkish");
    colors2.add("green");
    colors2.add("yellow");
    colors2.add("red");
    colors2.add("blue");
    model.addAttribute("coloring", colors2);
    ModelAndView mv = new ModelAndView();   
    mv.setViewName("index");
    return mv;
    
}



@GetMapping("/toList")
public String usingToList2(Model model,String color) {
    List<String> colors2 = new ArrayList<>();
    if(color.equals("pinkish"))
    {
        colors2.add("pinkish");
        colors2.add("amity");
        colors2.add("pimity");
    }
    model.addAttribute("colors", colors2);
    
    return "index";
}

}

任何人都可以帮助我实现这一目标,在此先感谢。

不确定是否很好地理解了这个问题,但是在使用此链接时:

th:href="@{/toList(color=${color})}"

您将一个参数传递给您需要在控制器中检索的 URL,例如:

 public String usingToList2(Model model,@RequestParam(name = "color" String color) {...}

也许它会有所帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM