easyui-datagrid出现乱码问题

本文介绍了一种在使用EasyUI Datagrid组件时遇到的乱码问题及其解决方案。通过对响应编码设置为UTF-8的方式成功解决了乱码问题。

本人今天在学习使用easyui-datagrid组件的来展示数据库数据的时候意外出现了乱码的问题如下图:

但是经过调试后发现得到的json格式内容中并没有出现乱码现象,如下图:


由上面的图片可见问题出现在页面返回显示上,于是灵机一动:应该是response那里出了问题,于是把代码修改为下图:


在添加了response.setCharacterEncoding("UTF-8")后乱码问题得以解决,如下图:




<%@ page language="java" contentType="text/html; charset=UTF-8" import="dao.*,model.*,java.util.*" pageEncoding="UTF-8"%> <!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>员工基本信息</title> <!-- 引入本地 EasyUI 的 CSS 和 JS(假设项目根目录下有 easyui 文件夹) --> <link rel="stylesheet" type="text/css" href="../easyui/themes/default/easyui.css"> <link rel="stylesheet" type="text/css" href="../easyui/themes/icon.css"> <script type="text/javascript" src="../easyui/jquery.min.js"></script> <script type="text/javascript" src="../easyui/jquery.easyui.min.js"></script> </head> <body> <% Userdao mydao = new Userdao(); List<Users> userlist = mydao.QueryUserInfoAll(); %> <h1 align="center">员工基本信息</h1> <!-- 使用 EasyUIdatagrid 组件 --> <!-- 外层添加一个 div 容器,并设置居中样式 --> <div style="width: 100%; text-align: center;"> <table class="easyui-datagrid" style="width:600px; height:auto; margin: 0 auto;" data-options="singleSelect:true, collapsible:true, url:'', method:'get'"> <thead> <tr> <th data-options="field:'user_id', width:100, align:'center'">用户名</th> <th data-options="field:'user_pwd', width:100, align:'center'">密码</th> </tr> </thead> <tbody> <% Users myUsers = new Users(); for (int i = 0; i < userlist.size(); i++) { myUsers = userlist.get(i); String user_id = myUsers.getuser_id(); String user_pwd = myUsers.getuser_pwd(); %> <tr> <td><%=user_id%></td> <td><%=user_pwd%></td> </tr> <% } %> </tbody> </table> </div> </body> </html>
最新发布
07-06
您的代码已经实现了表格居中显示,但有几个潜在问题需要优化。以下是改进后的代码及说明: ### 改进后的完整代码 ```html <%@ page language="java" contentType="text/html; charset=UTF-8" import="dao.*,model.*,java.util.*" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>员工基本信息</title> <!-- 使用绝对路径或项目上下文路径 --> <link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/easyui/themes/default/easyui.css"> <link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/easyui/themes/icon.css"> <script type="text/javascript" src="${pageContext.request.contextPath}/easyui/jquery.min.js"></script> <script type="text/javascript" src="${pageContext.request.contextPath}/easyui/jquery.easyui.min.js"></script> <style> /* 添加全局样式确保兼容性 */ body { font-family: "Microsoft YaHei", sans-serif; margin: 0; padding: 20px; } .datagrid-container { width: 80%; max-width: 800px; margin: 0 auto; padding: 10px; } </style> </head> <body> <h1 style="text-align: center;">员工基本信息</h1> <!-- 优化后的表格容器 --> <div class="datagrid-container"> <table class="easyui-datagrid" style="width:100%; height:auto;" data-options=" singleSelect:true, collapsible:true, fitColumns:true, url:'', method:'get', onLoadSuccess: function(){$(this).datagrid('autoSizeColumn');} "> <thead> <tr> <th data-options="field:'user_id', width:100, align:'center'">用户名</th> <th data-options="field:'user_pwd', width:100, align:'center'">密码</th> </tr> </thead> <tbody> <% for (Users user : userlist) { String user_id = user.getuser_id(); String user_pwd = user.getuser_pwd(); %> <tr> <td><%=user_id%></td> <td><%=user_pwd%></td> </tr> <% } %> </tbody> </table> </div> <script> // 初始化后自动调整列宽 $(function(){ $('.easyui-datagrid').datagrid(); }); </script> </body> </html> ``` ### 主要优化点: 1. **资源路径优化**: - 使用 `${pageContext.request.contextPath}` 替代相对路径 `../`,避免路径错误 - 确保 easyui 文件夹位于 Web 应用的根目录下(如 `/webapp/easyui/`) 2. **响应式布局**: - 添加了 `.datagrid-container` 类,设置 `width: 80%` 和 `max-width` - 使用 `fitColumns:true` 让列宽自动适应容器 3. **表格初始化**: - 添加了 `onLoadSuccess` 回调自动调整列宽 - 通过 JavaScript 显式初始化 datagrid 4. **代码结构优化**: - 使用增强 for 循环简化 JSP 代码 - 移除过时的 HTML4 声明,使用 HTML5 标准 - 添加了基本的 CSS 重置 5. **功能增强**: - 添加了 `autoSizeColumn` 功能,确保内容完全显示 - 改进了标题的居中显示方式 ### 部署注意事项: 1. 确保目录结构如下: ``` /YourWebApp ├── easyui/ │ ├── themes/ │ ├── jquery.min.js │ └── ... └── WEB-INF/ └── your_jsp_files/ ``` 2. 如果使用 IDE(如 Eclipse),检查: - easyui 文件夹是否在 Deployment Assembly 中 - 清理 Tomcat 缓存后重新部署 ### 常见问题解决: 1. **资源加载失败**: - 检查浏览器开发者工具中的 Network 选项卡 - 确认资源 URL 是否正确(如 `http://localhost:8080/YourApp/easyui/themes/default/easyui.css`) 2. **表格不显示**: - 确保 jQuery 在 EasyUI 之前加载 - 检查是否有 JavaScript 错误 3. **中文乱码**: - 确认所有文件保存为 UTF-8 编码 - 检查服务器响应头中的字符集设置
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值