Spring - 如何在所有URL中显示子菜单

时间:2018-03-01 16:21:50

标签: mysql spring spring-boot thymeleaf

我正在使用spring boot + thymeleaf,我将所有片段分为页眉,页脚,......
这是我的标题

<div th:fragment="header" th:remove="tag">
	<!--header-->
	<div class="logo">
		<div class="container">
			<div class="logo-info">
				<a href="index.html">
					<h1>Logo</h1> 
					<!-- <span class="glyphicon glyphicon-grain" aria-hidden="true"></span> -->
				</a>
			</div>
		</div>
	</div>
	<!--//header-->
	<!--navigation-->
	<div class="top-nav">
		<nav class="navbar navbar-default">
			<div class="container">
				<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1">Menu</button>
			</div>
			<!-- Collect the nav links, forms, and other content for toggling -->
			<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
				<ul class="nav navbar-nav">
					<li class="hvr-bounce-to-bottom active"><a href="index.html">Home</a></li>
					<li class="hvr-bounce-to-bottom"><a href="about.html">About</a></li>
					<li><a href="#" class="dropdown-toggle hvr-bounce-to-bottom" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">Gallery<span class="caret"></span></a>
						<ul class="dropdown-menu" th:each="category: ${listCategory}">
							<li><a class="hvr-bounce-to-bottom" th:href="@{/category/${category.getId()} } " th:text="${category.getName()}"></a></li>
						</ul></li>
					<li class="hvr-bounce-to-bottom"><a href="codes.html">Short Codes</a></li>
					<li class="hvr-bounce-to-bottom"><a href="contact.html">Contact Us</a></li>
				</ul>
				<div class="clearfix"></div>
			</div>
		</nav>
	</div>
	<!--//navigation-->
</div>

这是图片
this is picture for example

我想将数据从mysql显示到'Gallery'菜单(Gallery1,Gallery2,Gallery3) 所以当我访问我的网站时,所有网址都会在“图库”中显示子菜单

更新:这是控制器

@Controller
public class CategoryController {
	@Autowired
	private CategoryService categoryService;

	@RequestMapping(value = "/category/{id}")
	public String getCategoryByID(Model model, @PathVariable(name = "id") Long id) {
		List<CategoryDTO> categoryDTOs = categoryService.getAllCategory();
		model.addAttribute("listCategory", categoryDTOs);
		return "common/header";
	}
}

1 个答案:

答案 0 :(得分:0)

我也是Spring和Thymeleaf的新手,但是尝试做一个Repository

     public interface CategoryRepository extends 
           JpaRepository<CategoryDTO, String>{
     }

控制器:

      @Controller
      public class CategoryController {

@Autowired
private categoryRepository categoryRepository;

@RequestMapping(value = "/category")
public String getCategoryByID(Model model) {
    List<CategoryDTO> categoryDTOs = 
            categoryRepository.findAll();

    model.addAttribute("listCategory", categoryDTOs);
    return "common/header";
}
 }

在Thymeleaf尝试做这样的事情:

       category: ${listCategory}"  th:value=" ${file.fileName}"
       <li><a class="hvr-bounce-to-bottom" 
        th:value="${category.getName()}"></a></li>