tree.jsp页面:
function myreset(){
var node = zTree.getSelectedNodes()[0];
$.ajax({
type: "GET",
url:"upd.jsp",
data: "treeId=" +node.id+"&nodeName="+node.name,
dataType: "text",
success: function(data){
hideRMenu();
}
});
}
upd.jsp 页面:
<%@ page contentType="text/html; charset=UTF-8" %>
<%@ page language="java" import="java.util.*, java.sql.*" pageEncoding="utf-8"%>
<%
//连接MySQL数据库
Connection conn = com.zy.test.pojo.Node.getConn();
Statement st = conn.createStatement();
String idno=request.getParameter("treeId");
//中文乱码解决
String name = new String(request.getParameter("nodeName").getBytes("ISO-8859-1"),"UTF-8");
//修改节点名字
String sql="update menu set name=? where id=?";
ResultSet rs = null;
PreparedStatement pstmt = null;
try{
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, name);
pstmt.setString(2, idno);
pstmt.executeUpdate();
conn.commit();
}catch (SQLException e) {
e.printStackTrace();
}
com.zy.test.pojo.Node.closeAll(rs, pstmt, conn);
%>
结果在servlet页面中打印是问号,网上查这边是乱码。
解决方法如下:
function myreset(){
var node = zTree.getSelectedNodes()[0];
<span style="color:#ff0000;"><strong>var name_ = node.name;</strong></span>
$.ajax({
type: "POST",
url:"upd.jsp",
data: "treeId=" +node.id+"&nodeName="+name_,
contentType: "application/x-www-form-urlencoded; charset=utf-8",
dataType: "text",
success: function(data){
hideRMenu();
}
});
}
<%@ page contentType="text/html; charset=UTF-8" %>
<%@ page language="java" import="java.util.*, java.sql.*" pageEncoding="utf-8"%>
<%
//连接MySQL数据库
Connection conn = com.zy.test.pojo.Node.getConn();
Statement st = conn.createStatement();
String idno=request.getParameter("treeId");
request.setCharacterEncoding("UTF-8");
//中文乱码解决
//String name = new String(request.getParameter("nodeName").getBytes("ISO-8859-1"),"UTF-8");
String name = request.getParameter("nodeName");
//修改节点名字
String sql="update menu set name=? where id=?";
ResultSet rs = null;
PreparedStatement pstmt = null;
try{
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, name);
pstmt.setString(2, idno);
pstmt.executeUpdate();
conn.commit();
}catch (SQLException e) {
e.printStackTrace();
}
com.zy.test.pojo.Node.closeAll(rs, pstmt, conn);
%>
该了三点:
1、get改为post
2、增加contentType: "application/x-www-form-urlencoded; charset=utf-8", 代码段
3、servlet中去掉String name = new String(request.getParameter("nodeName").getBytes("ISO-8859-1"),"UTF-8")改为request.setCharacterEncoding("UTF-8");
4、容易忽略的一点,js中直接把name获取出来,然后在赋值,而不是直接取数,标红的部分
问题解决。