No message found under code 'j_username' for locale 'zh_CN'.?

原创
2020/03/23 17:01
阅读数 1.8K
package io.j99.app.measure.controller.api;

import io.j99.app.measure.component.TokenManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.servlet.support.RequestContextUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.Enumeration;
import java.util.Locale;

@Controller
@RequestMapping("/api")
public abstract class BaseApiController {
    protected HttpServletRequest request;
    protected HttpServletResponse response;
    protected HttpSession session;
    @Autowired
    protected RestTemplate restTemplate;
    @Autowired
    private TokenManager tokenManager;
    @Autowired
    private MessageSource messageSource;

    protected String message(String key, Object... args) {
        Locale locale = RequestContextUtils.getLocale(request);
        return messageSource.getMessage(key, args, locale);
    }


    @ModelAttribute
    public void setReqAndRes(HttpServletRequest request, HttpServletResponse response) {
        this.request = request;
        this.response = response;
        this.session = request.getSession();
    }

    protected final TokenManager.Submit submitToken() {
        return tokenManager.submitToken();
    }

    protected final boolean canSubmit() {
        Enumeration<String> names = request.getParameterNames();
        String key = null;
        String value = null;
        if (names != null) {
            while (names.hasMoreElements()) {
                String name = names.nextElement();
                if (name.startsWith("_submit")) {
                    key = name;
                    value = request.getParameter(name);
                    break;
                }
            }
        }
        if (StringUtils.hasText(key) && StringUtils.hasText(value)) {
            return tokenManager.canSubmit(new TokenManager.Submit(key, value));
        }
        return true;

    }

    protected final void removeSubmit(TokenManager.Submit submit) {
        tokenManager.removeSubmit(submit);
    }
}  

spring 国际化问题

通过

String message = message("account.verifycode", code);

这里读取

messages_zh_CN.properties

messages_en_US.properties

里面的account.verifycode=\u3010AIDU China\u3011Your verification code is {0}, valid for 5 minute

account.verifycode=\u60A8\u7684\u9A8C\u8BC1\u7801\u662F{0},\u6709\u6548\u671F5\u5206\u949F\u3002

展开阅读全文
打赏
0
0 收藏
分享
加载中
更多评论
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部