jsp代码<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<jsp:useBean id="user" class="com.yinzelin.refactor.Person"
scope="session"></jsp:useBean>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Time Management Online</title>
<link rel="stylesheet" href="css/style.css" type="text/css"
media="screen" /> <script src="js/jquery-1.4.4.min.js"></script>
<script>
// When the document loads do everything inside here ...
$(document).ready(function() { // When a link is clicked
$("a.tab").click(function() { // switch all tabs off
$(".active").removeClass("active"); // switch this tab on
$(this).addClass("active"); // slide all content up
$(".content").slideUp(); // slide this content up
var content_show = $(this).attr("title");
$("#" + content_show).slideDown(); }); });
</script>
</head>
<%
request.setCharacterEncoding("GBK");
%>
<body> <div id="tabbed_box_1" class="tabbed_box">
<h4>
Time Management Online
<small>${user.name}'s</small>
</h4>
<div class="tabbed_area">
<ul class="tabs">
<li>
<a href="#" title="content_1" class="tab active">今天</a>
</li>
<li>
<a href="#" title="content_2" class="tab">明天</a>
</li>
<li>
<a href="#" title="content_3" class="tab">计划</a>
</li>
<li>
<a href="#" title="content_4" class="tab">将来</a>
</li>
</ul> <div id="content_1" class="content">
<ul>
<c:forEach items="${user.add}" var="item">
<li>
<a href=""> ${item.content} <small>${item.occurTime}</small>
<small>${item.finishedTime}</small> </a>
</li>
</c:forEach>
<c:forEach items="${user.fromDataBase}" var="item">
<li>
<a href=""> ${item.content} <small>${item.occurTime}</small>
<small>${item.finishedTime}</small> </a>
</li>
</c:forEach>
<li>
<a href="">Web 交流会<small>4 Posts</small> </a>
</li>
<li>
<a href="">Web 交流会<small>32 Posts</small> </a>
</li>
<li>
<a href="">Web 交流会 <small>2 Posts</small> </a>
</li>
<li>
<a href="">Web 交流会 <small>19 Posts</small> </a>
</li>
<li>
<a href="">Web 交流会 <small>6 Posts</small> </a>
</li>
<li>
<a href="add.do"><form action="add.do" method="post">
<label>
事情
</label>
<input type="text" name="thing" size="40" maxlength="40" />
<label>
类别
</label>
<select name="type">
<option value="1" selected="selected">
快乐
</option>
<option value="2">
救火
</option>
<option value="3">
假忙
</option>
<option value="4">
游戏
</option>
</select>
<input type="submit" value="添加" />
</form> <small>8 Posts</small> </a>
</li>
</ul>
</div>
<div id="content_2" class="content">
<ul>
<li>
<a href="">Web 交流会 <small>6 Posts</small> </a>
</li>
<li>
<a href="">Web 交流会 <small>4 Posts</small> </a>
</li>
<li>
<a href="">Web 交流会 <small>22 Posts</small> </a>
</li>
<li>
<a href="">Web 交流会 <small>12 Posts</small> </a>
</li>
<li>
<a href="">Web 交流会 <small>3 Posts</small> </a>
</li>
<li>
<a href="">Web 交流会 <small>1 Posts</small> </a>
</li>
</ul>
</div>
<div id="content_3" class="content">
<ul>
<li>
<a href="">Web 交流会</a>
</li>
<li>
<a href="">Web 交流会</a>
</li>
<li>
<a href="">Web 交流会</a>
</li>
<li>
<a href="">Web 交流会</a>
</li>
</ul>
</div>
<div id="content_4" class="content">
<ul>
<li>
<a href="">Web 交流会</a>
</li>
<li>
<a href="">Web 交流会</a>
</li>
<li>
<a href="">Web 交流会</a>
</li>
<li>
<a href="">Web 交流会</a>
</li>
</ul>
</div> </div> </div>
</body>
</html>action代码/*
 * Generated by MyEclipse Struts
 * Template path: templates/java/JavaClass.vtl
 */
package com.yinzelin.struts.action;import java.io.UnsupportedEncodingException;
import java.util.Date;import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;import com.yinzelin.refactor.Person;
import com.yinzelin.refactor.Thing;/**
 * MyEclipse Struts Creation date: 12-06-2010
 * 
 * XDoclet definition:
 * 
 * @struts.action validate="true"
 * @struts.action-forward name="success" path="/test1.jsp"
 */
public class AddAction extends DispatchAction {
/*
 * Generated Methods
 */ /**
 * Method execute
 * 
 * @param mapping
 * @param form
 * @param request
 * @param response
 * @return ActionForward
 */
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
// TODO Auto-generated method stub
//会出现乱码
// try {
// request.setCharacterEncoding("GBK");
// } catch (UnsupportedEncodingException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
// 获取当前的session周期的Person
Person user = (Person) request.getSession().getAttribute("user");
// System.out.println(user.getName()); // 获取当前时间
Date date = new Date();
int year = date.getYear();
int month = date.getMonth();
int day = date.getDate();
int hour = date.getHours();
int minute = date.getMinutes();
int second = date.getSeconds();
String stringOfDate = (1900+year) + "-" + (month + 1) + "-" + day + " " + hour
+ ":" + minute + ":" + second;
System.out.println(stringOfDate);


String thingcontent = null;
try {
thingcontent = new String(request.getParameter("thing").getBytes("ISO-8859-1"),"GBK");
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(thingcontent);


int type = Integer.parseInt(request.getParameter("type"));
System.out.println(type);

Thing thing = new Thing();
thing.setOccurTime(stringOfDate);
thing.setState(0);
thing.setFinishedTime("还没完成哦");
if(thingcontent !="")
thing.setContent(thingcontent);
else 
return mapping.findForward("faile");
user.stackAdd(thing);
return mapping.findForward("success");
}
}

解决方案 »

  1.   


    <%
      request.setCharacterEncoding("GBK");
    %>
    request.setCharacterEncoding("GBK");改成<%
      request.setCharacterEncoding("utf-8");
    %>
    request.setCharacterEncoding("utf-8");
      

  2.   

    里面那个form提交到action后出现乱码,设置utf-8没用
      

  3.   


        // TODO Auto-generated method stub
            //会出现乱码
    //        try {
    //            request.setCharacterEncoding("GBK");
    //        } catch (UnsupportedEncodingException e) {
    //            // TODO Auto-generated catch block
    //            e.printStackTrace();
    //        }
            // 获取当前的session周期的Person你把这个注释去了 ,里面的那个“GBK”为什么页面上又用utf-8 又用gbk action里面又用了gbk 难道不知道编码格式要统一,要么都是utf-8 要么都是gbk
      

  4.   

    你没具体说出你哪有问题了。不知道你表单提交是post还是get。。
    要是你用url的方式到action。你可以在js里把你的url Encode一下。再把参数在action里decode一下。
    form提交的话。就不要这样了,可以写个filter。统一处理所有的编码问题。也省的在jsp里和action里做处理了。
    相关的具体方法。网上搜一下。很多的
      

  5.   

    看看那个页面的属性是不是gbk 或者是utf-8 
      

  6.   

    String userName_iso = user.getName();//得到的是以iso-8859-1编码的中文(乱码)
    String userName_gbk = new String(userName_iso.getBytes("iso-8859-1"),"gbk");//得到的userName_gbk 是正确的中文
      

  7.   


    String userName_gbk = new String(userName_iso.getBytes("iso-8859-1"),"UTF-8");11楼不行的话,用我改的看看.
      

  8.   

    请这样做
    1. 在TOMCAT/CONF下的STRUTS.XML将CONNECTOR的URIENCODING设置为UTF-8
    2. 页面编码,返回数据流CONTENTENCODING设置为UTF-8
    3. 浏览器编码设置为UTF-8
    4. OS系统区域语言设置/进阶设置为中国PRC(针对台湾香港繁体中文)
    5. 请传输中文时用POST,JSP页面直接传中文没问题
    6. SERVER端处理时可以正常接收中文,但是回传数据流时(例如下载)请用new String(fileName.getBytes("UTF-8"), "ISO-8859-1");ServletActionContext.getResponse().setCharacterEncoding("UTF-8");