【发布时间】:2018-07-19 18:45:32
【问题描述】:
检查messages.gettmessage()时的结果为null,使用model.addAttribute获取消息以显示在thymeleaf页面上。
我有一个 registrationConfirm 控制器来允许用户确认他们的电子邮件
这是我的注册控制器:
@RequestMapping(value = "/registrationConfirm", method = RequestMethod.GET)
public String confirmRegistration(final HttpServletRequest request, final
Model model, @RequestParam("token") final String token) throws
UnsupportedEncodingException {
Locale locale = request.getLocale();
final String result = userService.validateConfirmationToken(token);
if (result.equals("valid")) {
final User user = userService.getUser(token);
authWithoutPassword(user);
model.addAttribute("message",
messages.getMessage("message.accountVerified", null, locale));
return "redirect:/home.html?lang=" + locale.getLanguage();
}
model.addAttribute("message", messages.getMessage("auth.message." +
result, null, locale));
model.addAttribute("expired", "expired".equals(result));
model.addAttribute("token", token);
return "redirect:/wrong.html?lang=" + locale.getLanguage();
}
我的 MvcConfig 类:
@Configuration
public class MvcConfig extends WebMvcConfigurerAdapter{
@Autowired
private MessageSource messageSource;
@Override
public void addViewControllers(final ViewControllerRegistry registry) {
super.addViewControllers(registry);
registry.addViewController("/index");
registry.addViewController("/registration");
registry.addViewController("/wrong.html");
registry.addViewController("/home.html");
@Override
public void configureDefaultServletHandling(final
DefaultServletHandlerConfigurer configurer) {
configurer.enable();
}
@Bean
public LocaleResolver localeResolver() {
final CookieLocaleResolver cookieLocaleResolver = new
CookieLocaleResolver();
cookieLocaleResolver.setDefaultLocale(Locale.ENGLISH);
return cookieLocaleResolver;
}
@Bean
public LocaleChangeInterceptor localeChangeInterceptor() {
LocaleChangeInterceptor lci = new LocaleChangeInterceptor();
lci.setParamName("lang");
return lci;
}
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(localeChangeInterceptor());
}
请注意,当尝试使用无效令牌确认电子邮件时,该令牌将重定向到 badUser.html,即:
<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml"
xmlns:th="http://www.thymeleaf.org">
<head>
<title th:text="#{label.wrong.title}">wrong</title>
</head>
<body>
<div class="container">
<div class="alert alert-info" th:text="${param.message[0]}">error</div>
<br/>
</div>
</body>
</html>
我无法获取 param.message 并在日志中获取以下错误
请求处理失败;嵌套异常是
org.thymeleaf.exceptions.TemplateProcessingException: Exception evaluating
SpringEL expression: "param.message[0]" (wrong)
Root Cause: org.springframework.expression.spel.SpelEvaluationException:
EL1012E: Cannot index into a null value
抱歉,代码太长了,我试图解决这个问题很长时间了。真的很感激任何帮助!!!!
【问题讨论】:
标签: java spring-mvc spring-boot spring-security thymeleaf