目錄 1.環境說明,以及前言 2.問題描述: 3.失敗的方法(這里寫失敗并不代表在其他情況不管用) 3.1 html網頁本身編碼不是UTF-8(推薦嘗試) 3.2 web.xml中沒有設置配置編碼方式(ssm框架配置必定配置) 3.3修改html 文件(感覺沒屌用) 3.4 修改html文件中的<head>(html文件一定要加這個) 3.5 發送內容設置為中文 3.6 JSP翻譯為Servlet時的編碼(jsp下的配置) 3.7 jsp下后臺設置編碼(jsp下serverlet是肯定要寫的,可以用配置字符攔截器之類的替代這個) 3.8 給tomcat的server.xml文件設置編碼格式(推薦嘗試下) 4 問題就是這樣解決的 @(SpringMvc中設置了html視圖解析器卻不起作用(看了幾十多篇文章的相關內容的精華合集)) 1.環境說明,以及前言 使用ssm框架、IDEA、JKD1.8、因為這個模塊寫的是上傳模板、先開始用ajax傳,但是不知為啥不能注入,只能選擇表單,然后跳轉,所以要兩個頁面,成功頁面和失敗頁面。 2.問題描述: 成功之后跳轉頁面html文件的中文呈現亂碼,但是我直接訪問靜態資源是中文不是亂碼,我就查了近乎幾十篇文章,找到了許多方法。 3.失敗的方法(這里寫失敗并不代表在其他情況不管用) 3.1 html網頁本身編碼不是UTF-8(推薦嘗試) 描述:這個辦法就是html的網頁格式本來就不是UTF-8,自然訪問的時候,顯示是亂碼 解決辦法:用記事本打開,然后點擊另存為,設置為UTF-8 3.2 web.xml中沒有設置配置編碼方式(ssm框架配置必定配置) <!-- 配置編碼方式--> <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> <filter-mapping> <filter-name>encodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <filter-mapping> <filter-name>encodingFilter</filter-name> <url-pattern>*.html</url-pattern> </filter-mapping> 3.3修改html 文件(感覺沒屌用) 在百度的時候也查到了這這一篇zh-CN 說將en改為 至于原因可以看下面這一篇文章(但是這個根本沒有幫助,這個解釋是說這是給搜索引擎有幫助) //www.w3school.com.cn/tags/html_ref_language_codes.asp 3.4 修改html文件中的<head>(html文件一定要加這個) 在head標簽中加入: <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 至于這個代碼代表什么意思具體可以查看項目這個文章 //www.cnblogs.com/liubeimeng/p/3894177.html 我總結大概意思就是說,這個meta是html的元標簽,其中包含了對應html的相關信息,客戶端瀏覽器或服務端程序都會根據這些信息進行處理.==charset(編碼):這個網頁的編碼是UTF-8,需要注意的是這個是網頁內容的編碼,而不是文件本身的.==就是說設定這個文件內容的編碼為UTF-8即漢字編碼 3.5 發送內容設置為中文 比如你用form表單發送內容,你加上accept-charset="UTF-8"這句代碼代表 accept-charset 屬性規定服務器處理表單數據所接受的字符集。 <form action="" method="post" enctype="multipart/form-data" accept-charset="UTF-8"> enctype這個屬性表示form表單的數據以什么形式發送,我這邊有文件上傳所以要加這句,如果你們沒有文件上傳就要把這個刪除 3.6 JSP翻譯為Servlet時的編碼(jsp下的配置) 使用jsp就要在頭文件上加入這句代碼,相當于JSP翻譯為Servlet時的編碼 ==jsp和html網頁最大的區別就是一個是動態生成的,一個是靜態網頁== <%@ page language="java" pageEncoding="utf-8"%> 3.7 jsp下后臺設置編碼(jsp下serverlet是肯定要寫的,可以用配置字符攔截器之類的替代這個) 后臺可以手動設置request字符集 request.setCharacterEncoding("UTF-8"); 3.8 給tomcat的server.xml文件設置編碼格式(推薦嘗試下) 我找的文章說的是get方式 GET提交方式中文亂碼解決 通過修改request.setCharacterEncoding(“UTF-8”);只能解決POST提交方式的請求編碼,對GET無效。 1 、最簡單解決方案: 該解決方案不再需要任何額外配置(如filter) TOMCAT默認ISO-8859-1 因此可以設置默認編碼為UTF-8解決,在conf\server.xml文件中設置如下 Java代碼 <Connector port="8080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" URIEncoding=”UTF-8”/> 2 、在 tomcat4 之后,可以通過如下配置來解決中文亂碼,但必須配置 filter 在conf\server.xml文件中設置如下 Java代碼 <Connector port="8080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" useBodyEncodingForURI=“true” /> 然后request.setCharacterEncoding(“UTF-8”)也可解決GET亂碼 參考資料://www.iteye.com/topic/1121242#2322208 4 問題就是這樣解決的 首先是視圖解析器(我看網上的視圖解析器很多配置都是jsp的,過幾天我寫一個關于配置視圖解析器的) 因為是ssm框架,我沒有使用重定向來跳轉頁面,所以要配置視圖解析器 ==我在網上找到的視圖解析器坑了我一手,他只有:== xml <bean id="freemarkerConfig" class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer"> <property name="templateLoaderPath"> <value>/WEB-INF/view</value> </property> </bean> <bean id="htmlViewResolver" class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver"> <property name="contentType" value="text/html;charset=UTF-8"></property> <property name="suffix" value=".html"></property> <property name="order" value="0"></property> </bean> ==沒有動態化本地設置== <property name="freemarkerSettings"> <props> <prop key="locale">zh_CN</prop> <prop key="defaultEncoding">UTF-8</prop> </props> </property> 我嘗試過上述全部方法后,最后在這里成功了 分享我的視圖解析器: <bean id="freemarkerConfig" class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer"> <property name="templateLoaderPath"> <value>/WEB-INF/view</value> </property> <property name="freemarkerSettings"> <props> <prop key="locale">zh_CN</prop> <prop key="defaultEncoding">UTF-8</prop> </props> </property> </bean> <bean id="htmlViewResolver" class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver"> <property name="contentType" value="text/html;charset=UTF-8"></property> <property name="suffix" value=".html"></property> <property name="order" value="0"></property> </bean> ==同時需要導入一個jar包,maven如下:== xml <dependency> <groupId>org.freemarker</groupId> <artifactId>freemarker</artifactId> <version>2.3.20</version> </dependency> 還要在web.xml中添加如下代碼:(如果沒有的話) xml <filter-mapping> <filter-name>encodingFilter</filter-name> <url-pattern>*.html</url-pattern> </filter-mapping> 什么是Freemarker FreeMarker是一個用Java語言編寫的模板引擎,它基于模板來生成文本輸出。FreeMarker與Web容器無關,即在Web運行時,它并不知道Servlet或HTTP。它不僅可以用作表現層的實現技術,而且還可以用于生成XML,JSP或Java 等。 目前企業中:主要用Freemarker做靜態頁面或是頁面展示 5.后記 寫這篇文章希望能幫到大家,我今天坐在電腦前面一天,就為了搞定這個bug,希望能讓大家少走點彎路。有什么問題都可以在評論區和我交流 分享我的pom.xml配置以及ssm配置和web.xml配置: pom.xml <?xml version="1.0" encoding="UTF-8"?> <!-- Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. --> <!-- $Id: pom.xml 642118 2008-03-28 08:04:16Z reinhard $ --> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <packaging>war</packaging> <name>ITKIMROOT</name> <groupId>com.itkim.rootuser</groupId> <artifactId>ITKIMROOT</artifactId> <version>1.0-SNAPSHOT</version> <properties> <!-- spring版本號 --> <spring.version>4.2.5.RELEASE</spring.version> <!-- mybatis版本號 --> <mybatis.version>3.2.4</mybatis.version> <!-- log4j日志文件管理包版本 --> <slf4j.version>1.6.6</slf4j.version> <log4j.version>1.2.12</log4j.version> </properties> <dependencies> <!-- spring核心包 --> <!-- springframe start --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-oxm</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>${spring.version}</version> </dependency> <!-- springframe end --> <!-- mybatis核心包 --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>${mybatis.version}</version> </dependency> <!-- mybatis/spring包 --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>1.2.2</version> </dependency> <!-- mysql驅動包 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.12</version> </dependency> <!-- junit測試包 --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <scope>test</scope> </dependency> <!-- jstl --> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> <dependency> <groupId>javax.servlet.jsp</groupId> <artifactId>jsp-api</artifactId> <version>2.2.1-b03</version> <scope>provided</scope> </dependency> <!-- servlet --> <dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> <version>2.5</version> <scope>provided</scope> </dependency> <!-- json數據 --> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-core</artifactId> <version>2.7.1</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-annotations</artifactId> <version>2.7.1</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.7.1</version> </dependency> <dependency> <groupId>net.sf.json-lib</groupId> <artifactId>json-lib</artifactId> <version>2.4</version> <classifier>jdk15</classifier> </dependency> <!-- commons --> <dependency> <groupId>commons-lang</groupId> <artifactId>commons-lang</artifactId> <version>2.4</version> </dependency> <dependency> <groupId>commons-logging</groupId> <artifactId>commons-logging</artifactId> <version>1.1</version> </dependency> <dependency> <groupId>commons-pool</groupId> <artifactId>commons-pool</artifactId> <version>1.5.6</version> </dependency> <dependency> <groupId>commons-dbcp</groupId> <artifactId>commons-dbcp</artifactId> <version>1.4</version> </dependency> <dependency> <groupId>commons-beanutils</groupId> <artifactId>commons-beanutils</artifactId> <version>1.8.3</version> </dependency> <dependency> <groupId>commons-httpclient</groupId> <artifactId>commons-httpclient</artifactId> <version>3.1</version> </dependency> <dependency> <groupId>commons-collections</groupId> <artifactId>commons-collections</artifactId> <version>3.1</version> </dependency> <dependency> <groupId>commons-codec</groupId> <artifactId>commons-codec</artifactId> <version>1.9</version> </dependency> <dependency> <groupId>javax.annotation</groupId> <artifactId>jsr250-api</artifactId> <version>1.0</version> </dependency> <dependency> <groupId>net.sf.ezmorph</groupId> <artifactId>ezmorph</artifactId> <version>1.0.6</version> </dependency> <dependency> <groupId>javax.activation</groupId> <artifactId>activation</artifactId> <version>1.1</version> </dependency> <dependency> <groupId>taglibs</groupId> <artifactId>standard</artifactId> <version>1.1.2</version> </dependency> <!-- 日志文件管理包 --> <!-- log start --> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>${log4j.version}</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>${slf4j.version}</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>${slf4j.version}</version> </dependency> <!--上傳--> <dependency> <groupId>commons-fileupload</groupId> <artifactId>commons-fileupload</artifactId> <version>1.3.1</version> </dependency> <dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version>2.4</version> </dependency> <dependency> <groupId>org.freemarker</groupId> <artifactId>freemarker</artifactId> <version>2.3.20</version> </dependency> <!-- log end --> </dependencies> <build> <finalName>helloworld</finalName> <plugins> <plugin> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-maven-plugin</artifactId> <version>1.3.2</version> <configuration> <!--允許移動生成的文件--> <verbose>true</verbose> <!--允許覆蓋生成的文件--> <overwrite>true</overwrite> </configuration> </plugin> </plugins> <resources> <resource> <directory>src/main/java</directory> <includes> <include>**/*.properties</include> <include>**/*.xml</include> </includes> <filtering>false</filtering> </resource> <resource> <directory>src/main/resources</directory> <includes> <include>**/*.properties</include> <include>**/*.xml</include> </includes> <filtering>false</filtering> </resource> </resources> </build> </project> web.xml <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0"> <display-name>helloworld</display-name> <!-- 配置編碼方式--> <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> <filter-mapping> <filter-name>encodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <filter-mapping> <filter-name>encodingFilter</filter-name> <url-pattern>*.html</url-pattern> </filter-mapping> <!-- 配置springmvc的前端控制器 指向spring-mvc.xml 程序在啟動的時候就加載springmvc 可以接受所有請求 load-on-startup:表示啟動容器時初始化該Servlet; --> <servlet> <servlet-name>springServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!-- 可以自定義servlet.xml配置文件的位置和名稱, 默認為WEB-INF目錄下,名稱為[<servlet-name>]-servlet.xml,如spring-servlet.xml --> <init-param> <param-name>contextConfigLocation</param-name> <param-value> classpath:/applicationContext.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <!-- 將前端URL請求和后臺處理方法controller建立對應關系--> <servlet-mapping> <servlet-name>springServlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <!-- Spring配置 --> <listener> <listener-class> org.springframework.web.context.ContextLoaderListener </listener-class> </listener> <!-- 取消對某一類文件的攔截--> <servlet-mapping> <servlet-name>default</servlet-name> <url-pattern>*.md</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>default</servlet-name> <url-pattern>*.html</url-pattern> </servlet-mapping> <context-param> <param-name>contextConfigLocation</param-name> <param-value> classpath:/applicationContext.xml</param-value> </context-param> <!-- 歡迎頁面--> <welcome-file-list> <welcome-file>/index.html</welcome-file> </welcome-file-list> <!--404錯誤展示頁面,可自行配置--> <!--<error-page>--> <!--<error-code>404</error-code>--> <!--<location>/WEB-INF/views/404.jsp</location>--> <!--</error-page>--> <!--設置session失效時間為30分鐘 --> <session-config> <session-timeout>600</session-timeout> </session-config> </web-app> springmvc的設置 applicationContext.xml: <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:beans="http://www.springframework.org/schema/beans" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd" > <!-- ①:對com.springmvc包中的所有類進行掃描,以完成Bean創建和自動依賴注入的功能 --> <context:component-scan base-package="com.darksouls"/> <mvc:annotation-driven /> <!-- 靜態資源訪問 --> <!--如果webapp下你新建了文件夾,想訪問里面的靜態資源,那么就要在這配置一下--> <mvc:resources location="/images/" mapping="/images/**"/> <mvc:resources location="/css/" mapping="/css/**"/> <mvc:resources location="/js/" mapping="/js/**"/> <mvc:resources location="/WEB-INF/view/" mapping="/**.html" /> <!-- Configures the @Controller programming model <mvc:annotation-driven />--> <!-- ②:啟動Spring MVC的注解功能,完成請求和注解POJO的映射 --> <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"> <property name="messageConverters"> <list> <ref bean="mappingJacksonHttpMessageConverter"/> </list> </property> </bean> <bean id="mappingJacksonHttpMessageConverter" class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"> <property name="supportedMediaTypes"> <list> <value>application/json;charset=UTF-8</value> </list> </property> </bean> <!-- 配置視圖解析器,把控制器的邏輯視頻映射為真正的視圖 --> <!-- /WEB-INF/jsp/start.jsp --> <!--<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">--> <!--<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>--> <!--<property name="prefix" value="/WEB-INF/views/" />--> <!--<property name="suffix" value=".jsp" />--> <!--</bean>--> <bean id="freemarkerConfig" class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer"> <property name="templateLoaderPath"> <value>/WEB-INF/view</value> </property> <property name="freemarkerSettings"> <props> <prop key="locale">zh_CN</prop> <prop key="defaultEncoding">UTF-8</prop> </props> </property> </bean> <bean id="htmlViewResolver" class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver"> <property name="contentType" value="text/html;charset=UTF-8"></property> <property name="suffix" value=".html"></property> <property name="order" value="0"></property> </bean> <!-- 配置dbcp數據庫連接池 --> <!-- <context:property-placeholder location="classpath:db.properties"/> --> <!--數據庫配置 --> <bean id = "propertyConfigurer" class = "org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <list> <value>classpath:jdbc.properties</value> </list> </property> </bean> <!-- 數據庫連接池 --> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="${driver}"/> <property name="url" value="${url}"/> <property name="username" value="${username}"/> <property name="password" value="${password}"/> <property name="initialSize" value="1"/> <property name="maxActive" value="100"/> <property name="maxIdle" value="5"/> <property name="maxWait" value="80000"/> </bean> <!-- 配置事務管理器 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource" /> </bean> <!-- 攔截器 --> <mvc:interceptors> <!-- 國際化操作攔截器 如果采用基于(請求/Session/Cookie)則必需配置 --> <bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor" /> </mvc:interceptors> <!-- 定義無Controller的path<->view直接映射 --> <!-- <mvc:view-controller path="/" view-name="redirect:/" /> --> <!-- 配置會話工廠SqlSessionFactory --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <!-- 數據源 --> <property name="dataSource" ref="dataSource"/> <property name="mapperLocations" value="classpath:sqlmap/*.xml"/> <property name="typeAliasesPackage" value="com.darksouls.entity" /> </bean> <!-- 在spring容器中配置mapper的掃描器產生的動態代理對象在spring的容器中自動注冊,bean的id就是mapper類名(首字母小寫)--> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <!-- 指定掃描包的路徑,就是mapper接口的路徑,多個包中間以 半角逗號隔開 --> <property name="basePackage" value="com.darksouls.dao"/> <!-- 配置sqlSessionFactoryBeanName --> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/> </bean> <!--文件解析器--> <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <property name="maxUploadSize"> <value>5242880</value> </property> </bean> </beans>
轉載自://www.cnblogs.com/lzy321/p/11487770.html
国产无遮挡无码视频在线观看