如何将功能值转移到百里香

时间:2019-01-17 13:13:48

标签: java spring thymeleaf

我想将文本传输到百里香,以便根据验证页面中函数的值显示文本

public String activate(Map<String, String> model, @PathVariable String code) {
    boolean isActivated = userService.activateUser(code);
    // TODO: change to boolean in template
    if (isActivated) {
        model.put("message", "User Successfully activated");
    } else model.put("message", "Activation code not found");
    return "verificationPage";
}   

该函数中消息的值不是,我转移到了模板,但是我不知道如何获取该函数的值,即是真还是假,以了解显示哪个消息

<div th:if="${#bools.isTrue(message)}"><p>User Successfully activated .</p></div>
<div th:unless="${#bools.isFalse(message)}"><p>Activation code not found.</p></div>

如何将功能值传递给百里香?

public String processForgotPasswordForm(@RequestBody User userData) throws Exception {
    User dbUser;
    String userEmail = userData.getEmail();
    String userName = userData.getUsername();
    if (userEmail == null && userName == null) {
        throw new Exception("Пустое поле");
    }
    if (userEmail != null) {
        dbUser = userService.findUserByEmail(userEmail);
    } else {
            dbUser = userService.findUserByUsername(userName);
    }

    if (dbUser != null) {
        String newPassword = RandomStringUtils.randomAlphanumeric(8);
        String hashPass = DigestUtils.md5Hex(newPassword);
        dbUser.setValidatyTime(new Date());
        dbUser.setTemporaryPassword(hashPass);
        userService.save(dbUser);
        // TODO: move to template
        String message = String.format(
                "Your new temporary password, which will be valid for an hour " + " " + newPassword + "\nYou can log in and change your password to a new one.\n");
        mailSenderService.send(dbUser.getEmail(), "Temporary password", message);
    }
    return "wait";

}

这里也有消息,但还有另一个模板

1 个答案:

答案 0 :(得分:0)

我建议您通过控制器发送truefalse

public String activate(Map<String, String> model, @PathVariable String code) {
    boolean isActivated = userService.activateUser(code);
    // TODO: change to boolean in template
    if (isActivated) {
        model.put("message", "User Successfully activated");
    } else model.put("message", "Activation code not found");
     model.put("isActivated", isActivated);

    return "verificationPage";
}   

在html中,您可以尝试

<div  th:if="${isActivated} == true"><p>User Successfully activated .</p></div>
<div th:unless="${isActivated}"><p>Activation code not found.</p></div>

已更新`

在您的情况下,您可以只打印一条消息,而根本不需要if

<div ><p>${message}</p></div>