ปัญหามันเกิดจากค่า default uriEncoding ของ tomcat จะเป็น ISO-8859-1 (ตัวอักษรอังกฤษเท่านั้น) ถึงเราจะใช้ servlet filter เช่น org.springframework.web.filter.CharacterEncodingFilter
กำหนดไว้ที่ web.xml แล้วก็ตาม เราจะต้องกำหนด uriEncoding ของ tomcat connector เป็น UTF-8 ด้วย
วิธีแก้ไข คือบอก tomcat ให้ใช้ uriEncoding เป็น UTF-8 ด้วย property นี้ maven.tomcat.uriEncoding
ซึ่งทำได้หลายวิธีดังนี้
วิธีที่ 1. ใส่ property เวลา execute mvn tomcat:run
$ mvn tomcat:run -Dmaven.tomcat.uriEncoding=UTF-8
วิธีที่ 1 มันเปลืองพลังงานการพิมพ์ นำมาสู่วิธีที่ 2
วิธีที่ 2. ใส่ไว้ที่ pom.xml ในส่วนที่เราประกาศใช้งาน tomcat-maven-plugin
<project>
...
<build>
...
<plugins>
...
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
<configuration>
<uriEncoding>UTF-8</uriEncoding>
</configuration>
</plugin>
...
</plugins>
...
</build>
...
</project>
ข้อสังเกต หากทำแล้วก็ยังใช้งานไม่ได้อีก ให้สังเกตดูที่ console เวลา log มันวิ่งไป ว่า maven มันใช้งาน tomcat-maven-plugin ตัวไหนกันแน่
[INFO] <<< tomcat-maven-plugin:1.1:run (default-cli) @
ซึ่งหากเป็นแบบนี้ คือมันดันไปเอา tomcat plugin version 1.1 มาใช้ แทนที่จะเอา version 2.2 ตามที่เรา config เอาไว้ใน pom ข้างต้นแล้วล่ะก็ มันก็เท่ากับว่าเรา config ผิดตัวน่ะสิ่ -_-” แสดงว่ามันมี dependencies บางตัวที่ไปดึงเอา tomcat คนละตัวมา
ถ้าไม่อยากแก้ไข pom เรายังมีไม้สุดท้าย วิธีที่ 3
วิธีที่ 3. กำหนด properties เป็น global ไปเลย
<project>
…
<properties>
…
<maven.tomcat.uriEncoding>UTF-8</maven.tomcat.uriEncoding>
…
</properties>
...
</project>
หากสังเกตดูจะพบว่าการที่เรากำหนด configuration ต่างๆใน properties tag เนี่ย จะเหมือนกับที่เราใส่ตอนที่ execute ใน command line แล้วใส่ -D
เลย
ฉะนั้นหากจะต้องมาพิมพ์ใส่ -D
หลายๆตัวใน command line มันก็ดูเยอะ เลอะเทอะ ย้ายเอามาใส่ใน properties tag นี้ ได้นะเออ
Happy Coding ^ ^
ไม่มีความคิดเห็น :
แสดงความคิดเห็น