嗨,我有模板,它使用默认的布局和片段作为标题和菜单。我想要做的是检测当前的页面url,这样我就可以添加所需的css来显示活动。
我尝试过th:classappend=“@{‘} == 'find-all'?'active':’”,但是这总是将当前路径呈现为空白(即当前页面)。我还尝试了${#request.getCurrentPath},但出现了一个错误,即无法找到值或为null。
URL是/find--所以它应该检测到这一点并应用css,但它似乎不起作用。
html片段代码如下:
<div class="header" th:fragment="header">
<nav class="navbar navbar-expand-md navbar-dark bg-dark">
<a class="navbar-brand" th:href="@{/}">Time Keeping/Time Sheets</a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbar" aria-controls="navbar"
aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbar">
<ul class="navbar-nav mr-auto">
<li class="nav-item" th:classappend="@{''} == 'find-all'? 'active': ''">
<a class="nav-link" th:href="@{/find-all}">Find All</a>
</li>
<li class="nav-item">
<a class="nav-link" th:href="@{/add-new-entry}">Add new Time Entry</a>
</li>
</ul>
</div>
</nav>
Maven依赖关系片段,
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb-reactive</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
<dependency>
<groupId>nz.net.ultraq.thymeleaf</groupId>
<artifactId>thymeleaf-layout-dialect</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>de.flapdoodle.embed</groupId>
<artifactId>de.flapdoodle.embed.mongo</artifactId>
</dependency>
<dependency>
<groupId>io.projectreactor</groupId>
<artifactId>reactor-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>正如下面所要求的,只是使用的控制器之一。
@Controller
public class HomeController {
private static final String currentMonthName = LocalDateTime.now().getMonth().getDisplayName(TextStyle.FULL, Locale.ENGLISH);
private final TimeKeepingEntryService service;
public HomeController(TimeKeepingEntryService service) {
this.service = service;
}
@GetMapping("/")
public Mono<String> index(Model model) {
model.addAttribute("metaTitle", "Time Keeping Application");
model.addAttribute("month", currentMonthName);
model.addAttribute("timeEntries", service.findByMonth(currentMonthName));
return Mono.just("index");
}
}发布于 2018-10-30 23:32:59
您可以使用Themeleaf中的以下代码获取当前页面的URL。
th:value="${T(org.springframework.web.servlet.support.ServletUriComponentsBuilder).fromCurrentRequest()}"更新
因为,使用Thymeleaf获取当前URL似乎不起作用,所以我们可以做一个解决办法。我们可以在@Controller中获取当前URL,并通过属性发送它。这可以使用HttpServletRequest来实现。代码最终会看起来类似于下面的代码。
@GetMapping("/")
public Mono<String> index(Model model, HttpServletRequest request) {
model.addAttribute("metaTitle", "Time Keeping Application");
model.addAttribute("month", currentMonthName);
model.addAttribute("timeEntries", service.findByMonth(currentMonthName));
model.addAttribute("currentUrl", request.getRequestURL().toString());
return Mono.just("index");
}现在,您需要做的就是在模板中使用这个新属性。
<p th:text="${currentUrl}"></p>更新2
由于最后的任何选项都不起作用,所以您可以尝试使用jQuery。
$(document).ready(function() {
var url = $(location).attr('href');
$("#id").text(url);
})<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<p id="id"></p>
发布于 2019-04-04 05:57:39
使用Thymeleaf 3.0.x,您可以使用:
${#ctx.getRequest().getPath()}不幸的是,到目前为止,我还没有看到mvc和webflux的任何抽象。
https://stackoverflow.com/questions/53074257
复制相似问题