标签:make order template erer free meta map ber tin
新项目中准备抛弃jsp而改用html,遇到一些列奇怪的问题,现记录如下:
首先是视图解析问题,在Controller中返回视图时间变得异常慢,一个页面将近20秒,方法很快就执行完毕,就是最后 return viewName 一直卡着,各种检查没问题,最后去查看视图解析配置:
<--org.springframework.web.servlet.view.InternalResourceViewResolver-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="${web.view.prefix}"/> <property name="suffix" value="${web.view.suffix}"/> <property name="contentType"> <value>text/html; charset=UTF-8</value> </property> </bean>
org.springframework.web.servlet.view.InternalResourceViewResolver 是jsp用的解析器,html时要用FreeMaker视图解析
<!-- 定义视图文件解析 html专用--> <bean id="freemarkerConfig" class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer"> <property name="templateLoaderPath"> <value>${web.view.prefix}</value> </property> </bean> <bean id="htmlviewResolver" class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver"> <property name="suffix" value=".html" /> <property name="order" value="0"></property> <property name="contentType" value="text/html;charset=UTF-8"></property> </bean>
改完之后果然速度在飞,搞定。不过页面中文却乱码了,这小事一桩,web应用我们解决乱码都是高手,开始如下之旅:
1.先改html页面:无效
<head> <title>登录</title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
2.web.xml:
<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>
无效 ,甚至病急乱投医,把forceEncoding改为false试一试
3:Controller代码:(网上找的各种方法)
@RequestMapping(value = "${adminPath}/login", produces = "text/html;charset=UTF-8",method = RequestMethod.GET) //@ResponseBody public String login(HttpServletRequest request, HttpServletResponse response, String s) { try { request.setCharacterEncoding("UTF-8"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } response.setContentType("text/html;charset=UTF-8"); HttpServletRequest hRequest = WebUtils.toHttp(request);
4:tomcat 服务器serer.xml,URIEncoding=utf-8,这个对get请求中文参数其实管用。
。。。。。
最终还是没能解决,差点放弃了。在网上百度各种 “SpringMVC html 中文乱码”,最后想,是不是FreeMaker视图解析器的问题呢,换关键词“SpringMVC FreeMaker视图解析 中文乱码”,果然有新方法,如下
<!-- 定义视图文件解析 html专用--> <bean id="freemarkerConfig" class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer"> <property name="templateLoaderPath"> <value>${web.view.prefix}</value> </property> <property name="freemarkerSettings"> <props> <prop key="template_update_delay">1</prop> <prop key="default_encoding">UTF-8</prop> //关键解决点 <prop key="number_format">0.##</prop> <prop key="datetime_format">yyyy-MM-dd HH:mm:ss</prop> </props> </property> </bean> <bean id="htmlviewResolver" class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver"> <property name="suffix" value=".html" /> <property name="order" value="0"></property> <property name="contentType" value="text/html;charset=UTF-8"></property> </bean>
终于解决了,中文乱码真实头疼啊。。。。,哪天编程语言用中文写就,也就就彻底解决了! 百度用了那么多年,你真的会吗?
标签:make order template erer free meta map ber tin
原文地址:https://www.cnblogs.com/KeepYongth/p/9188008.html