zoukankan      html  css  js  c++  java
  • SpringMVC跳转到html页面以及Controller返回html页面的乱码问题

    一、SpringMVC跳转到html页面
      1、自定义视图解析器:
         public class CustomResourceView extends InternalResourceView {
            @Override
            public boolean checkResource(Locale locale) throws Exception {
                 File file=new File(this.getServletContext().getRealPath("/")+getUrl());
                  return file.exists(); //判断页面是否存在
            }
         }
        2、在dispatcherServlet-config.xml文件中配置视图解析器:
                  <bean id="jspViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
              <property name="viewClass" value="ssm.tools.CustomResourceView"/>
              <property name="prefix" value="/WEB-INF/jsp/"/>
              <property name="suffix" value=".jsp"/>
              <property name="order" value="1"/>
            </bean>
            <bean id="htmlViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
               <property name="viewClass" value="ssm.tools.CustomResourceView"/>
               <property name="prefix" value="/WEB-INF/html/"/>
               <property name="suffix" value=".html"/>
                         <property name="order" value="2"/>
            </bean>
     
    二、Controller返回html页面的乱码问题:在web.xml文件中配置字符编码过滤器
                <filter>
            <filter-name>characterEncodingFilter</filter-name>
            <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
            <init-param>
               <param-name>encoding</param-name>
               <param-value>UTF8</param-value>
            </init-param>
            <init-param>
               <param-name>forceEncoding</param-name>
               <param-value>false</param-value>
             </init-param>
           </filter>
           <filter-mapping>
              <filter-name>characterEncodingFilter</filter-name>
              <url-pattern>*.html</url-pattern>
           </filter-mapping>
           <filter-mapping>
              <filter-name>characterEncodingFilter</filter-name>
              <url-pattern>*.jsp</url-pattern>
           </filter-mapping>
  • 相关阅读:
    MySQL++:Liunx
    MySQL++:liunx 安装 MySQL
    MySQL++:MySQL 相关机制参数解释说明
    linux++:Linux
    linux++:基本操作命令
    coding++:java正则表达式获取指定HTML标签的指定属性值
    mongodb++:mongodb 基本语法
    xml模块
    shelve模块简单用法
    shutil模块
  • 原文地址:https://www.cnblogs.com/lone5wolf/p/11534002.html
Copyright © 2011-2022 走看看