【问题标题】:While working with internationalization in spring boot, why messages.getmessage() returns null?在 Spring Boot 中使用国际化时,为什么 messages.getmessage() 返回 null?
【发布时间】: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


    【解决方案1】:

    如果您的 message 变量是视图模型,则不应在 html(Thymeleaf 视图)中为其使用 param. 前缀。来自百里香文档:

    param :用于检索请求参数。 ${param.foo} 是一个 String[]foo 请求参数的值,所以 ${param.foo[0]} 通常用于获取第一个值。

    所以,最后还是用message吧。

    更新

    您还应该避免重定向,因为您的 message 变量将在您重定向后立即丢失。除此之外,您应该重定向到控制器本身。

    换行:

    return "redirect:/home.html?lang=" + locale.getLanguage();
    

    与:

    if (request.getParameterMap().size() == 0)
        return "redirect:/registrationConfirm?lang=" + locale.getLanguage();
    // ...
    return "home";
    

    但是,为了提高效率,将上述代码中的 if 块移动到 registrationConfirm() 方法体的第一行。

    我使用 spring-boot 1.5.6 和 Spring MVC 4.3 对此进行了测试

    【讨论】:

    • 感谢@Leone 的回答,但是当使用 message[0] 而不是 param.message[0] 时,我遇到了同样的错误
    • 我对此的更新:在 MvcConfig.java 中添加 @EnableWebMvc 注释时,它正在运行,我可以正确看到 thymeleaf 中的消息。但是由于使用了spring boot,我不想添加这个注释
    • 感谢您的回复,但不幸的是它对我不起作用。它将我重定向到 registrationConfirm.html 但同样的错误。也许我的配置或 bean 有问题?
    • 您还在收到Cannot index into a null value 错误吗?您是否重新启动了您的应用程序?
    • 还有一件事是您应该为return "redirect:/home.html?lang=" + locale.getLanguage(); 部分和它下面的部分进行重定向(如上面的答案):return "redirect:/wrong.html?lang=" + locale.getLanguage();
    猜你喜欢
    • 2016-02-23
    • 1970-01-01
    • 1970-01-01
    • 2023-01-14
    • 2016-04-04
    • 2015-07-07
    • 2020-04-22
    • 2021-12-18
    • 1970-01-01
    相关资源
    最近更新 更多