我试图在 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/