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

OpenXava 6.6.3保存简体中文后浏览器显示问号乱码问题求助

运行环境
  • 操作系统:Windows 10
  • 框架版本:OpenXava 6.6.3
故障现象

创建产品数据时,简体中文格式的产品名称保存后,浏览器端部分字符显示为?????。
已尝试以下操作,均未解决问题:

  • 配置Hibernate使用UTF-8编码读写数据
  • 更换多款浏览器测试
  • 重启OpenXava服务

附问题截图:中文乱码截图

当前配置

hibernate.cfg.xml

<hibernate-configuration>
    <session-factory>
        <property name="hibernate.connection.datasource">java://comp/env/jdbc/InvoicingDS</property>
        <property name="hibernate.jdbc.use_get_generated_keys">false</property>     
        <property name="hibernate.show_sql">false</property>
        <property name="hibernate.default_schema">Invoicing</property>  
            
        <!-- 仅使用IMAGES_GALLERY/GALERIA_IMAGENES构造型时需要GalleryImage -->
        <mapping resource="GalleryImage.hbm.xml"/>
    </session-factory>
</hibernate-configuration>

Product实体类代码

package com.yourcompany.invoicing.model;

import java.math.*;
import javax.persistence.*;
import org.openxava.annotations.*;
import lombok.*;

@Entity
@Getter
@Setter
public class Product {
    @Id
    @Column(length = 9)
    int number;

    @Column(length = 50)
    @Required
    String description;

    @ManyToOne(
            fetch = FetchType.LAZY,
            optional = true)
    @DescriptionsList
    Category category;

    @Stereotype("MONEY")
    BigDecimal price;

    @Stereotype("IMAGES_GALLERY")
    @Column(length = 32)
    String photos;

    @Stereotype("MEMO")
    String remarks;
}

解决方法

修改Hibernate配置不生效的核心原因:当前使用容器托管的JNDI数据源,数据库连接由Servlet容器(OpenXava默认内置Tomcat)创建和维护,Hibernate仅从容器获取已有连接,不会处理连接编码参数,编码配置需要加在数据源定义中,而非Hibernate配置文件。

按以下步骤修复:

  1. 定位数据源配置文件
    找到定义jdbc/InvoicingDS数据源的配置文件,通常为以下两个路径之一:
    • 项目路径下web/META-INF/context.xml(OpenXava默认项目的配置存放位置)
    • Tomcat安装目录下conf/context.xml
  2. 给数据源JDBC连接追加UTF-8编码参数
    找到对应数据源的<Resource>标签,根据你使用的数据库类型,在连接URL末尾追加编码参数:
    • MySQL/MariaDB:追加?useUnicode=true&characterEncoding=utf8&serverTimezone=Asia/Shanghai
      配置示例:
      <Resource name="jdbc/InvoicingDS"
                auth="Container"
                type="javax.sql.DataSource"
                maxTotal="100"
                maxIdle="30"
                maxWaitMillis="10000"
                username="你的数据库账号"
                password="你的数据库密码"
                driverClassName="com.mysql.cj.jdbc.Driver"
                url="jdbc:mysql://localhost:3306/Invoicing?useUnicode=true&characterEncoding=utf8&serverTimezone=Asia/Shanghai"/>
      
    • PostgreSQL:追加?charSet=UTF8
    • SQL Server:追加;characterEncoding=UTF-8
  3. 校验数据库字符集
    确认Invoicing库、Product表的description字段字符集为UTF-8(MySQL推荐使用utf8mb4),避免字段级配置不支持中文存储。
  4. 配置JVM启动编码
    若上述操作完成后仍存在乱码,在Windows环境的启动脚本start.bat中,启动命令前追加JVM参数:
    set JAVA_OPTS=-Dfile.encoding=UTF-8 %JAVA_OPTS%
    
    重启服务后重新测试中文保存即可。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.27 18:31:28