Pager-taglib pagination Chinese garbled principle analysis

Source: Internet
Author: User

A solution to the problem of passing Chinese parameter garbled characters using Pager-taglib paging front end

1. Reproduce the problem

Pager-taglib is sometimes used in Web projects as a tab for pagination, as shown in the Red box logo, and when we need to keep the page parameters, we pass the parameters in the <pg:param/> tag.

If your page is encoded as gb2312 that's fine, but if your page code is UTF-8, then there will be garbled problems. I tried a lot of methods, coded filters, coded interceptors (STRUTS2), passed

Parameters are encoded and then decoded in the background, as well as the web (such as Tomcat) application server encoding. Still feel that these methods do not fundamentally solve the problem. Now that we are paging with the pager-taglib tag, we

Take a look at Pager-taglib's handling of character set encoding.

2.pager-taglib character Set processing source code

A. First, we need to prepare a pager-taglib-2.0 war package.

B. Then we'll unzip it.

Below the Web-inf, there are two jar packages underneath the Lib package

C.pager-src.jar This is the Pager-taglib source package, we open it to find the final void Addparams (String name, StringValue) This method

The red line of code is easy to see that the Urlencoder.encode (value) method is to pass the parameters of the character set encoding processing, that specific how to deal with it?

We continue to follow up to see the source code.

D.urlencoder Source

After finding this method, we find that it has been marked with @deprecated annotation, what is the meaning, because English is not good, we open the translation tool to check

Well, it's a deprecated method, but it doesn't matter, this method calls another encode (s, dfltencname) method, so let's go ahead and see

A large section of the source code, to see the key place

if (enc = = null) {

throw new NullPointerException ("CharsetName");


I am not a technical Daniel, but when I see the Charstname keyword, we still know that it is definitely related to the character set, what does enc mean by this parameter?

Look up, this is the outdated encode (s, dfltencname) passed over the parameters dfltencname is not defaultencname abbreviation?

What do you mean? The default environment name? I don't know where to look. There are operations on this variable:

and found it in the Staic static block of this class.

I still do not understand how to deal with the process, do not worry, continue to find this getpropertyaction ("file.encoding") trouble, but see this method is not very familiar?

I want to get our current operating system character set encoding not all this?

And then we'll go into the source code of this class Getpropertyaction

Well, see here it must be understood that the original Pager-taglib the default character encoding is the use of your current operating system encoding method,

Even if you set the other character set encoding, it will not control you, it is still get the current system's character set encoding method and then processing, suddenly feel

Write this pager-taglib man good jb stupid. In general, if we are using the Windows operating system, then the code is GBK, so I understand it.

Because your page is UTF-8 encoded, and you pass the Chinese parameters when the Pager-taglib and the default GBK encoding for processing, of course, will appear garbled.

What about the solution?

Let's go back to the Pagertag class, just below the Encode () method, let's take a closer look at this line of code

String[] values = Pagecontext.getrequest (). getparametervalues (name);

Is this not the PageContext object of cow B? Can we get the page from this context you set the encoding method?

The answer is yes, so we just need to make the following changes


3. Problem solving

OK, the next thing to do is very simple, will you modify the whole thing into jar package and then upload the project can solve the Chinese garbled problem, you no longer encode (), decode () back.

A. First create a new project named Pager-taglib in your IDE, and then import the following Java classes and meta-if from the official PAGER-SRC below the COM package

Find the class for the modifications in the above solution and then export the jar package with your IDE, I'm using MyEclipse

FILE--Export-->java---JAR file

4. Summary

When I first met this problem, I also went online to find a solution. About the last to change the code of this part, the Internet has related articles, but almost all just say pager-talib use is your system

The default encoding method needs to be changed to the encoding of your page. But the individual does not like this know it does not know the way, so he followed the source to find the pager-taglib in the end is how to use

System default encoding method, and finally want to get the page encoding method and see the PageContext this object, but also enhance their understanding of PageContext this object. Because the internet has a very

Multi-modified encoding is written directly, like this.

Name = (name, "Utf-8");
Value = (value, "utf-8");

Personally think this is a very stupid way, because the cause of Chinese garbled is not that you do not use UTF-8 encoding, but because the page and Pager-taglib processing Chinese garbled encoding inconsistency caused,

Although now basically the processing of Chinese encoding is utf-8 and GBK but if I have a lot of Chinese encoding, when I use another encoding of the page you still write dead here Utf-8

Wouldn't it be 2 B? So this is still recommended.

Name = (name, Pagecontext.getresponse (). getcharacterencoding ());

Value = (Values[i], Pagecontext.getresponse (). getcharacterencoding ());

This way, because all we need to do is take the page encoding and pager-taglib to be consistent.

Of course, there may be some other better solution, or other problems caused by the garbled problem, this is only recommended for individuals to find a suitable solution to the problem.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Pager-taglib pagination Chinese garbled principle analysis

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.