【问题标题】:How To insert Gujarati in TextBox via Spring Hibernate in database?如何通过数据库中的 Spring Hibernate 在 TextBox 中插入古吉拉特语?
【发布时间】: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&amp;useUnicode=true&amp;createDatabaseIfNotExist=true&amp;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


【解决方案1】:

上周我不得不面对这个,试试这个。 使用 'definitivo' 具有 textBox.getText() 并转换为 UTF-8

byte ptext[] = definitivo.getBytes("ISO_8859_1");

字符串值 = new String(ptext, "UTF-8");

【讨论】:

  • 我正在使用spring hibernate。
  • 好的,所以在表单所在的操作中,您必须插入此代码。
  • 不,这对我不起作用我把它放在我的模型 pojo 类设置器中但不起作用,它正在将字符串转换为'?????? ????'
  • 尝试在 setter 中调试 de 代码,看看得到了什么以及转换成什么
  • 写完System.out.println("controller "+heading.getMessage());后,它在控制台中给了我这个 - 控制器રાહà«?લ-àª?àª?િલà«?શનà«? સàª?યà«?àª?à«?ત PC, માયા પર નરમ, àªàª¾àª?પ પર àª?રમ
猜你喜欢
  • 2022-10-15
  • 2012-07-24
  • 2017-02-16
  • 1970-01-01
  • 1970-01-01
  • 2013-08-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多