Search code examples
spring-bootsessionthymeleaf

Exception evaluating SpringEL expression: "#session.removeAttribute('message') in thymeleaf"


I am trying to remove my view like this from my html file:

<div th:if="${session.message}" th:classappend="${session.message.type}" class="alert" role="alert">
    <p class = "text-center " th:text="${session.message.content}"></p>
    <th:block th:text="${#session.removeAttribute('message')}"></th:block>
</div>

But it throws error

Caused by: org.attoparser.ParseException: Exception evaluating SpringEL expression: "#session.removeAttribute('message')" (template: "signup" - line 19, col 43)
    at org.attoparser.MarkupParser.parseDocument(MarkupParser.java:393)
    at org.attoparser.MarkupParser.parse(MarkupParser.java:257)
    at org.thymeleaf.templateparser.markup.AbstractMarkupTemplateParser.parse(AbstractMarkupTemplateParser.java:230)
    ... 48 more

Here is my controller code...

    User addedUser = this.userRepository.save(user);
    model.addAttribute("user", new User());
    session.setAttribute("message", new Message("Successfully Register", "alert-success"));
    return "signup";
} catch (Exception e) {
    e.printStackTrace();
    model.addAttribute("user", user);
    session.setAttribute("message", new Message("Server Error !!" + e.getMessage(), "alert-danger"));
    return "signup";
}

Can anyone ans me how can I fix this issue...?

Trying to remove attribute from session but unable to do so...


Solution

  • this method is no longer used you have to create a utility bean and create a session removing method then use that method in thymeleaf to remove the attribute

    class:

        package com.Contact.config;
    
    import org.springframework.context.annotation.Bean;
    import org.springframework.stereotype.Component;
    import org.springframework.web.context.request.RequestContextHolder;
    import org.springframework.web.context.request.ServletRequestAttributes;
    
    import jakarta.servlet.http.HttpSession;
    
    @Component
    public class sessionUtilityBean {
        
        @Bean
         public void removeMessageFromSession() {
                try {
                    System.out.println("removing message form session ");
                    HttpSession session = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest().getSession();
                    session.removeAttribute("message");
                    
                    
                    
                    
                } catch (Exception e) {
                    e.printStackTrace();
    
                
                }
    
    }
    

    }

    then use the method in the thymeleaf:

    <div th:if="${session.message}"
                            th:classappend="${session.message.type}" class="alert "
                            role="alert">
                            <p th:text="${session.message.content}"></p>
                            <th:block
                                th:text="${@sessionUtilityBean.removeMessageFromSession()}"></th:block>
                        </div>