0

I have a website to get survey from employee of a company using Struts2 and Hibernate, MySQL, using Tomcat 6 server. This is method to get question of an event, the question of event is using UTF-8 encoding. This is my code:

public String getEventQuestion() {
          try {
                 event = surveyEventController.getEvent(this.getId());
                 surveyQuestions = surveyQuestionController.getQuestion(this.getId());
                 if (surveyQuestions != null) {
                       return Constants.SUCCESS;
                 } else {
                       return Constants.FAIL;
                 }
          } catch (Exception e) {
                 logger.error("[Exception]getSurvey: " + e.getMessage(), e);
                 return Constants.FAIL;
          }
   }

getQuestion method from controller:

@SuppressWarnings("unchecked")
   public List<SurveyQuestion> getQuestion(Long eventID)
   {
          Session session = HibernateUtil.getSessionFactory().getCurrentSession();
          List<SurveyQuestion> listQuestion = null;
          try
          {
                 session.getTransaction().begin(); 

                 listQuestion = (List<SurveyQuestion>)session.createQuery("from SurveyQuestion where event_id="+eventID.toString()).list();
          session.getTransaction().commit();
          }
          catch(Exception e)
          {
                 logger.error("[Exception] getQuestion SurveyQuestion: ",e);
          session.getTransaction().rollback();
          }
          if(listQuestion!=null && !listQuestion.isEmpty())
          {
                 return listQuestion;
          }
          else
          {
                 return null;
          }
   }

All the jsp page encoding is UTF-8 also.

On the first time I access website, the content is successfully loaded with UTF-8 content as I expect.

The problem is after I press F5 to refresh the page, the content is automatically changed to non UTF-8 encoding. I debug and recognize that when the line session.getTransaction().commit() is run, the database automatically update with the listQuestion in non-UTF-8 encoding content, maybe ISO or something like that, all special character is changed to ?. How can I solve this issue?

4

1 回答 1

0

我假设我可以定义字符编码

<property name="connection.url">
        jdbc:mysql://localhost:3306/survey?useUnicode=true&amp;amp;characterEncoding=UTF-8;
    </property>

但是当上述方法不起作用时,我找到了另一种解决方案,在 hibernate.cfg.xml 中,您必须通过以下方式定义休眠字符编码

<property name="hibernate.connection.characterEncoding">UTF-8</property>

它工作正常。感谢您对罗马 C 的支持。

于 2013-07-09T02:16:16.340 回答