gpt4 book ai didi

java - Spring MVC Tomcat 编码 UTF-8

转载 作者:行者123 更新时间:2023-11-28 22:27:10 25 4
gpt4 key购买 nike

您好,我在用西里尔字母书写时遇到了问题。我使用 Spring MVC 和 Tomcat 服务器。 Tomcat web.xml 看起来像这样:

 <filter>  
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter- class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<display-name>Welcome to Tomcat</display-name>
<description>
Welcome to Tomcat
</description></web-app>

在 JSP 中,我在页面语言和页面内容类型中添加了 utf-8,也在表单中。

<%@ page language="java" pageEncoding="UTF-8"%>
<%@ page contentType="text/html;charset=UTF-8" %>
<%@ taglib prefix="springForm"
uri="http://www.springframework.org/tags/form"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Department</title>
</head>
<body>

<p>Въвеждане на нов отдел</p>
<c:if test="${not empty success}">
<p style="color: green">${success}</p>
</c:if>

<springForm:form action="department" method="post"
modelAttribute="department" accept-charset="UTF-8">

<springForm:input type="text" placeholder="Department name"
path="name" />

<label style="color: red"> <springForm:errors path="name" /></label>
<label style="color: red"> ${errorName}</label>

<br>
<br>
<button>Създай</button>
</springForm:form>
</body>
</html>

在DB中设置了编码utf8-default collat​​ion,我改成了utf 8 general ci,但没有效果。 Мy 字符看起来像这样的“нов опиє。在 Controller 中我添加了这段代码:

department.setName(new String (department.getName().getBytes ("iso-8859-1"), "UTF-8"));

唯一的变化是它开始看起来像“??????????????”。

这是我的数据库连接代码。我是否必须在某处添加 UTF-8 以及更准确的位置。

private static final String DB_PASSWORD = "";
private static final String DB_USER = "root";
private static final String DATABASE = "bms_requests";
private static final String DB_PORT = "3306";
private static final String DB_HOST = "127.0.0.1";
private static final String DB_URL = DB_HOST + ":" + DB_PORT;

private static DBConnection connectionInstance = null;
private static Connection connection = null;

private DBConnection() {
try {
Class.forName("com.mysql.jdbc.Driver");
connection = DriverManager.getConnection("jdbc:mysql://" + DB_URL + "/" + DATABASE, DB_USER, DB_PASSWORD);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
}

public Connection getConnection() {
return connection;
}

public static DBConnection getInstance() {
synchronized (DBConnection.class) {
if (connectionInstance == null) {
connectionInstance = new DBConnection();
}
}

return connectionInstance;
}

哪里不对,什么需要改。我看了很多,也改了很多,但还是找不到解决问题的办法。如果有人能帮助我,我会很高兴。

最佳答案

尝试在JDBC连接串中加入UTF-8编码。

例如:

connection = DriverManager.getConnection("jdbc:mysql://" + DB_URL + "?useUnicode=true&characterEncoding=utf-8" + "/" + DATABASE, DB_USER, DB_PASSWORD);

关于java - Spring MVC Tomcat 编码 UTF-8,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39062911/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com