java - thymeleaf 模板中 Web 上下文对象的调用方法问题

标签 java spring spring-boot thymeleaf

我试图在 thymeleaf 3.0.3 和 Spring Boot 1.5.1 的模板中调用 Web 上下文对象的方法,例如 #request 和 #response。

我不断收到这样的错误:

org.springframework.expression.spel.SpelEvaluationException: EL1011E: 方法调用:尝试在空上下文对象上调用方法 method(java.lang.String)

这是一个 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";
}

和一个模板:

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

这是一个示例,但在我尝试执行此操作的任何地方都会遇到这些错误。有没有我遗漏的部分?

最佳答案

原来#request 需要是#httpServletRequest

关于java - thymeleaf 模板中 Web 上下文对象的调用方法问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42167681/

相关文章:

java - 数据 JPA - 从存储库中删除实体

java - 是否可以在 spring batch 中跨单个文件进行分区?

java - 测试 Spring boot Camel 应用程序时出现 NullPointerException

java - 使用 java 函数从 Spring boot 调用 Spring 执行器/重启端点

java - setOnClickListener 上的 nullpointerException

java - 如何在Windows下编写Java代码,在Linux下编译运行

java - SessionFactory getCurrencSesion 不适用于 Autowiring

配置服务器上的 spring-cloud-starter-bus-kafka 设置

java - 如何在等待长 POST 请求时显示进度/请等待类型组件

Java : String representation from Integer