使用servlet和mybatis開發(fā)web項(xiàng)目中的問題

編碼過濾器

<filter>
  <filter-name>encodingFilter</filter-name>
  <filter-class>org.springframework.web.filter.CharacterEncodingFilter

  </filter-class>
  <init-param>
   <param-name>encoding</param-name>
   <param-value>UTF-8</param-value>
  </init-param>
  <init-param>
   <param-name>forceEncoding</param-name>
   <param-value>true</param-value>
  </init-param>
 </filter>

如果設(shè)置了過濾器后,html頁面亂碼,jsp頁面正常顯示,這時(shí)候要?jiǎng)h除
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>

解決中文數(shù)據(jù)存入數(shù)據(jù)庫亂碼, 頁面亂碼等問題
不使用框架的web項(xiàng)目中配置log4j

使用ThreadLocal來管理SqlSession中應(yīng)注意session關(guān)閉問題,在關(guān)閉SqlSession時(shí)要清空ThreadLocal(調(diào)用remove方法)

package com.crsbg.utils;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.Reader;

public class DBUtils {
    private static ThreadLocal<SqlSession> threadLocal = new ThreadLocal<>();
    private static SqlSessionFactory factory = null;
    //初始化SqlSessionFactory
    static {
        Reader reader = null;
        try {
            reader = Resources.getResourceAsReader("config.xml");
            factory = new SqlSessionFactoryBuilder().build(reader);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(reader!=null) {
                try {
                    reader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    //獲取SqlSession
    public static SqlSession getSqlSession() {
        SqlSession session = threadLocal.get();
        if (session==null){
            session = factory.openSession();
            threadLocal.set(session);
        }
        return session;
    }
    //關(guān)閉SQL Session
    public static void closeSession(){
        SqlSession session = threadLocal.get();
        if(session!=null){
            session.close();
            //需要移出,否則會(huì)出現(xiàn)org.apache.ibatis.executor.ExecutorException: Executor was closed異常
            threadLocal.remove();
        }
    }
}

使用Idea開發(fā)web設(shè)置
1.idea中tomcat亂碼:
a. file - settings - 搜File Encodings,改為utf-8
b. 在help菜單 點(diǎn)擊 edit custom VM Options,最后追加-Dfile.encoding=UTF-8
c.配置tomcat的頁面中:VM option設(shè)置:-Dfile.encoding=UTF-8
2.熱部署問題(jsp+java)在Tomcat的運(yùn)行參數(shù)中設(shè)置
Update:更新操作(經(jīng)測(cè)試,很多時(shí)候無效)
Frame:idea失去焦點(diǎn)時(shí)觸發(fā)
推薦選項(xiàng):
Update:任意
Frame:update classes and resources
idea:熱部署,如果是run啟動(dòng),僅JSP等靜態(tài)資源有效
如果是debug啟動(dòng),java和jsp等均有效
總結(jié) 熱部署:
a. Frame:update classes and resources
b. 以debug模式啟動(dòng)
注意:編寫servlet前 需要先加入tomcat環(huán)境

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容