在進行struts開發的過程中,總也是出現很多的亂碼問題 ,但歸根到底,也只是以下三種情況:
1.頁面顯示中文亂碼
2.傳遞參數中文亂碼
3.國際化資源文件亂碼
下面就這三中情況介紹怎麼在具體項目中處理這些亂碼問題。而對於整體的處理思想,是要統一編碼爲: UTF-8
1.頁面顯示中文亂碼
對於在頁面中顯示出現亂碼,這個問題比較簡單,便是檢查你的JSP文件裏是不是出現了中文要處理,因爲JSP默認的編碼格式爲“ISO-8859-1”,當JSP中出現要處理的中文時,其顯示就出現亂碼了,這種情況一般出現在手寫JSP,或修改時。因爲在myeclipse6.0中,如果出現了編碼錯誤時,程序不會讓你保存,而是會提示你注意編碼,這點很好。具體的修改辦法是把
改成:
2傳遞參數中文亂碼
傳遞參數出現的亂碼,參數的內容爲中文。比如在struts應用中,簡單的一個登錄界面中,需要傳遞的登錄名爲中文時,你沒經處理之前,是會出現亂碼傳遞的,爲了讓我們能看到顯示的亂碼,我們在對應的Action類的子類裏,修改一下,用System.out把接受到的參數輸出,代碼如下:
package com.finance.actions;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionError;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import com.finance.bussiness.UserInfoBussiness;
import com.finance.model.UserInfo;
public class RegistAction extends Action {
private UserInfoBussiness userInfoBussiness = null;
public UserInfoBussiness getUserInfoBussiness() {
return userInfoBussiness;
}
public void setUserInfoBussiness(UserInfoBussiness userInfoBussiness) {
this.userInfoBussiness = userInfoBussiness;
}
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
ActionErrors actionErrors = new ActionErrors();
this.userInfoBussiness = new UserInfoBussiness();
RegistActionForm registActionForm = (RegistActionForm)form;
String userid = registActionForm.getUserid();
String username = registActionForm.getUsername();
String password = registActionForm.getPassword();
String ageStr = registActionForm.getAge();
String sexStr = registActionForm.getSex();
String tel = registActionForm.getTel();
String email = registActionForm.getEmail();
UserInfo userInfo = new UserInfo();
userInfo.setAge(Integer.parseInt(ageStr));
userInfo.setEmail(email);
userInfo.setId(userid);
userInfo.setName(username);
userInfo.setPassword(password);
userInfo.setRegDate(new SimpleDateFormat("yyyy-MM-dd").format(new Date()));
userInfo.setRight("common");
if(sexStr.equals("1")){
userInfo.setSex(true);
}else{
userInfo.setSex(false);
}
userInfo.setTel(tel);
if(userInfoBussiness.addUserInfo(userInfo))
actionErrors.add("success", new ActionError("success.common.regist"));
else
actionErrors.add("failed", new ActionError("failed.common.regist"));
this.saveErrors(request, actionErrors);
return mapping.getInputForward();
}
}
那麼當你提交了中文輸入後就會出現亂碼了。
具體的解決方法:
1.修改Tomcat---->conf----->server.xml文件,在修改端口的標籤後面加一行代碼,如下:
2.編寫過濾器Filter
利用過濾器,把requst傳遞的中文參數都設成“UTF-8”編碼。
3.修改web.xml文件
添加如下內容
<filter>
<filter-name>Set Character Encoding</filter-name>
<filter-class>com.wiley.SetCharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>utf-8</param-value>
</init-param>
<init-param>
<param-name>ignore</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>Set Character Encoding</filter-name>
<servlet-name>action</servlet-name>
</filter-mapping>
爲了使設置生效,重起tomcat。
3.國際化資源文件亂碼
利用JDK的native2ascii工具進行編碼轉換
MessageResources.properties(ISO-8859-1編碼格式)的內容如下:
#errors
error.login.nouserid=*賬號不能爲空!
error.regist.nousername=*用戶名不能爲空!
error.login.nopassword=*密碼不能爲空!
error.login.noright=*請選擇權限!
error.regist.nopasswordag=*確認密碼不能爲空!
error.regist.notmatch=*兩次密碼不匹配!
error.common.age.notnumber=*年齡必須是一個整數!
#success
success.common.regist=註冊成功!
#failed
failed.common.regist=賬號已經存在!
但保存,myeclipse會報錯,需要修改資源文件的編碼格式。Windons---->Preferences---->Content Type------>Text----->JavaPropertiesFile,把其Default encoding改爲“utf-8”,按“update”更新。但是,當進行驗證的時候,則仍然是亂碼。
這時,需要打開DOS窗口,CMD到資源文件所在目錄,運用JDK的native2ascii工具把我們新建的資源文件改成另一個名字的資源文件,例如MessageResource_zh.properties。命令如下:
>native2ascii -encoding utf-8 MessageResources.properties MessageResources_zh.properties
打開MessageResources_zh.properties 資源文件,自動生成的代碼如下:
error.login.nouserid=*/u8d26/u53f7/u4e0d/u80fd/u4e3a/u7a7a!
error.regist.nousername=*/u7528/u6237/u540d/u4e0d/u80fd/u4e3a/u7a7a!
error.login.nopassword=*/u5bc6/u7801/u4e0d/u80fd/u4e3a/u7a7a!
error.login.noright=*/u8bf7/u9009/u62e9/u6743/u9650!
error.regist.nopasswordag=*/u786e/u8ba4/u5bc6/u7801/u4e0d/u80fd/u4e3a/u7a7a!
error.regist.notmatch=*/u4e24/u6b21/u5bc6/u7801/u4e0d/u5339/u914d!
error.common.age.notnumber=*/u5e74/u9f84/u5fc5/u987b/u662f/u4e00/u4e2a/u6574/u6570!
success.common.regist=/u6ce8/u518c/u6210/u529f!
failed.common.regist=/u8d26/u53f7/u5df2/u7ecf/u5b58/u5728!
然後再到MyEclipse的項目視圖中,將項目刷新一下,就可以看到MessageResources_zh.properties資源文件了,爲了保險起見,重啓tomcat服務器。