当前位置:网站首页>Chinese garbled code caused by Grails
Chinese garbled code caused by Grails
2022-07-18 08:54:00 【Four fires】
In a Grails Inside the project , I want to write a filter for bad information Service, And put the filtering rules in xml In the document .
following xml File placement bad information , Contains words 、 Phrases and syntax , regular expression :
<filter>
<words>
<word>fuck</word>
<word>kill</word>
</words>
<phrases>
<phrase>asshole</phrase>
</phrases>
<grammars>
<grammar>kick.*ass</grammar>
<grammar>impud.nce</grammar>
</grammars>
</filter>Then there is a filter Service:
classFilterService{
def private static node=newXmlParser().parse(newFile('./config/filter.xml'))
static Stringfilter(Stringsource){
def substitution=node.substitution.text()
def sensitive=[node.words.word*.text().join('|'),
node.phrases.phrase*.text().join('|'),
node.grammars.grammar*.text().join('|')].join('|')
if(!source||!sensitive)returnsource
returnsource.replaceAll(sensitive,substitution)
}
}That's the code , It turns out that Grails There is a Chinese problem while running , And not just pages , stay filter Chinese cannot be displayed correctly in the method , and Service The same code in common Groovy There is no Chinese problem in the application .
As before J2EE Development experience , Try the following method :
String result=new String(testString.getBytes("iso-8859-1"),"UTF-8")Still garbled ……
Try it again : Add defined operating parameters -Dfile.encoding=UTF-8 Invalid ……
xml Add... To the document
<?xmlversion="1.0"encoding="UTF-8"?>It still doesn't work ……
I've been thinking about it for a long time , Come up with a solution :
For example, we plan to unify the coding of the whole project into UTF-8 Format , I use Eclipse Development , my Eclipse The default code is GBK Of , Then this can remain unchanged , Right click on the project , choice Properties Medium Resource, take Text file encoding Set to UTF-8.
Be careful : The consequence is the project groovy And other documents originally used GBK Chinese will become garbled ! So please choose the appropriate code .
Then delete the operating parameters -Dfile.encoding=UTF-8.
Make another change :
// The original
def node=newXmlParser().parse(newFile('./config/filter.xml'))
// Change it to
def node=newXmlParser().parseText(newFile('./config/filter.xml').getText())Last , The old rule on the page , add :
<meta http-equiv="Content-Type" content="text/html; charset=GBK" />That's all right. .
If your code is GBK Of , Handle it the same way .
The articles are all original without special indication , It shall not be used for any commercial purpose without permission , Please keep the integrity of reprint and indicate the source link 《 Four fire's nagging 》
×Scan to share with WeChat
边栏推荐
- Test the speed characteristics of optical flow sensor
- One article understand go integrate CAPTCHA to realize the function of verification code
- DNS attack protection principle
- 这个国产编辑器,即将开源!
- (codeforce319)B.Psychos in a Line(单调栈)
- WPF Visifire.Charts4.6.1使用教程 附含源码
- ros(25):发布一个静态的里程计odom信息
- 【MATLAB项目实战】基于GUI的数字信号处理系统
- The logic of archives | archives collection
- sql计算前top n%的数据
猜你喜欢

国内外知名的待办事项app

Event 4624 is login successful!?! Is that true?

蓝帽杯2022初赛电子取证

(2021 Niuke multi school V) d-double strings (multiplication principle + dynamic programming)
![[leetcode weekly -- hash table simulation] 6113 The smallest number in an infinite set](/img/4f/817ca6ff4265ab2fafe71210e22a4e.png)
[leetcode weekly -- hash table simulation] 6113 The smallest number in an infinite set

When the wechat applet activates the account, it will prompt "this account has been activated, please log in directly with the account password"

九联科技开发板正式合入OpenHarmony主干

The logic of archives | archives collection

使电脑拥有公网IP方法

(codeforce631)C.Report(单调栈)
随机推荐
EasyCVR视频调阅页面如何增加对应视频的云台控制?
九联科技开发板正式合入OpenHarmony主干
Top level planning scheme of "smart forest and grass" in Heilongjiang
STM32 and Internet of things 02 network data sending and receiving
如何清理你的电子邮件订阅者名单以改善电子邮件营销
Honghu Wanlian Zhiyuan development board is officially integrated into the openharmony backbone
HybridCLR——划时代的Unity原生C#热更新技术
[leetcode binary tree -- maximum path sum] 124 Maximum path sum in binary tree
【集训DAY1】Spy dispatch【最小生成树】
Huawei's general card identification function enables multiple card bindings with one key
2022.7.11~8.1纪中游记
Real time user session tracking using eventlog analyzer
VBA驱动SAP GUI完成界面元素值初始化
(2021牛客多校五)K-King of Range(单调队列/ST表)
金融、生物医药、集成电路,上海巨头行业用AI加持核心竞争力
【Luogu_P4556】 [Vani有约会]雨天的尾巴 /【模板】线段树合并
[leetcode string -- public prefix] bm84 Longest Common Prefix
【Arduino与mpu6050握手】
290 pages 110000 words digital agriculture rural project planning and construction scheme 2022
How to set the allure test report