Something wrong with encoding of .properties or JSP

不问归期 提交于 2019-12-21 04:22:18

问题


I have jsp file:

   <%@ page language="java" contentType="text/html;
charset=UTF-8"
    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="localization.text" />
<!DOCTYPE html>
<html lang="${language}">
<head>
<title>JSP/JSTL i18n demo</title>
</head>
<body>
<form accept-charset="UTF-8"><select id="language"
    name="language" onchange="submit()">
    <option value="en" ${language=='en' ? 'selected' : ''}>English</option>
    <option value="ru" ${language=='ru' ? 'selected' : ''}>Russian</option>
</select></form>
<form name="loginForm" method="POST" action="controller"><input
    type="hidden" name="command" value="login" /> <label for="login"><fmt:message
    key="login.label.login" />:</label> <input type="text" name="login" value="">
<br>
<input type="hidden" name="command" value="password" /> <label
    for="password"><fmt:message key="login.label.password" />:</label> <input
    type="password" name="password" value=""> <br>
<fmt:message key="login.button.submit" var="buttonValue" /> <input
    type="submit" name="submit" value="${buttonValue}"></form>
</body>
</html>

As you could get from written above the problem is in encoding and the problem is in Russian language. So here is my .properties file (text_ru.properties in localization folder):

login.label.login = Логин
login.label.password = Пароль
login.button.submit = Отправить

Btw,it's English file:

login.label.login = Login
login.label.password = Password
login.button.submit = Sign in

But browser gives me this thing:

I saved my .properties files in utf-8 and tried it with the help of two programms (the first is Eclipse and the second is Notepad++) and I don't really know what to do with this encoding problem.

Will be very grateful for your help.


回答1:


Unfortunately, when .properties files are read via a ResourceBundle, it is expecting ISO-8859-1 always.

The usual approach is to unicode-escape the non-ascii characters in the properties file. Then it will look something like that:

hours.label=\u0427\u0430\u0441\u043e\u0432\u0435

AnyEdit tools is an eclipse plugin that makes the escaping and unescaping during development easy.

Another, more tedious approach is to provide your own tag that uses your own ResourceBundle implementation that in turn uses .load(reader), where the reader is using UTF-8



来源:https://stackoverflow.com/questions/8423025/something-wrong-with-encoding-of-properties-or-jsp

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