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

标签 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显示到“图库”菜单(Gallery1,Gallery2,Gallery3) 因此,当我访问我的网站时,所有网址都会在“图库”中显示子菜单

更新:这是 Controller

@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";
	}
}

最佳答案

我也是 Spring 和 Thymeleaf 的新手,但尝试创建一个存储库

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

Controller :

      @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>

关于mysql - Spring - 如何在所有 URL 中显示子菜单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49054191/

相关文章:

java - Spring Data、REST 和 ManyToMany 关系

java - 如何在 JBOSS EAP6.4 的自定义模块后使用 Spring Boot 应用程序使用自定义 module.xml 并进行部署

spring - 如何在 spring boot 中使用 mockito 模拟 deleteById

java - 我收到“存在意外错误(类型=未找到,状态=404)”错误?

php - 您的 SQL 语法有误;查看与您的 MySQL 服务器对应的手册

PHP文件显示空白页

Python 多个 MySQL 插入

php - mysql连接查询有很多字段

java - 在 Spring 中,为什么具有作用域原型(prototype)的服务会实例化多次?

java - 处理更改的 ENUM 定义 - 数据库