【发布时间】:2023-03-06 23:04:01
【问题描述】:
我正在尝试使用 Java Spring Hibernate 项目将古吉拉特语存储在 postgreSQL 数据库中,但它存储的是这样的东西
મà«àª¦à«àª¨àª¾ àªàª¯-પરાàªàª¯ પાàªàª³ ઠવà«àª¯àªà«àª¤àª¿àª¨à«àª àªà«àªà«àª
而不是
મોદીના જય-પરાજય પાછળ આ વ્યક્તિનું ભેજું
在我的数据库中,编码是 UTF-8,如果我直接在 postgreSQL 中复制粘贴,它会正确存储,但从 Web 应用程序中的 html 表单中它无法正确存储。
以下是我的hibernate.hbm.cfg 文件
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- Database connection settings -->
<property name="connection.driver_class">
org.postgresql.Driver
</property>
<property name="connection.url">
jdbc:postgresql://192.168.6.51:5432/JayHind?autoReconnect=true&useUnicode=true&createDatabaseIfNotExist=true&characterEncoding=utf-8
</property>
<property name="connection.username">postgres</property>
<property name="connection.password">pshiv</property>
<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">10</property>
<!-- SQL dialect -->
<property name="dialect">
org.hibernate.dialect.PostgreSQLDialect
</property>
<!-- Enable Hibernate's automatic session context management -->
<property name="current_session_context_class">thread</property>
<!-- Disable the second-level cache -->
<property name="cache.provider_class">
org.hibernate.cache.NoCacheProvider
</property>
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>
<!-- Drop and re-create the database schema on startup -->
<property name="hbm2ddl.auto">update</property>
<property name="hibernate.connection.CharSet">utf8</property>
<property name="hibernate.connection.characterEncoding">utf8</property>
<property name="hibernate.connection.useUnicode">true</property>
<mapping class="com.models.Role" />
<mapping class="com.models.UserAttempts" />
<mapping class="com.models.UserLogin" />
<mapping class="com.models.UserRole" />
<mapping class="com.models.Program" />
<mapping class="com.models.NationalProgram" />
<mapping class="com.models.StateProgram" />
<mapping class="com.models.OtherProgram" />
<mapping class="com.models.Video" />
<mapping class="com.models.Contact" />
<mapping class="com.models.Heading" />
</session-factory>
</hibernate-configuration>
我也用过
%@page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" % >
在 jsp 页面中,这会使古吉拉特语正确显示,但在表单提交中仍然存在问题。
在模型类中
package com.models;
import static javax.persistence.GenerationType.IDENTITY;
import java.io.UnsupportedEncodingException;
import java.util.Date;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.UniqueConstraint;
import javax.validation.Valid;
import javax.validation.constraints.Pattern;
import org.hibernate.validator.constraints.Email;
import org.hibernate.validator.constraints.NotBlank;
import org.springframework.format.annotation.DateTimeFormat;
import com.sun.istack.internal.NotNull;
@Entity
@Table(name = "tbl_heading_info", uniqueConstraints = {
@UniqueConstraint(columnNames = "id")})
public class Heading {
private int id;
@NotNull
@NotBlank
private String message;
@Id
@GeneratedValue(strategy = IDENTITY)
@Column(name = "id", unique = true, nullable = false)
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
@Column(name = "message", nullable = false)
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
/*byte ptext[] = null;
try {
ptext = message.getBytes("ISO_8859_1");
} catch (UnsupportedEncodingException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
String value = new String(ptext, "UTF-8");
this.message=value;
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} */
}
}
我也在 web.xml 中添加了过滤器
<filter>
<filter-name>SetCharacterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>SetCharacterEncodingFilter</filter-name>
<url-pattern>*</url-pattern>
</filter-mapping>
预期输出
电流输出
【问题讨论】:
-
检查编码,也许?...
-
在我的数据库编码是 UTF-8 中,如果我直接在 postgreSQL 中复制粘贴,它会正确存储,但从 web 应用程序中的 html 表单中它无法正确存储。
-
在文本框中我输入古吉拉特语,但它存储垃圾。
-
在休眠中我也使用了编码但没有运气
-
在您描述的堆栈中有编码和翻译层(浏览器、服务器、您的应用程序、hibernate、postgresql)。你必须在他们每个人中做到正确。除非您尝试隔离问题,否则您可能会被蒙住眼睛。而且没有什么好办法可以帮助你。
标签: java hibernate spring-mvc