During web application development, data interaction between the front and back ends is required in many cases. Sometimes we may need to transmit the Chinese data from the front end to the back end through URL, but at this time there is a headache, Because Java Network Transmission uses the standard character set is ISO-8859-1, so in the background with request. getParameter ("message"); get the front-end passed Chinese, the obtained is the ISO-8859-1 Character Set, Chinese will appear garbled phenomenon, the solution for many people is to perform decode before the front-end transmits Chinese characters, and then decode the back-end for conversion. This is very troublesome. Isn't there any other solution?
I thought for a moment: why can I transmit Chinese characters in the form mode, but the URL mode cannot be used? Can the problem be solved only when URL-based value passing is required? Here I think of a dynamic form. Why not use it? The example is as follows:
For example, in the following scenarios: On the KPI list page, the alarm data is generated for each KPI. When you click the button for generating data for each KPI in the list, you need to pass the KPI id and name to the background.
Js method corresponding to the frontend:
Use the URL to pass the value:
Copy codeThe Code is as follows: function createData (indexId, indexName ){
Window. location. href = "catalogAction. do? Action = CreateIndexData & catalogId = "+ indexId +" & catalogName = "+ indexName +" & random = "+ Math. random ();
}
In this way, the KPI name is mostly Chinese, and the value received in the background must be garbled.
If you use the dynamic form generation method, you can solve the problem:
Copy codeThe Code is as follows: function createData (indexId, indexName ){
Var urlStr = "catalogAction. do? Action = CreateIndexData & PcatalogId = <c: out value = '$ {catalogId}'/> & catalogId = "+ indexId +" & random = "+ Math. random ();
Var f = document. createElement ('form ');
F. action = urlStr;
F. method = 'post ';
Document. body. appendChild (f );
Var temp = document. createElement ('input ');
Temp. type = 'ddn ';
Temp. value = indexName;
Temp. name = 'catalogname ';
F. appendChild (temp );
F. submit ();
}
The Chinese field is submitted in form mode. If it is not Chinese, the URL is still used for transmission. This easily solves the problem of garbled Chinese characters passed to the background.