Я практикую Spring Security + Thymeleaf, и у меня есть шаблон с верхним и нижним колонтитулами, main. html и страница входа , где находится содержимое для входа. Я успешно сделал входное содержимое «обернутым» в шаблон, но единственное, что не хочет работать, это th:text="${#dates.format(dateTime, 'yyyy-MM-dd')}"
и th:text="${yourip}
"в нижнем колонтитуле основного шаблона. html. Они работают в другие страницы после того, как я установил их в классе контроллера. Я убедился в классе Config, что / templates / layouts / main игнорируется (.antMatchers(staticResources).permitAll()
), но все равно не хочет работать. Я не знаю, как следует Я определяю страницу шаблона в классе контроллера, так как шаблон - это просто шаблон в отличие от страницы входа.
ОБНОВЛЕНИЕ: Я просто попытался поместить нижний колонтитул с указанными переменными контекста th: text в представлении входа в систему и не работает, но работает, когда я помещаю их, например, в представление регистрации.
main. html (/ templates / layouts / main)
<!doctype html>
<html xmlns:th="http://www.thymeleaf.org" xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout" xmlns:sec="http://www.thymeleaf.org/extras/spring-security">
<head>
<!-- Required meta tags -->
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script> -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js"></script>
<script src="https://kit.fontawesome.com/2dafcd6f62.js" crossorigin="anonymous"></script>
<!-- Bootstrap CSS // Let's connect the CSS to the grid system -->
<link rel="stylesheet" type="text/css" href="css/bootstrap-4.3.1-dist/css/bootstrap.min.css">
<!--CSS-->
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<link rel="stylesheet" type="text/css" href="css/style.css" th:href="@{css/style.css}">
<link rel="stylesheet" type="text/css" href="css/bootstrap-4.3.1-dist/css/bootstrap-grid.min.css" th:href="@{css/bootstrap-4.3.1-dist/css/bootstrap-grid.min.css}">
<link rel="stylesheet" type="text/css" href="css/bootstrap-4.3.1-dist/css/bootstrap.min.css" th:href="@{css/bootstrap-4.3.1-dist/css/bootstrap.min.css}">
<link href="https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet" integrity="sha384-wvfXpqpZZVQGK6TAh5PVlGOfQNHSoD2xbE+QkPxCAFlNEevoEH3Sl0sibVcOQVnN" crossorigin="anonymous">
<title layout:title-pattern="$LAYOUT_TITLE - $CONTENT_TITLE">Using Spring Security Thymeleaf</title>
</head>
<body>
<div class="jumbotron text-center container-color">
<h1 class="home-title font-text-style">Text</h1>
<p>Text, text, text...</p>
</div>
<div layout:fragment="content">
the login content should get here
</div>
<footer class="text-center footer bg-dark">
<div class="font-style">
<div>
Today is <span th:text="${#dates.format(dateTime, 'yyyy-MM-dd')}">Today is</span></div>
<br>Your IP address is <span class="yourIp" th:text="${yourip}">IP as: 127.0.0.1</span>
</div>
</footer>
</body>
</html>
login. html (/ templates / auth / login)
<!doctype html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:th="http://www.thymeleaf.org"
xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3"
xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"
lang="en" layout:decorate="~{layouts/main}">
<head>
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<!-- Bootstrap CSS -->
<link rel="stylesheet" type="text/css" href="css/bootstrap-4.3.1-dist/css/bootstrap-grid.min.css">
<link rel="stylesheet" type="text/css" href="css/bootstrap-4.3.1-dist/css/bootstrap.min.css">
<!--We are choosing these two to let the browser to load faster-->
<link rel="stylesheet" type="text/css" href="css/logincss.css">
<!--this is the last one so that we can override previous boostrap styles if we want-->
<head>
<body class="signin-body">
<div class="container signin-container" layout:fragment="content">
<div class="row">
<div class="col"></div>
<div class="col-sm-12 col-md-8">
<div class="card signin-card">
<div class="card-block">
<!-- <img src="images/logindetails.png" width="50%" height="50%" class="img-fluid signin-img"> -->
<form name="login" th:action="@{/login}" method="post" class="signin-form">
<div class="form-group">
<h2 class="form-signin-heading">Please sign in</h2>
<div th:if="${param.error}" class="alert alert-danger">Wrong username and password</div>
<div th:if="${param.logout}" class="alert alert-success">You successfully logged out</div>
<label for="username" class="sr-only">Username</label>
<input type="text" id="username" name="username" class="form-control" placeholder="Username" required="true">
<label for="password" class="sr-only">Password</label>
<div class="form-group">
<input type="password" id="password" name="password" class="form-control" placeholder="Password" required="true">
</div>
</div>
<button class="btn btn-lg btn-primary btn-block signin-btn" type="submit">Login</button>
<div class="custom-control custom-checkbox">
<input type="checkbox" class="custom-control-input" id="customCheck1">
<!-- <label class="custom-control-label" for="customCheck1">Remember me <a href="#"> Need help?</a></label> -->
<a class= "new-account" href="/registration">Create New Account</a>
</div>
</form>
</div>
</div>
</div>
<div class="col"></div>
</div>
</div>
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js"></script>
<script src="css/bootstrap-4.3.1-dist/js/bootstrap.min.js"></script>
</body>
</html>
фрагмент ApiController. java
@Controller
public class ApiController {
private final Logger log = LoggerFactory.getLogger(this.getClass());
private EmailService emailService;
private UserService userService;
private FormUserService formUserService;
@Autowired
public void setFormUserService(FormUserService formUserService) {
this.formUserService = formUserService;
}
@Autowired
public void setEmailService(EmailService emailService) {
this.emailService = emailService;
}
@Autowired
public void setUserService(UserService userService) {
this.userService = userService;
}
@RequestMapping("/")
public String home(Model model) {
model.addAttribute("serverTime", new Date());
model.addAttribute("itskills", "IT skills");
model.addAttribute("references", "References");
model.addAttribute("qualifications", "Qualifications");
model.addAttribute("myproject", "My Project");
model.addAttribute("it", "IT");
model.addAttribute("vaadin", "VAADIN");
model.addAttribute("java", "JAVA");
model.addAttribute("exam", "EXAM");
model.addAttribute("yourip", userService.userIpAddress());
return "index";
}
@PostMapping("/login")
public String login(Model model) {
model.addAttribute("dateTime", new Date());
model.addAttribute("yourIp", userService.userIpAddress());
return "auth/login";
}
}
фрагмент класса конфигурации
* 10 32 *