fmt:message prints key with question marks like so “???login.label.username???”

孤街浪徒 提交于 2020-01-03 17:29:33

问题


I am trying to internationalize my website using JSTL 1.2 based on How to internationalize a Java web application?. Here is the beginning of the JSP file:

<%@ page pageEncoding="UTF-8" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<c:set var="language" value="${not empty param.language ? param.language : not empty language ? language : pageContext.request.locale}" scope="session" />
<fmt:setLocale value="${language}" />
<fmt:setBundle basename="com.bundle.test" />
<html lang="${language}">

Here is the test.properties file:

login.label.username = Username
login.label.password = Password
login.button.submit = Sign in
login.label.direction = rlt

In the body, I include those lines:

<label for="username"><fmt:message key="login.label.username" /></label>

But when I open the JSP page in browser, it outputs like this:

???login.label.username???

I expected it to output the value "Username".

How is this caused and how can I solve it?


回答1:


A message in the format ???key??? means that the message associated with the key couldn't be found. This can mean that you typo'ed the key, or that the key is absent in the bundle file, or even that the whole bundle file is absent as resource in runtime classpath.

Given that the key looks good, and is present in the bundle file, this can only mean that the webapp couldn't find the bundle file as classpath resource.

Given that you're using Maven, you should assure that you have placed them in /src/main/resources folder. In your particular case with the bundle name of com.bundle.test, you should have a /src/main/resources/com/bundle/test.properties file exactly there.

If that still doesn't work, then well, your build may be dirty or broken. Clean and rebuild to be sure. Then extract the Maven-produced WAR file using some ZIP tool and explore its contents. It should ultimately end up in /WEB-INF/classes folder like /WEB-INF/classes/com/bundle/test.properties.




回答2:


Adding this code in my web.xml solved my problem

<context-param>
    <param-name>javax.servlet.jsp.jstl.fmt.localizationContext</param-name>
    <param-value>messages</param-value>
</context-param>


来源:https://stackoverflow.com/questions/30617950/fmtmessage-prints-key-with-question-marks-like-so-login-label-username

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!