Spring下velocity页面中文乱码问题

一、单一映射的解决方案。

     1.  便于管理,将.vm文件格式设置为UTF-8编码;

     2.  在applicationContext.xml中添加velocity引擎

<bean id="velocityConfigurer" class="org.springframework.web.servlet.view.velocity.VelocityConfigurer">
    <property name="resourceLoaderPath" value="/"></property>
    <property name="velocityProperties">  
        <props>  
            <prop  key="input.encoding">UTF-8</prop>  
            <prop  key="output.encoding">UTF-8</prop>    
         </props>  
     </property>                  
  </bean>

      3.添加视图解析器

 <bean id="velocityViewResolver" class="org.springframework.web.servlet.view.velocity.VelocityViewResolver">
   <property name="suffix"><value>.vm</value></property>
   <property name="contentType"><value>text/html;charset=UTF-8</value></property>
 </bean>

 

  按上述配置后,则显示中文正常。不过具体实现是有几点需要注意:

  a.

<property name="resourceLoaderPath" value="/"></property>

   由于设置引擎的路径是/,所以具体的view是不能再以/开头,如WEB-INF/x,而不能为/WEB-INF/x

  b.

 <property name="suffix"><value>.vm</value></property>

   后缀名已经申明为.vm,所以view不用再申明.vm后缀,即WEB-INF/x,而不能为WEB-INF/x.vm,不然会被用于查找WEB-INF/x.vm.vm.

 

二、多映射的解决方案

   利用VelocityViewResolver,使得视图与url只能进行固定的单一映射,若希望使用ResourceBunlderViewResovler则又会出现中文乱码,解决这一问题则需要使用过滤器Filter。

  1.  编写过滤器的代码

public class CharacterEncodeFilter implements Filter {

	private String characterSetName = null;

	private boolean enableEncoding = false;

	/**
	 * @see javax.servlet.Filter#destroy()
	 */
	public void destroy() {

	}

	/**
	 * @see javax.servlet.Filter#doFilter(javax.servlet.ServletRequest,
	 *      javax.servlet.ServletResponse, javax.servlet.FilterChain)
	 */
	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain chain) throws IOException, ServletException {

		System.out.println(this.enableEncoding);
		System.out.println(this.characterSetName);

		if (this.enableEncoding)
		{
			//request.setCharacterEncoding(characterSetName);
			response.setContentType("text/html;charset=" + characterSetName);
		}

		chain.doFilter(request, response);

	}

	/**
	 * @see javax.servlet.Filter#init(javax.servlet.FilterConfig)
	 */
	public void init(FilterConfig config) throws ServletException {
		loadConfigurer(config);

	}

	/**
	 * 载入配置信息
	 * 
	 * @param config
	 */
	private void loadConfigurer(FilterConfig config) {
		characterSetName = config.getInitParameter("characterSetName");
		String enable = config.getInitParameter("enableEncoding");

		if (enable != null && enable.equalsIgnoreCase("true"))
		{
			this.enableEncoding = true;
		} else
		{
			this.enableEncoding = false;
		}

	}

}

 2.在web应用的web.xml文件中添加过滤器设置

 <filter>
   <filter-name>characterFilter</filter-name>
   <filter-class>filters.CharacterEncodeFilter</filter-class>
   <init-param>
     <param-name>characterSetName</param-name>
     <param-value>UTF-8</param-value>
   </init-param>
   <init-param>
    <param-name>enableEncoding</param-name>
    <param-value>true</param-value>
   </init-param>
  </filter>
  
  <filter-mapping>
   <filter-name>characterFilter</filter-name>
   <url-pattern>*.html</url-pattern>
  </filter-mapping>
  

   上述过滤器当后缀名为html时才生效。其实这种利用过滤器的方法对于任何页面的乱码都同样有效,注意,当你需要过滤器其他文件访问时,请设置<filter-mapping>下的<url-pattern>为相应的文件即可。

 三、使用spring内置过滤器

      其实,spring自己提供一个编码过滤器,因而只需要在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>

 

 

 

 

电流模架构Bandgap设计与仿真 带隙基准作为集成电路中一个重要模块,被广泛应用在低压差线性稳压(LDO)、充电电池保护芯片和通信电路、射频收发器、flash存储器等多种模拟及数模混合集成电路中,并且是片上集成系统(SOC)芯片中可或缺的部分,为整个芯片提供精确的电压参考点............ 阅读详情

相关推荐

23. STM32F103C8T6 ADC采集光敏电阻传感器模块光照强度实战

本文详细介绍了如何使用STM32F103C8T6的ADC模块采集光敏电阻传感器的光照强度。通过硬件接线、核心原理讲解、四步代码配置(GPIO、ADC模式、使能校准、数据滤波)以及串口验证,手把手指导完成从模拟信号到数字值的完整实战流程,并重点提示了ADC时钟分频等关键配置要点。

weixin_42300144的博客 189

velocity中文乱码最终解决方案

                            velocity中文乱码最终解决方案                    (转载请注明是 kojh原著)           前言:最近在用velocity开发个东西,但其vm页面的输出总是会乱码,在网上找了很多资料,还是能解决,最终在一篇网上的文章的启发下,http://www.javaeye.com/post/540300,终于搞

kojhliang的专栏 2577

velocity中faile to load source 和中文乱码问题

新手接触velocity时遇到的两个简单问题的解决办法

velocity demo 解决中文显示问题

项目结构如截图所示: shou

Molly的专栏 1920

Springframwork中集成Velocity中文解决方案

Springframework中使用Velocity是非常方便的,只需在spring配置文件中申明:        即可在spring mvc框架中直接返回new ModelAndView("velocity模板", map),但是中文一直为乱码。为了解决中文问题,首先,考虑到国际化,将所有web页面都用UTF-8编码,然后在/WEB-INF/velocity.properties文

如来 930

Java使用Velocity模板发送HTML格式邮件并解决中文乱码问题

项目中经常会遇到发送邮件的问题,有时还需要发送html格式的邮件,如果直接在java文件硬编码写html,那么维护起来将会非常麻烦,利用Velocity模板引擎可以解决这个问题,使得业务处理和视图渲染相分离,方便维护。项目依赖:<properties> <springframework>4.2.6.RELEASE</springframework> </properties><depende

wang704987562的博客 4154

Spring velocity 中文乱码 解决方案

Spring velocity 中文乱码 解决方案

p15097962069的博客 166

Spring MVC与Veclocity结合中文问题及常用中文问题总结

文章出自:http://xiecc.itpub.net/post/1476/34635昨天在整合Spring MVC和Velocity,Sitemesh时,又碰到了久违的中文问题。唉,JSP, mysql, struts,每次都会碰到这样的问题,总是以为这种以后会碰到这种看似初级的问题了,结果还是躲过。网上没查到相关资料,于是开始动手跟踪SpringVelocity的源码,弄了一天终于搞

HouYing 1002

Springframwork集成Velocity的解决方案

Springframework中使用Velocity是非常方便的,只需在spring配置文件中申明:          即可在spring mvc框架中直接返回new ModelAndView("velocity模板", map),但是中文一直为乱码。    为了解决中文问题,首先,考虑到国际化,将所有web页面都用UTF-8编码,然后在/WEB-INF/velocity.properties文

f9inux的专栏 1530

Spring MVC与Veclocity结合中文问题及常用中文问题总结(转)

昨天在整合Spring MVC和Velocity,Sitemesh时,又碰到了久违的中文问题。唉,JSP, mysql, struts,每次都会碰到这样的问题,总是以为这种以后会碰到这种看似初级的问题了,结果还是躲过。网上没查到相关资料,于是开始动手跟踪SpringVelocity的源码,弄了一天终于搞定。后来一个同学告诉我这个问题Spring中文论坛里有精华贴,跟我最后的解决方案一样的,...

从小白做起。。。 126

Spring学习(三)——集成 Velocity

上篇文章http://www.cnblogs.com/wenjingu/p/3822989.html我们使用Gradle构建了一个简单的Spring MVC Web应用程序, 本篇将在上篇的基础上将jsp页面改为velocity模板,并集成到Spring MVC框架中。使用Velocity开发视图的好处是在团队开发中,将 java代码从web页面中分离出来,使得页面开发人员和业务逻辑开发人员的...

weixin_30820151的博客 71

spring + velocity + sitemesh2 乱码问题

以前都是用spring + jsp + sitemesh2,近来想学习下velocity

码农自留地 767

Spring mvc 实例--使用velocity视图,解决中文乱码问题

    上个星期读了一些spring mvc源码,现在可以将之前的写的简单的例子贴出来,为以后的开发作些准备。 这是一个非常简单的例子,就是使用SimpleFormController实现spring mvc的例子。首先,在web.xml上配置DispatcherServlet。  winking org.springframework.web.servlet.Di

nick许赟钦的专栏 6580

使用spring发送Mail + Velocity 模板 - 中文乱码解决

 1- 出现model中的中文信息显示正常,而模板中的静态中文信息显示乱码       我的模板是用html进行编写的,而且html模板是UTF-8的,使用如下代码会出现乱码。             String text = VelocityEngineUtils.mergeTemplateIntoString(                       velocityEn

spring_21cn的专栏 2491

spring下使用velocity中文乱码问题

1 为了简单,所有文档都用utf-8编码。2  在xxxx-servlet.xml中配置使用velocity的view的返回编码为utf-8    .vm        text/html;charset=UTF-8    3 在xxx-servlet.xml中配置velocity引擎使用的编码                    /pc/vm/               

yethyeth的专栏 2613

Spring boot 1.2.5.RELEASE 使用velocity模板中文乱码问题

application.properties文件: spring.velocity.resourceLoaderPath=classpath:/templates/ spring.velocity.prefix= spring.velocity.suffix=.vm spring.ve...

chensong7962的博客 428

Velocity中文乱码问题解决方法

Velocity中文乱码问题需要注意一下几点: 1、eclipse默认编码方式 2、页面的编码方式 3、volocity模板的编码方式   第一步: 选择工程右键-&gt;Properties-&gt;Resource  查看默认的编码方式 第二步: &lt;%@ page language="java" import="java.util.*" pageEncoding="g...

gaoyong3013的专栏 915

Spring3 MVC 集成Velocity中文支持

Maven中加上Velocity的依赖如下: <!-- Velocity --> <dependency> <groupId>org.apache.velocity</groupId> <artifactId>v...

chuifuhuo6864的博客 76

spring mvc velocity中文问题

昨天开始用spring MVC的velocity做前台页面开发,写了简单例子,在velocity的模板里写了中文字符,结果出现乱码,从java后台出入的中文没有。 未解决之前的代码:如下 <beanid="viewResolver"class="com.talkweb.framework.web.springmvc.view.Mult...

weixin_34310127的博客 107

spring mvc使用Maven配置Velocity

Spring mvc中视图解析器组件默认为Jsp,这仅仅是其中一种视图解析器。除了使用Jsp作为视图解析器之外,我们还可以使用Velocity、FreeMarker作为视图解析器,该文章主要针对Spring mvc配置Velocity作为spring mvc的视图解析器,至于配置Freemarker与配置Velocity类似,Volocity的定位和Jsp一样。

Jde冻结 427

DASSIDirect2.0 Intouch连接驱动.zip

软件介绍:Intouch与西门子S7-300/400设备连接时需要用到的驱动资源由如下构成:BinDocsGlobalAssemblyCacheRedistUserDocsWindowsAutorun.infInstall-DASSIDirect.chmlicense.rtfPreRequisites.exePrerequisites.iniPrerequisitesDll.dllReadMe.htmlSetup.exeSetup.iniSetup.msiSetup-1033.dll

上一篇: 【转】Spring包及其依赖关系
下一篇: google加速载入js代码
JackAndroid
博客等级 码龄17年 3粉丝 12原创
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值