Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Issues calling methods on web context objects in thymeleaf template

I am trying to call methods on web context objects such as #request and #response in my templates in thymeleaf 3.0.3 and Spring Boot 1.5.1.

I continually get errors like this:

org.springframework.expression.spel.SpelEvaluationException: EL1011E: Method call: Attempted to call method method(java.lang.String) on null context object

Here is a Controller:

@Controller
public class Controller {

@RequestMapping(method = RequestMethod.GET, value = "/endpoint", produces = "text/html")
public String customerServiceSignin(Model uiModel, HttpServletRequest request) {
    uiModel.addAttribute("attr1", true); // show proper header
    uiModel.addAttribute("attr2", false);
    return "template";
}

and a template:

<html xmlns:th="http://www.thymeleaf.org">
<div>
<div style="...">

    <div class="errorblock" th:unless="${#strings.isEmpty(#request.getAttribute('some_attr'))}" th:utext="${#request.getAttribute('some_other_attr')}"></div>

    <form name='f' action="action" method='POST'>
        <table>
            <tr>
                <td>User:</td>
                <td><input type='text' name='username' value="" />
                </td>
            </tr>
            <tr>
                <td>Password:</td>
                <td><input type='password' name='password' />
                </td>
            </tr>
            <tr>
                <td><input name="submit" type="submit" value="submit" />
                </td>
                <td><input name="reset" type="reset" />
                </td>
            </tr>
        </table>
    </form>
</div>
</div>
</html>

This is one example, but anywhere I attempt to do this I get these errors. Is there a piece I am missing?

like image 849
Peter Samyn Avatar asked Aug 08 '26 06:08

Peter Samyn


1 Answers

Turns out #request needed to be #httpServletRequest

like image 121
Peter Samyn Avatar answered Aug 09 '26 19:08

Peter Samyn



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!