spingMVC問題小結

來源:互聯網
上載者:User

標籤:

環境: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&amp;characterEncoding=utf-8
即:
            jdbc:mysql://localhost:3306/logistichm?useUnicode=true&amp;characterEncoding=utf-8

       

1     <property name="url">2             <value>jdbc:mysql://localhost:3306/logistichm?useUnicode=true&amp;characterEncoding=utf-83         4             </value>5         </property>

 

spingMVC問題小結

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.