我查看了数据库都是utf-8,myeclipse工作空间的也是utf-8,jsp也是utf-8,以及我的action也是utf-8的。可是为什么乱码?
最后我在web.xml中配置spring的过滤器,还是不行。最后把Tomact编码都改成utf-8了。可是存入数据库还是乱码
我在action输出了 输出的参数是乱码。
实在没有办法了!请问前辈们有木有什么办法啊!
jsp:
  <%@ page language="java" import="java.util.*" pageEncoding="UTF-8" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">    
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
  </head>
  
  <body>
    <form action="foodAction!insert.action" method="get">
     <table>
        <tr>
           <td>食品的名称</td>
           <td><input type="text" name="food.name" value="" /></td>
        </tr>
       <tr>
           <td>食品的介绍</td>
           <td><input type="text" name="food.content" value="" /></td>
        </tr>
        <tr>
           <td>食品的价格</td>
           <td><input type="text" name="food.price" value="" /></td>
        </tr>
     </table>
     <input type="submit" value="提交">
    </form>
  </body>
</html>web.xml:
      <?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
xmlns="http://java.sun.com/xml/ns/javaee" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

<!--定义Spring的配置的位置,可以定义多个配置文件,可以使用通配符。  -->
<context-param>
   <param-name>contextConLocationfig</param-name>
   <param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>

<!-- 监听spring的配置文件 启动项目加载spring配置文件 -->
<listener>
    <listener-class>
     org.springframework.web.context.ContextLoaderListener
    </listener-class>
</listener>

<!-- Spring字符编码过滤器 -->
<filter>
<filter-name>CharsetFilter</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>
</filter>
<filter-mapping>
<filter-name>CharsetFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

<!-- struts2中央控制器又叫过滤器 -->
<filter>
   <filter-name>struts2</filter-name>
   <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
    <filter-name>struts2</filter-name>
    <url-pattern>*.action</url-pattern>
</filter-mapping>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>

解决方案 »

  1.   

    action部分:
       package org.zwck.compass.action;
    import org.zwck.compass.po.Food;
    import org.zwck.compass.service.FoodService;public class FoodAction  {
    private FoodService foodService;
    private Food food;public FoodService getFoodService() {
    return foodService;
    }public void setFoodService(FoodService foodService) {
    this.foodService = foodService;
    }
    public Food getFood() {
    return food;
    }public void setFood(Food food) {
    this.food = food;
    }public String insert(){
    System.out.println(food.getName());//输出来是乱码的
    foodService.save(food);
    return "ok";
    }}Tomact部分:
        <Connector executor="tomcatThreadPool"
                   port="8080" protocol="HTTP/1.1" 
                   connectionTimeout="20000" 
                   URIEncoding="UTF-8" 
                   redirectPort="8443" />
    mysql数据库查看到编码:
          character_set_client  utf-8
         character_set_connection  utf-8
         character_set_database   utf-8
         character_set_results    utf-8
         character_set_server     gbk
         character_set_system     utf-8
         
         
           
      

  2.   

    试着判断food的乱码原先是什么编码,然后去查找哪设置了这个编码
      

  3.   

    你可以在后台打印输出看下后台在页面上得到的是乱码。
    如果是的话可以这样试看看:
    在其set方法中
    public void setFood(Food food) {
    this.food.name = new String(food.name,'utf-8');
    ......
    }
      

  4.   

    你用了get提交方式。该方式是表单默认的提交方式,其默认编码是ISO-8859-1。因为get方法提交表单,提交的内容在URL中,一开始就已经按照编码分析所有的提交内容,所以,设置的编码也就无效了,你需要转码。例如:String title = request.getParameter("title");
    String titleTimp = new String(title.getBytes("ISO-8859-1"),("UTF-8"));注意:在JSP页面中,再加上这句,试试看。<%@ page contentType="text/html;chartset=UTF-8" %>
      

  5.   

    整合和乱码没有关系。
    my blog
      

  6.   

    估计你是使用了MySQL数据库,你改用Oracle吧,那个几乎不用转。MySQL在连接数据库时候定义字符类型就可解决乱码问题了。
      

  7.   

    对于ssh乱码问题,最后的办法就是写个过滤器