I'm developing a web application using Struts 2 and Hibernate.
But, I have a problem that, when I start a server and load website then my loading method in the action will be called, but it can't load UTF-8, instead of "Phương"
I get "PH??NG"
. But in JSP page still display "Phương"
. So that, when I execute my SQL query and it is (from Posts WHERE topics like '%PH??NG PHÁP GIÁO D?C TR?%'
, so I cannot query to my database.
My action class here:
public class PostsAction extends ActionSupport {private List<HomeTopic> homeTopics = new ArrayList<HomeTopic>();public List<HomeTopic> getHomeTopics() { return homeTopics;}public void setHomeTopics(List<HomeTopic> homeTopics) { this.homeTopics = homeTopics;}public void callHomeTopics(){ Session session = HibernateUtil.getSessionFactory().openSession(); Transaction trans = session.beginTransaction(); String[] list = {"NEW – TIN TỨC TRẺ","Khu Vui Chơi Cho Trẻ","PHƯƠNG PHÁP GIÁO DỤC TRẺ","KỸ NĂNG – KINH NGHIỆM","Phòng Bệnh","Tập Tô Màu"}; for (int i = 0; i < list.length; i++) { getTopics(session, trans, list[i]); }trans.commit(); session.close();}public void getTopics(Session session, Transaction trans, String type) { String hql = "from Posts WHERE topics like '%" + type +"%'"; System.out.println("HQL: " + hql); Query query = session.createQuery(hql); query.setMaxResults(3); List<Posts> t = (ArrayList<Posts>) query.list(); HomeTopic h = new HomeTopic(type, t); System.out.println("List topics: " + type +" " + t); homeTopics.add(h); session.flush();}
HomeTopics.java
class:
public class HomeTopic { private String nameTopic; private List<Posts> postList; public List<Posts> getPostList() { return postList; } public void setPostList(List<Posts> postList) { this.postList = postList; } public HomeTopic(String nameTopic, List<Posts> postList) { this.nameTopic = nameTopic; this.postList = postList; } public HomeTopic() { } @Override public String toString() { return "HomeTopic{" +"nameTopic=" + nameTopic +", postList=" + postList +'}'; } public String getNameTopic() { return nameTopic; } public void setNameTopic(String nameTopic) { this.nameTopic = nameTopic; }}
When I write a main method to test, It still displays UTF-8. But, whenever I start the server and call callHomeTopics()
method in the browser the text is "PH??NG PHÁP GIÁO D?C TR?"
,but in JSP page still display "Phương Pháp Giáo Dục Trẻ"
.
Everything is ok except my HQL query.