Search code examples
javaspringthymeleaf

How to transfer function value to thymeleaf


I want to transfer the text to thymeleaf, so that depending on the value of the function in verificationPage the text is displayed

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";
}   

the value of the message in the function will not be, I transferred to the template, but I don’t know how to take the value of this function, that is, true or false to understand which message to show

<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>

How to transfer function value to thymeleaf?

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";

}

also with the message here, but another template


Solution

  • I would suggest you to send true or false value through controller

    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";
    }   
    

    And in the html you can try

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

    Updated`

    In your case you can just print message alone you dont need to haveifcondition at all.

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