RESROUCEBUNDLE Chinese Issue My Solution

xiaoxiao2021-03-06  122

I used to use Struts Hibernate, did not encounter this problem.

Application_zh.properties is written in JSP with GBK

<%

ResourceBundle B = ResourceBundle.getBundle ("Application", Request.getLocale ());

%>

<% = B.GetString ("a.a")%>

If JSP does not specify an encoding method, use the default ISO-8859-1, there is no problem. But under my Mozilla, you need to manually call the code, use GBK.

But there is a bad place, that is, if the browser must make the user in hand, it is too much like a word.

However, if the JSP is specified as GBK or other words, there is no other way to use Native2ASCII to transfer the Application_ZH.Properties file. The editor that transcoded with Eclipse is not available, hehe.

As mentioned earlier, I now solve the, the method is to write a TAG. All the words you want to output in all JSPs will pass this tag, and this tag is responsible for reading the text in Application_Zh.Properties in a certain encoding method. Turn into a certain encoding mode.

This TAG includes a Java class, a Taglib description file. as follows:

Message.java

/ *

* Project Ffcinema

* Filename Message.java

* Last Modified On 2004-10-11

* Author by hongliang

* /

Package ff.cinema.tag;

Import java.io.ioException;

Import java.io.unsupportedEncodingexception;

Import java.util.resourcebundle;

Import javax.servlet.jsp.jspexception;

Import javax.servlet.jsp.jspwriter;

Import javax.servlet.jsp.tageXt.tagsupport;

/ **

* @Author Hongliang

*

* Solve the JSP reading resourcebundle Chinese garbled TAG method to read Application_ZH.Properties based on ISO-8859-1 encoding,

* Then turn to GBK after output

*

* Taglib files about this tag: Web-inf / fftag.tld

* /

Public Class Message Extends Tagsupport

{

/ **

* This label properties Key

* /

String Key = "";

/ **

* What code is read, the default is ISO-8859-1

* /

String fromencoding = "ISO-8859-1";

/ **

* Transfer why coding, default is GBK

* /

String toencoding = "GBK";

/ **

* Resource text, default is Application

* /

String bundle = "application";

Public void setKey (String K)

{

Key = k;

}

Public int desartTAG () THROWS JSPEXCEPTION

{

ResourceBundle B = ResourceBundle.getBundLe (Bundle, PageContext.getRequest (). GetLocale ());

Jspwriter out = pageContext.getut ();

Try

{

IF (Key! = null && key.length ()> 0)

{

Out.print (New String (B.getstring (key) .Getbytes (Fromencoding),

TOENCODING));

}

} catch (unsupportedEncodingexception E)

{

// ignore it

} catch (ioException e)

{

Throw new JSPEXCEPTION (E);

}

Return Skip_body;

}

/ **

* @Param bundle

* The bundle to set.

* /

Public void setBundle (String bundle)

{

THIS.Bundle = bundle;

}

/ **

* @Param Fromencoding

* The fromencoding to set.

* /

Public void setFromencoding (String Fromencoding)

{

THIS.FROMENCODING = Fromencoding;

}

/ **

* @Param toEncoding

* The toencoding to set.

* /

Public void settoencoding (String toEncoding)

{

THIS.TOENCODING = TOENCODING;

}

}

FFTag.TLD

DOCTYPE TAGLIB

PUBLIC "- // Sun microsystems, Inc.//dtd JSP Tag Library 1.2 // en"

"http://java.sun.com/j2ee/dtd/web-jsptaglibrary_1_2.dtd">

1.0

1.2

Fftag

http: // localhost: 8080 / ffcinema / fftag

Message

ff.cinema.tag.Message

EMPTY

Key

True

TRUE

fromencoding

false

TRUE

toEncoding

false

TRUE

bundle

false

TRUE

After that, use this taglib behind Web.xml:

http: // localhost: 8080 / ffcinema / fftag

/web-inf/fftag.tld

In this way, in JSP can be used in this, it is simpler than the previous way:

<% @ page contenttype = "text / html; charset = GBK"%>

<% @ Taglib Uri = "http: // localhost: 8080 / ffcinema / fftag" prefix = "ff"%>

转载请注明原文地址:https://www.9cbs.com/read-100453.html

New Post(0)