You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

JSP页面英文转西班牙语翻译乱码:特殊字符显示为问号

西班牙语翻译出现乱码(�)的原因及解决办法

先看你提供的JSP代码片段:

<div class="form-group">
    <label for="training" class="form-check-label mt-30">                           
        <form:checkbox id="training" path="isTraining" class="form-check-input"/><em><spring:message code="sol.settings.training.label"/></em>
    </label>
    <input type="button" class="bbtn bbtn-secondary pull-right mt-30" onclick="window.location='/sol/list/page/redirectToSolutionView.htm?filterId=MYITS&amp;showAlert=false&amp;searchText='" 
        title="Cancel Solution"                     
        value="<spring:message code='create.sol.page.cancelsol'/>"
    />
</div>

你遇到的西班牙语翻译乱码(显示Cancelar soluci�n而非预期Cancelar solución),核心原因是编码不匹配,具体分析如下:

  • 属性文件编码问题:
    Spring默认使用ISO-8859-1编码加载.properties文件,而西班牙语中的ñ属于UTF-8专属字符,直接用ISO-8859-1存储会导致字符丢失,显示为�。

  • Spring消息源配置缺失:
    若未明确配置MessageSource的编码,Spring会沿用默认的ISO-8859-1,无法正确解析UTF-8格式的属性文件内容。

  • JSP页面编码未统一:
    如果JSP页面本身未设置UTF-8编码,即便属性文件正确,输出到页面时也可能出现乱码。

解决步骤:

  1. 调整属性文件编码:
    将西班牙语属性文件(如messages_es.properties)保存为UTF-8编码(注意不要包含BOM头,部分编辑器默认会加,需手动关闭)。

  2. 配置Spring MessageSource编码:
    若使用XML配置,给ReloadableResourceBundleMessageSource添加编码配置:

    <bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
        <property name="basename" value="classpath:messages"/>
        <property name="defaultEncoding" value="UTF-8"/>
        <property name="fileEncodings" value="UTF-8"/>
    </bean>
    

    若用Spring Boot,在application.properties中添加:

    spring.messages.encoding=UTF-8
    spring.messages.basename=messages
    
  3. 统一JSP页面编码:
    在JSP页面开头添加编码声明:

    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    

    同时在HTML头部添加meta标签:

    <meta charset="UTF-8">
    

内容的提问来源于stack exchange,提问作者Ruchi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.07 13:42:32