Привет, у меня есть шаблон, который использует макет по умолчанию и fragemnt для заголовка и меню. Что я пытаюсь сделать, так это определить текущий URL-адрес страницы, чтобы я мог добавить требуемый css для отображения активного.
Я пробовал th: classappend = "@ {''} == 'find-all'? 'Active': ''", однако при этом текущий путь всегда отображается как пустой (т.е. текущая страница). Я также пробовал $ {# request.getCurrentPath} с ошибкой, говорящей, что не могу найти значение или имеет значение null.
URL-адрес / find-all, поэтому он должен обнаружить это и применить 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");
}
}




Вы можете использовать следующий код в Themeleaf, чтобы получить URL-адрес текущей страницы.
th:value = "${T(org.springframework.web.servlet.support.ServletUriComponentsBuilder).fromCurrentRequest()}"
Обновлять
Поскольку получение текущего URL-адреса с помощью Thymeleaf, похоже, не работает, мы могли бы найти обходной путь. Мы могли бы получить текущий наш текущий URL-адрес в нашем @Controller и отправить его через атрибут. Этого можно добиться с помощью 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>Вы пробовали с ${#httpServletRequest.requestURI}?
Да, я получаю следующую ошибку. org.springframework.expression.spel.SpelEvaluationException: EL1007E: свойство или поле 'requestURI' не может быть найдено в null
Вы не возражаете добавить свой метод @Controller, который загружает эту страницу? Похоже, проблема в вашем HttpServletRequest.
Я добавил контроллер для индексной страницы, также он построен с использованием макетов Thymeleaf.
Привет, Ален, спасибо за обновление, у меня нет HttpServletRequest из-за использования netty по умолчанию с webflux. Я пробовал HttpServerRequest и т. д. И не нашел конструктора defualt.
Я добавил опцию с использованием jQuery, она должна работать, так как вам ничего не нужно из Spring.
Трюк с jquery позволил мне получить URL-адрес страницы и добавить активный класс в правый пункт меню.
Превосходно! Рад, что смог помочь!
С Thymeleaf 3.0.x вы можете использовать:
${#ctx.getRequest().getPath()}
К сожалению, пока я не вижу абстракции для mvc и webflux.
Привет, я получаю следующую ошибку org.springframework.expression.spel.SpelEvaluationException: EL1005E: Тип не может быть найден 'org.springframework.web.servlet.support.ServletUriComponent sBuilder' в org.springframework.expression.spel.support.Stoc tor.findType (Standar dTypeLocator.java:11 7) ~ [spring-expression-5.0.9.RELEASE.jar: 5.0.9.RELEASE] в org.springframework.expression.spel.ExpressionState.findType (ExpressionState. jav a: 155) ~ [spring-expression-5.0.9.RELEASE.jar: 5.0.9.RELEASE] в org.springframework.expression.spel.ast.TypeReference.getVal ueInternal (TypeRefer ence.java:68 )