今天我做注册登录的时候碰到了非常奇怪的编码问题,请各位高手指教一下:
我建的项目的编码是UTF-8,所有的页面也是UTF-8的编码,框架我只用了struts2,在struts.xml文件我也加了这样一句:
<constant name="struts.il8n.encoding" value="UTF-8"/>
然后我的数据库的编码也都改成UTF-8了
可是在测试的时候就出问题了:
我重启tomcat后地磁登录的时候编码是对的,数据库里存进去的数据也对了
可第二次注册就出问题了,数据库存进去的全是乱码,
然后我在action里面打印出来也是乱码
一下是我的注册的actionpackage user.web;import user.entity.User;
import user.dao.AddUserDao;
import com.opensymphony.xwork2.Action;
public class RegokAction implements Action{ private User user;
private String message1,message; public String execute() throws Exception{
int imsg=0;
System.out.println("name = "+user.getName());
String name = new String(user.getName().getBytes("iso-8859-1"), "utf-8");
System.out.println("name += "+name);
String username = new String(user.getUsername().getBytes(),"utf-8");
String department = new String(user.getDepartment().getBytes(),"utf-8");
User user1=new User();
user1.setName(name);
user1.setPassword(user.getPassword());
user1.setUsername(username);
user1.setDepartment(department);
System.out.println(user.getName());
System.out.println(name);
AddUserDao addUser=new AddUserDao();
addUser.Addone(user);
imsg=addUser.getImsg();
if(imsg==1){
message="注册成功,请登录";
return "SUCCESS";
}
else if(imsg==2){
message1="注册失败!该用户名已经存在,请使用其他用户名注册";
return "ERROR";
}
else{
message1="未知错误,请重新注册";
return "ERROR";
}
}
public String getMessage1(){
return message1;
}
public String getMessage(){
return message;
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user= user;
}}

解决方案 »

  1.   

    数据库以byte的存中文的? 这样 存取的时候 编码转换要注意了user.getName().getBytes("iso-8859-1")⊙﹏⊙b汗
      

  2.   


    package user.web;import user.entity.User;
    import user.dao.AddUserDao;
    import com.opensymphony.xwork2.Action;
    public class RegokAction implements Action{ private User user;
    private String message1,message; public String execute() throws Exception{
    int imsg=0;
    System.out.println("name = "+user.getName());
    AddUserDao addUser=new AddUserDao();
    addUser.Addone(user);
    imsg=addUser.getImsg();
    if(imsg==1){
    message="注册成功,请登录";
    return "SUCCESS";
    }
    else if(imsg==2){
    message1="注册失败!该用户名已经存在,请使用其他用户名注册";
    return "ERROR";
    }
    else{
    message1="未知错误,请重新注册";
    return "ERROR";
    }
    }
    public String getMessage1(){
    return message1;
    }
    public String getMessage(){
    return message;
    }
    public User getUser() {
    return user;
    }
    public void setUser(User user) {
    this.user= user;
    }}以上是没有加任何调试代码的action
      

  3.   

    既然你编码都是一致的为什么还要user.getName().getBytes("iso-8859-1"),直接用user不行吗?
      

  4.   

    web.xml中看是否有CharacterEncodingFilter的设置
    我想应该都有一个这样的设置,看下编码是什么
      

  5.   

    可不可以 看下 你的user是怎么在那里转成User的
      

  6.   

    <constant name="struts.il8n.encoding" value="UTF-8"/>就是等同操作了~
      

  7.   


    package user.entity;import java.io.Serializable;public class User implements Serializable{ /**
     * 
     */
    private static final long serialVersionUID = 4062665848394842235L;
    private String name;
    private String username;
    private String password;
    private String department; public User(){

    }
    public String getName(){
    return this.name;
    }
    public void setName(String name){
    this.name=name;
    }
    public String getUsername(){
    return this.username;
    }
    public void setUsername(String username){
    this.username=username;
    }
    public String getPassword(){
    return this.password;
    }
    public void setPassword(String password){
    this.password=password;
    }
    public String getDepartment(){
    return this.department;
    }
    public void setDepartment(String department){
    this.department=department;
    }
    }以上是User.java
      

  8.   


    这是web.xml<?xml version="1.0" encoding="UTF-8"?>
    <web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">    <display-name>Struts Blank</display-name>    <filter>
            <filter-name>struts2</filter-name>
            <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
        </filter>
        
        <filter-mapping>
            <filter-name>struts2</filter-name>
            <url-pattern>/*</url-pattern>
        </filter-mapping>    <welcome-file-list>
            <welcome-file>index.jsp</welcome-file>
        </welcome-file-list></web-app>
      

  9.   

    <constant name="struts.il8n.encoding" value="UTF-8"/> 改成GBK试试 
    struts2 出乱码大概 就jsp页面 和数据库问题  好像你 数据库是保证了还有个 就是 那个配置了 
      

  10.   

    User那里 你也没做 什么validate 所以 这个 有点 难搞啊
      

  11.   

    <constant name="struts.il8n.encoding" value="UTF-8"/>改成GBK也没有用
    action的System.out.println("name = "+user.getName());
    输出的是name = ???è??
      

  12.   

    注册的jsp页面:
    <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
    <%@ taglib prefix="s" uri="/struts-tags" %>
    <%
    String path = request.getContextPath();
    String basePath = path+"/";
    %><html>
      <head>
        
        <title>用户注册</title>
    <script type="text/javascript">
    function checkRegister(){
    if(document.getElementById("name").value==''){
    alert("用户名不能为空!");
    document.getElementById("name").focus();
    return false;
    }
    if(document.getElementById("password").value==''){
    alert("用户密码不能为空!");
    document.getElementById("password").focus();
    return false;
    }
    if(document.getElementById("repassword").value!=document.getElementById("password").value){
    alert("两次输入的密码不一致!");
    document.getElementById("repassword").focus();
    return false;
    }
    if(document.getElementById("department").value==''){
    alert("所属部门不能为空!");
    document.getElementById("department").focus();
    return false;
    }
    return true;
    }
    </script>
      </head>
      
      <body>
        <h2>用户注册</h2>
        <form action="Regok.action" method="post" id="userRegister">
         <table align="center" border="0">
         <tr>
         <td align="right">用户名:</td>
         <td><input type="text" id="name" name="user.name"/></td>
         </tr>
         <tr>
         <td align="right">密码:</td>
         <td><input type="password" id="password" name="user.password"/></td>
         </tr>
         <tr>
         <td align="right">重复密码:</td>
         <td><input type="password" id="repassword" name="repassword"/></td>
         </tr>
         <tr>
         <td align="right">昵称:</td>
         <td><input type="text" id="username" name="user.username"/></td>
         </tr>
         <tr>
         <td align="right">所属部门:</td>
         <td><input type="text" id="department" name="user.department"/></td>
         </tr>
         <tr>
         <td align="center"><input type="submit" name="submit" value="注册" onclick="return checkRegister();"/></td>
         <td><input type="reset" name="reset" value="重置"/><br/>
         </td>
         </tr>
         <tr>
         <td colspan="2"><font color="red"><s:property value="message1" /></font></td>
         </tr>
         </table>
        </form>
      </body>
    </html>
      

  13.   

    <constant name="struts.il8n.encoding" value="UTF-8"/> 改成GBK也没用
    action的System.out.println("name = "+user.getName());
    输出的是name = ???è??
      

  14.   

    看代码似乎没有问题,你访问以下注册页面,查看源文件,另存为,看下默认的编码,照理来说应该是utf-8
      

  15.   

    注册页面源文件:<html>
      <head>
        
        <title>用户注册</title>
    <script type="text/javascript">
    function checkRegister(){
    if(document.getElementById("name").value==''){
    alert("用户名不能为空!");
    document.getElementById("name").focus();
    return false;
    }
    if(document.getElementById("password").value==''){
    alert("用户密码不能为空!");
    document.getElementById("password").focus();
    return false;
    }
    if(document.getElementById("repassword").value!=document.getElementById("password").value){
    alert("两次输入的密码不一致!");
    document.getElementById("repassword").focus();
    return false;
    }
    if(document.getElementById("department").value==''){
    alert("所属部门不能为空!");
    document.getElementById("department").focus();
    return false;
    }
    return true;
    }
    </script>
      </head>
      
      <body>
        <h2>用户注册</h2>
        <form action="Regok.action" method="post" id="userRegister">
         <table align="center" border="0">
         <tr>
         <td align="right">用户名:</td>
         <td><input type="text" id="name" name="user.name"/></td>
         </tr>
         <tr>
         <td align="right">密码:</td>
         <td><input type="password" id="password" name="user.password"/></td>
         </tr>
         <tr>
         <td align="right">重复密码:</td>
         <td><input type="password" id="repassword" name="repassword"/></td>
         </tr>
         <tr>
         <td align="right">昵称:</td>
         <td><input type="text" id="username" name="user.username"/></td>
         </tr>
         <tr>
         <td align="right">所属部门:</td>
         <td><input type="text" id="department" name="user.department"/></td>
         </tr>
         <tr>
         <td align="center"><input type="submit" name="submit" value="注册" onclick="return checkRegister();"/></td>
         <td><input type="reset" name="reset" value="重置"/><br/>
         </td>
         </tr>
         <tr>
         <td colspan="2"><font color="red"></font></td>
         </tr>
         </table>
        </form>
      </body>
    </html>
      

  16.   

    源文件是什么编码?应该也是utf-8吧?
    你先加上<meta http-equiv="content-type
    虽然感觉不是这个问题。
      

  17.   

    我加了:
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    还是没有用
      

  18.   

    <%@ page language="java" contentType="text/html; charset=utf-8"
        pageEncoding="utf-8"%>怪乱的
      

  19.   

    凭直觉,感觉是struts的过滤器编码问题,应该是设置,但我不懂struts2这点我就无能为力了。
      

  20.   

    改成这样也没用啊
    <%@ page language="java" contentType="text/html; charset=utf-8"%>
      

  21.   

    得  用servlet写个过滤器把  过滤中文的 原来struts1的时候 我都是 这么处理的,struts2 还没遇到编码问题,估计 我数据库 都用oracle的  没啥编码需要
      

  22.   

    终于找到问题了
    是页面的跳转问题
    我从另一个页面直接跳到注册的jsp页面,没有走struts了
    这样就导致了跳转过来之后的注册页面的编码不对