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>