標籤:
環境:Eclipse10、Tomcat7
項目:spingMVC+hibernate+mysql
編碼:前台和資料庫同一編碼:UTF-8
程式設計語言:java
一、後台傳值亂碼問題。
1.jsp傳資料到後台post方式中文亂碼。
web.xml檔案加入下面代碼,即可解決post方式傳值的亂碼問題。
1 <filter> 2 <filter-name>encodingFilter</filter-name> 3 <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> 4 <init-param> 5 <param-name>encoding</param-name> 6 <param-value>UTF-8</param-value> 7 </init-param> 8 <init-param> 9 <param-name>forceEncoding</param-name> 10 <param-value>true</param-value> 11 </init-param> 12 </filter> 13 <filter-mapping> 14 <filter-name>encodingFilter</filter-name> 15 <url-pattern>/*</url-pattern> 16 </filter-mapping>
2、jsp傳資料到後台get方式中文亂碼。
二、js檔案亂碼問題。
1.選中js檔案,右鍵屬性,將編碼改為UTF-8.
2.jsp頁面:<script src="${ctx}/view/js/login.js" type="text/javascript" charset=UTF-8></script>
PS:
1.src也可使用絕對路徑
2.${ctx}後台傳送的資料為:String ctx=request.getContextPath();
三、樣式問題。
將下面代碼加入springMVCServlet-servlet.xml。
1 <mvc:annotation-driven /> 2 <mvc:resources mapping="/view/css/**" location="/view/css/" />3 <mvc:resources mapping="/view/images/**" location="/view/images/" />4 <mvc:resources mapping="/view/js/**" location="/view/js/" />5 <mvc:resources mapping="/kaptcha/**" location="/kaptcha/" />
例:某CSS路徑為 /Logistic/view/css/admin.css
則location="/view/css/" mapping="/view/css/**"
前台頁面:<LINK href="/Logistic/view/css/admin.css" type="text/css" rel="stylesheet" charset=UTF-8>
Logistic為項目名
四 、掃描java包。
將下面代碼加入springMVCServlet-servlet.xml。
1 <context:component-scan base-package="com.controller" />2 <context:component-scan base-package="org.cric.util" />
第一行:掃描"com.controller"下面的所有帶@Controller的檔案,包括"com.controller.login"下面的所有帶@Controller的檔案
第二行:掃描"org.cric.util"下面的所有帶@Controller的檔案
五、資料庫接收中文資料。
applicationContext.xml內
jdbc:mysql://localhost:3306/logistichm
加入下面代碼:
?useUnicode=true&characterEncoding=utf-8
即:
jdbc:mysql://localhost:3306/logistichm?useUnicode=true&characterEncoding=utf-8
1 <property name="url">2 <value>jdbc:mysql://localhost:3306/logistichm?useUnicode=true&characterEncoding=utf-83 4 </value>5 </property>
spingMVC問題小結