Struts国际化乱码解决方法- -

 

Struts+Mysql+Tomcat5.0.28+mysql-connector-java-3.0.16-ga-bin.jar 国际化乱码解决方法       国际化的东西带来的问题还真的好多,各国语言不同,所使用的字符集都不一样,JAVA,Mysql,Tomcat,浏览器等等用的字符集也不一样,这几天气得我都说了好几次不用什么Struts,Mysql,Tomcat了,全部都是自己写出来好了,用统一的编码统一的字符集,可惜能力不够,说说而已,问题还是得解决。在网上查了好久,自己也实践了好多天,问题终于算是解决了。
        因为要考虑到很多国的语言,一开始就把项目立足于国际化,在把连接方式从Struts连接池改为Tomcat的连接池之后,原来我遇到的乱码有
    1、资源文件里读出来在页面上的乱码;
    2、数据库读出来的乱码
    3、数据库写进去的乱码
    4、在ActionForm验证不通过Errors返回的乱码,也就是request,IE参数传递的乱码了。
    5、有一个JS写的时间选择跳出框,如果在charset=UTF-8的时候就出错,但如果我改为charset=GBK或GB2312时就一切正常了,很奇怪的现象,我还找不出答案来。
        哇,好多的乱码。。。。头都大了好几天。
        下面是我的解决方法
    1、资源文件里读出来在页面上的乱码:这个最容易解决了,把写好的ApplicationResources.properties文件,在DOC底下用 native2ascii -encoding gb2312 ApplicationResources.properties 
ApplicationResources_zh_CN.properties 命令来个字符编码转换,将原来的中文转为Unicode编码就搞定了中文简体,繁体也用同样的命令,只是把 bg2312 改为 bgk 就可以了。
    2、数据库读写的乱码,刚开始的时候因为受以前的SQL Server+JDBC 影响(在那时写入数据库是可以不用做什么工作的,只是在读出来的时候来个 gbk = new String(iso.getBytes("ISO-8859-1"), "GBK") 转换就行了)我也都在把读写都在转换,搞得好复杂也很麻烦,后来在连接池连接代码jdbc:mysql://localhost:3306/database?autoReconnect=true&useUnicode=true&characterEncoding=GBK那里加上一个&useUnicode=true&characterEncoding=GBK,保证了在数据库操作时候使用了统一的编码字符集,又解决了两个乱码问题,一举两得,嘿嘿。
    4、request,response的乱码在网上找了好久,也有两个解决的办法,也是来个转换,还有种办法是写一个过滤器,我选择了后者,因为简单:),这方法用到两个文件,一个是 filter ,一个是 web.xml 文件,代码在后面。
    5、至于JS的这个问题,还没办法,只好在JSP页面上改为<%@ page contentType="text/html; charset=GBK"%>了,反正这样也没问题。
        到此为止,乱码问题总算告一段落了,感觉蛮不错的,郁闷了这么久,终于可以高兴了好大一段子了。

-------------------------------------------------------------------------------------------------------------------
过滤器使用代码:EncodingFilter.java

package com.***.***.***; 

import javax.servlet.*;
import java.io.IOException;

/**
 * <p>Filter that sets the character encoding to be used in parsing the
 * incoming request, either unconditionally or only if the client did not
 * specify a character encoding.  Configuration of this filter is based on
 * the following initialization parameters:</p>
 * <ul>
 * <li><strong>encoding</strong> - The character encoding to be configured
 *     for this request, either conditionally or unconditionally based on
 *     the <code>ignore</code> initialization parameter.  This parameter
 *     is required, so there is no default.</li>
 * <li><strong>ignore</strong> - If set to "true", any character encoding
 *     specified by the client is ignored, and the value returned by the
 *     <code>selectEncoding()</code> method is set.  If set to "false,
 *     <code>selectEncoding()</code> is called <strong>only</strong> if the
 *     client has not already specified an encoding.  By default, this
 *     parameter is set to "true".</li>
 * </ul>
 *
 * <p>Although this filter can be used unchanged, it is also easy to
 * subclass it and make the <code>selectEncoding()</code> method more
 * intelligent about what encoding to choose, based on characteristics of
 * the incoming request (such as the values of the <code>Accept-Language</code>
 * and <code>User-Agent</code> headers, or a value stashed in the current
 * user's session.</p>
 *
 */
public class EncodingFilter implements Filter {

    // ----------------------------------------------------- Instance Variables


    /**
     * The default character encoding to set for requests that pass through
     * this filter.
     */
    protected String encoding = null;


    /**
     * The filter configuration object we are associated with.  If this value
     * is null, this filter instance is not currently configured.
     */
    protected FilterConfig filterConfig = null;


    /**
     * Should a character encoding specified by the client be ignored?
     */
    protected boolean ignore = true;


    // --------------------------------------------------------- Public Methods


    /**
     * Take this filter out of service.
     */
    public void destroy() {

        this.encoding = null;
        this.filterConfig = null;

    }


    /**
     * Select and set (if specified) the character encoding to be used to
     * interpret request parameters for this request.
     *
     * @param request The servlet request we are processing
     * @param result The servlet response we are creating
     * @param chain The filter chain we are processing
     *
     * @exception IOException if an input/output error occurs
     * @exception ServletException if a servlet error occurs
     */
    public void doFilter(ServletRequest request, ServletResponse response,
                         FilterChain chain)
    throws IOException, ServletException {

        // Conditionally select and set the character encoding to be used
        if (ignore || (request.getCharacterEncoding() == null)) {
            String encoding = selectEncoding(request);
            if (encoding != null)
                request.setCharacterEncoding(encoding);
        }

    // Pass control on to the next filter
        chain.doFilter(request, response);

    }


    /**
     * Place this filter into service.
     *
     * @param filterConfig The filter configuration object
     */
    public void init(FilterConfig filterConfig) throws ServletException {

    this.filterConfig = filterConfig;
        this.encoding = filterConfig.getInitParameter("encoding");
        String value = filterConfig.getInitParameter("ignore");
        if (value == null)
            this.ignore = true;
        else if (value.equalsIgnoreCase("true"))
            this.ignore = true;
        else if (value.equalsIgnoreCase("yes"))
            this.ignore = true;
        else
            this.ignore = false;

    }


    // ------------------------------------------------------ Protected Methods


    /**
     * Select an appropriate character encoding to be used, based on the
     * characteristics of the current request and/or filter initialization
     * parameters.  If no character encoding should be set, return
     * <code>null</code>.
     * <p>
     * The default implementation unconditionally returns the value configured
     * by the <strong>encoding</strong> initialization parameter for this
     * filter.
     *
     * @param request The servlet request we are processing
     */
    protected String selectEncoding(ServletRequest request) {

        return (this.encoding);

    }

}//EOC


WEB.XML文件:将下面的代码放在WEB.xml文件的<display-name>下面就行了
    <filter>
       <filter-name>encodingFilter</filter-name>
       <display-name>EncodingFilter</display-name>
       <description>Set the request encoding</description>
       <filter-class>com.voip.admin.util.EncodingFilter</filter-class>
       <init-param>
         <param-name>encoding</param-name>
         <param-value>GB18030</param-value>
      </init-param>
      <init-param>
         <param-name>ignore</param-name>
         <param-value>true</param-value>
      </init-param>
    </filter>

    <filter-mapping>
        <filter-name>encodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

4月11日
Jsp生成隨機驗證碼
<%@ page contentType="image/jpeg" import="java.awt.*,java.awt.image.*,java.util.*,javax.imageio.*" %> 
<% 
// 在記憶體中創建圖像
int width=60, height=20;
BufferedImage image = new BufferedImage(width, height,BufferedImage.TYPE_INT_RGB);
// 獲取圖形上下文
Graphics g = image.getGraphics();
// 設定背景色
g.setColor(new Color(0xDCDCDC));
g.fillRect(0, 0, width, height);
//畫邊框
g.setColor(Color.black);
g.drawRect(0,0,width-1,height-1);
// 取隨機產生的認證碼(4位元數位)
String str1="0123456789";//abcdefghijklmnopqrstuvwxyz";
int codeLength=4;
String rand="";
Random random2 = new Random();
for(int ii=0;ii<=codeLength;ii++)
{
 int x=random2.nextInt(str1.length());
 int y=x+1;
 rand=rand+str1.substring(x,y);
}
/*
switch(rand.length())
{
case 1:rand = "000"+rand; break;
case 2:rand = "00"+rand; break;
case 3:rand = "0"+rand; break;
default:rand = rand.substring(0,4); break;
}*/
// 將認證碼存入SESSION
session.setAttribute("rand",rand);
// 將認證碼顯示到圖像中
g.setColor(Color.black);
String numberStr = rand.toString();
g.setFont(new Font("Atlantic Inline",Font.PLAIN,18));
String Str = numberStr.substring(0,1);
g.drawString(Str,4,17);
Str = numberStr.substring(1,2);
g.drawString(Str,19,15);
Str = numberStr.substring(2,3);
g.drawString(Str,34,18);
Str = numberStr.substring(3,4);
g.drawString(Str,49,15);
// 隨機產生88個干擾點,使圖像中的認證碼不易被其他程式探測到 
Random random = new Random();
for (int i=0;i<88;i++)
{
int x = random.nextInt(width);
int y = random.nextInt(height);
g.drawOval(x,y,0,0);
}
// 圖像生效 
g.dispose();
// 輸出圖像到頁面 
ImageIO.write(image, "jpeg", response.getOutputStream());
%>
3月11日
VB中如何动态调用dll
Create a new project and add this code to Form1 Private Declare Function FreeLibrary Lib "kernel32" (ByVal hLibModule As Long) As Long Private Declare Function LoadLibrary Lib "kernel32" Alias "LoadLibraryA" (ByVal lpLibFileName As String) As Long Private Declare Function GetProcAddress Lib "kernel32" (ByVal hModule As Long, ByVal lpProcName As String) As Long Private Declare Function CallWindowProc Lib "user32" Alias "CallWindowProcA" (ByVal lpPrevWndFunc As Long, ByVal hWnd As Long, ByVal Msg As Any, ByVal wParam As Any, ByVal lParam As Any) As Long Private Sub Form_Load() On Error Resume Next 'KPD-Team 1999 'URL: http://www.allapi.net/ 'E-Mail: KPDTeam@Allapi.net 'We're going to call an API-function, without declaring it! Dim lb As Long, pa As Long 'map 'user32' into the address space of the calling process. lb = LoadLibrary("user32") 'retrieve the address of 'SetWindowTextA' pa = GetProcAddress(lb, "SetWindowTextA") 'Call the SetWindowTextA-function CallWindowProc pa, Me.hWnd, "Hello !", ByVal 0&, ByVal 0& 'unmap the library's address FreeLibrary lb End Sub
vb程序帶參數
和普通工程一样
试一下如下代码
Private Sub Form_Load()
    MsgBox Command()
End Sub
 
 
 
 
 
Command 函数示例
本示例在某个函数中用 Command 函数获得命令行参数,并将命令行参数以 Variant 类型之数组返回。
Function GetCommandLine(Optional MaxArgs)
   '声明变量。
   Dim C, CmdLine, CmdLnLen, InArg, I, NumArgs
   '检查是否提供了 MaxArgs 参数。
   If IsMissing(MaxArgs) Then MaxArgs = 10
   ' 使数组的大小合适。
   ReDim ArgArray(MaxArgs)
   NumArgs = 0: InArg = False
   '取得命令行参数。
   CmdLine = Command()
   CmdLnLen = Len(CmdLine)
   '以一次一个字符的方式取出命令行参数。
   For I = 1 To CmdLnLen
      C = Mid(CmdLine, I, 1)
      '检测是否为 space 或 tab。
      If (C <> " " And C <> vbTab) Then
         '若既不是 space 键,也不是 tab 键,
         '则检测是否为参数内含之字符。
         If Not InArg Then
         '新的参数。
         '检测参数是否过多。
            If NumArgs = MaxArgs Then Exit For
               NumArgs = NumArgs + 1
InArg = True
            End If
         '将字符连接到当前参数中。
         ArgArray(NumArgs) = ArgArray(NumArgs) & C
      Else
         '找到 space 或 tab。
         '将 InArg 标志设置成 False。
         InArg = False
      End If
   Next I
   '调整数组大小使其刚好符合参数个数。
   ReDim Preserve ArgArray(NumArgs)
   '将数组返回。
   GetCommandLine = ArgArray()
End Function


<script></script>
 
 

<script type="text/javascript"></script>

<script></script>
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值