首页 > 解决方案 > 为什么 th:text 不想显示在 main.html 模板中,而它显示在其他页面中?

问题描述

我正在练习 Spring Security+Thymeleaf,我有一个带有页眉和页脚的模板,main.html和一个登录页面,其中登录内容是。我成功地将登录内容“包装”到模板中,但唯一不想工作的是main.html 模板页脚中th:text="${#dates.format(dateTime, 'yyyy-MM-dd')}"的“和th:text="${yourip}”。这些在我设置后在其他页面中工作控制器类。我确保在 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";
    }
    }

配置类的片段

@EnableGlobalMethodSecurity(securedEnabled=true)
@EnableWebSecurity
@Configuration
public class SecurityConf extends WebSecurityConfigurerAdapter {

    String[] staticResources  =  {
            "/css/**",
            "/layouts/main"

        };

    @Bean
    public UserDetailsService userDetailsService() {
        return super.userDetailsService();  
    }


    private UserDetailsService userService;

    @Autowired
    public void setUserService(UserDetailsService userService) {
        this.userService = userService;
    }
    @Autowired
    public void configureAuth(AuthenticationManagerBuilder auth) {

        try {
            auth
            .inMemoryAuthentication()
                .withUser("user")
                .password("user")
                .roles("USER");
        } catch (Exception e) {
             System.out.println(" " + e.getMessage());
        }
    }

    @Override
    public void configure(WebSecurity web) {
        web
            .ignoring()
            .antMatchers(staticResources);


    }
    @Override
    public void configure(HttpSecurity httpSec) {


        try {
            httpSec.authorizeRequests()

                .antMatchers(staticResources).permitAll()
                .requestMatchers(PathRequest.toStaticResources().atCommonLocations()).permitAll()
                .antMatchers("/admin/**").hasRole("ADMIN")
                .antMatchers("/registration").permitAll()
                .antMatchers("/reg").permitAll()
                .antMatchers("/log").permitAll()
                .antMatchers("/main").permitAll()
                .antMatchers("/addUser").permitAll()
                .anyRequest().authenticated()
            .and()
                .formLogin()
                .loginPage("/login").permitAll()
                .and().rememberMe().tokenValiditySeconds(60*60*7).key("message")
                .and()
        .logout()
            .logoutSuccessUrl("/login?logout")
            .permitAll();
        } catch (Exception ex) {
            System.out.println(" " + ex.getMessage());
        }   
}

标签: javaspring-bootthymeleaf

解决方案


只有当我在 WebConf 类中将 HIGHEST_PRECEDENCE 更改为 LOWEST_PRECEDENCE 时,它才对我有用:

@Configuration
public class WebConf implements WebMvcConfigurer {

    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/login").setViewName("auth/login");
        registry.setOrder(Ordered.LOWEST_PRECEDENCE);
    }

}

这显然不是最佳实践,还有其他更好的解决方案吗?


推荐阅读