旋转的钢笔 2019-08-06 17:27 采纳率: 54.5%
浏览 1441
已结题

springboot freemarker 国际化只显示中文,点击英文切换不起作用?

为什么我这个国际化,点击切换按钮英文不起作用?只显示中文

图片说明

图片说明

图片说明

图片说明

最后附上controller代码

package com.yadu.example.demo.controller;

import com.yadu.example.demo.entity.User;
import com.yadu.example.demo.service.LocaleMessageSourceService;
import com.yadu.example.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;

@RestController
public class Controller {

    @Autowired
    private UserService userService;

    @Autowired
    private MessageSource messageSource;

    /**
     * 获取国际化信息
     */
    @Resource
    private LocaleMessageSourceService localeMessageSourceService;

    @GetMapping("/hi")
    public String index(){
        return "hello spring boot";
    }
    @RequestMapping("/users/{userId}")
    public User getUser(@PathVariable("userId") int userId){
        User user = userService.getById(userId);
        return user;
    }

    /**
     * freemarker模板
     * @param modelAndView
     * @return
     */
    @RequestMapping(value = "/indexFre")
    public ModelAndView indexFre(ModelAndView modelAndView) {
        //设置返回的页面名称
        modelAndView.setViewName("indexFre");
        //要返回在页面的数据
      /*  List<User> userList =  userService.getUsers();
        modelAndView.addObject("userList", userList);*/
        return modelAndView;
    }


    @RequestMapping("/hello")
    public ModelAndView hello(ModelAndView modelAndView){
        String msg3 = localeMessageSourceService.getMessage("welcome");
        modelAndView.setViewName("hello");
        return  modelAndView;

    }



   /* @RequestMapping("/changeSessionLanauage")
    public String changeSessionLanauage(HttpServletRequest request, HttpServletResponse response, String lang){
        System.out.println(lang);
        LocaleResolver localeResolver = RequestContextUtils.getLocaleResolver(request);
        if("zh".equals(lang)){
            localeResolver.setLocale(request, response, new Locale("zh", "CN"));
        }else if("en".equals(lang)){

            localeResolver.setLocale(request, response, new Locale("en", "US"));
        }
        return "redirect:/hello";
    }*/
}
  • 写回答

1条回答 默认 最新

  • 南归北隐 博客专家认证 2019-08-07 09:07
    关注
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 6月13日
  • 已采纳回答 10月31日

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵