velocityidea 支持 velocity中文标签吗

项目结构如截图所示:
1、首先,导入velocity编程所需的jar包,建好各类文件。所需jar包所下:
2、配置web.xml文件
&!-- 配置velocity的servlet --&
&servlet-name&velocity&/servlet-name&
&servlet-class&org.apache.velocity.tools.view.VelocityViewServlet&/servlet-class&
&/servlet&
&servlet-mapping&
&servlet-name&velocity&/servlet-name&
&url-pattern&*.vm&/url-pattern&
&/servlet-mapping&
&servlet-name&hello&/servlet-name&
&servlet-class&velocityHandler.HelloHandler&/servlet-class&
&/servlet&
&servlet-mapping&
&servlet-name&hello&/servlet-name&
&url-pattern&/index&/url-pattern&
&!-- 供浏览器访问 --&
&/servlet-mapping&
HelloHandler.java具体代码如下:
public class HelloHandler extends VelocityViewServlet{
private static final long serialVersionUID = 1L;
private VelocityE
public void init() throws ServletException{
//velocity引擎对象
velo = new VelocityEngine();
//设置vm模板的装载路径
Properties prop = new Properties();
String path = this.getServletContext().getRealPath(&/&);
prop.setProperty(Velocity.FILE_RESOURCE_LOADER_PATH, path + &templates/&);
//设置编码
prop.setProperty(Velocity.ENCODING_DEFAULT, &UTF-8&);
prop.setProperty(Velocity.INPUT_ENCODING, &UTF-8&);
prop.setProperty(Velocity.OUTPUT_ENCODING, &UTF-8&);
//初始化设置,下面用到getTemplate(&*.vm&)输出时一定要调用velo对象去做,即velo.getTemplate(&*.vm&)
velo.init(prop);
} catch (Exception e1) {
e1.printStackTrace();
@SuppressWarnings(&unchecked&)
protected Template handleRequest(HttpServletRequest request,
HttpServletResponse response, Context ctx) {
request.setCharacterEncoding(&UTF-8&);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
response.setCharacterEncoding(&UTF-8&);
String p1 = &lily&;
String p2 = &molly&;
String p3 = &张三疯&;
@SuppressWarnings(&rawtypes&)
Vector personList = new Vector();
personList.addElement(p1);
personList.addElement(p2);
personList.addElement(p3);
ctx.put(&personList&, personList); //将模板数据 list放置到上下文环境context中
Template template = velo.getTemplate(&index.vm&);
}index.vm文件
&meta http-equiv=&Content-Type& content=&text/ charset=UTF-8&&
&title&Sample velocity page&/title&
&body bgcolor=&#ffffff&&
&h2&Hello Velocity&/h2&
&table width=&100& cellpadding=&5& cellspacing=&1& bordercolor=&#333333&&
&tr&&td bgcolor=&#eeeeee& align=&center&&Names&/td&&/tr&
#foreach ($name in $personList)
&tr&&td bgcolor=&#6666FF& align=&center&&$name&/td&&/tr&
3、在浏览器中输入地址http://localhost/velocity/index,即可看到如下效果
注意:vm文件不可以直接被访问,即在浏览器中直接输入地址http://localhost/velocity/templates/index.vm不会看到效果,vm文件需经过某个机制才能被解析,如struts2,spring等等。
本文已收录于以下专栏:
相关文章推荐
http://blog.csdn.net/hotdust/article/details/3876786
(转载请注明是 kojh原著)
前言:最近在用velocity...
VelocityEngine ve = new VelocityEngine();
String path = Post.class.getClassLoader().getRe...
Velocity只是充当一个展示层,和JSP的功能类似,利用mybatis从数据库中取出数据,然后进行数据处理,最后通过Velocity在页面上展示出来。
环境搭建主要分为几个过程,第一步是配置pom...
前言:最近在用velocity开发个东西,但其vm页面的输出总是会乱码,在网上找了很多资料,还是不能解决,最终在一篇网上的文章的启发下,/post/5403...
使用Velocity模板发送邮件的问题
最近做一个监控项目,需要发送邮件预警,选择了使用Spring整合velocity 模板发送邮件,遇到了2个问题:
问题1:中文乱码变成了????,坑啊。
从一个简单demo开始 了解Velocity
README.md 文件里面是有中文的。在 GitHub For Windows 客户端软件里面显示中文是乱码的。解决办法:Step 1 .
将 README.md 文件使用 Notepad++ 软...
他的最新文章
讲师:董岩
您举报文章:
举报原因:
原文地址:
原因补充:
(最多只允许输入30个字)编码问题一直是个很令人头疼的事,这几天搭了一个Spring MVC+VTL的web框架,发现中文乱码了,这里记录一种解决乱码的方案。
开发环境为eclipse,首先,检查Window-&preferences-&workplace-&Text File Encoding,设置为GBK
.vm文件中加入编码约束,举例如下
&!DOCTYPE html&
&meta http-equiv=&Content-Type& content=&text/charset=GBK&&
&title&show param&/title&
&h1&自动绑定的数据&/h1&
Url参数:${urlParam} &/br&
form参数:${formParam} &/br&
form文件:${formFile} &/br&
&h1&手动拉取的数据&/h1&
Url参数${urlParam1} &/br&
form参数${formParam1} &/br&
form文件${formFile1} &/br&
在spring关于velocity的配置文件中加入以下配置:
&bean id=&velocityConfigurer&
class=&org.springframework.web.servlet.view.velocity.VelocityConfigurer&
&property name=&resourceLoaderPath&&
&value&WEB-INF/views/&/value&
&/property&
&property name=&velocityProperties&&
&prop key=&input.encoding&&GBK&/prop&
&prop key=&output.encoding&&GBK&/prop&
&/property&
&bean id=&viewResolver&
class=&org.springframework.web.servlet.view.velocity.VelocityViewResolver&
&property name=&suffix&&
&value&.vm&/value&
&/property&
&property name=&contentType&&
&value&text/charset=GBK&/value&
&/property&
如果eclipse的默认编码设置为utf-8,那么从一开始编辑文件,不管是英文还是中文都是utf-8的,此时按照gbk的类似流程写一遍就好了。
阅读(...) 评论()博客分类:
一、单一映射的解决方案。
便于管理,将.vm文件格式设置为UTF-8编码;
在applicationContext.xml中添加velocity引擎
&bean id="velocityConfigurer" class="org.springframework.web.servlet.view.velocity.VelocityConfigurer"&
&property name="resourceLoaderPath" value="/"&&/property&
&property name="velocityProperties"&
key="input.encoding"&UTF-8&/prop&
key="output.encoding"&UTF-8&/prop&
&/property&
3.添加视图解析器
&bean id="velocityViewResolver" class="org.springframework.web.servlet.view.velocity.VelocityViewResolver"&
&property name="suffix"&&value&.vm&/value&&/property&
&property name="contentType"&&value&text/charset=UTF-8&/value&&/property&
按上述配置后,则显示中文正常。不过具体实现是有几点需要注意:
&property name="resourceLoaderPath" value="/"&&/property&
由于设置引擎的路径是/,所以具体的view是不能再以/开头,如WEB-INF/x,而不能为/WEB-INF/x
&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。
编写过滤器的代码
public class CharacterEncodeFilter implements Filter {
private String characterSetName =
private boolean enableEncoding =
* @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/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 =
this.enableEncoding =
2.在web应用的web.xml文件中添加过滤器设置
&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-mapping&
&filter-name&characterFilter&/filter-name&
&url-pattern&*.html&/url-pattern&
&/filter-mapping&
上述过滤器当后缀名为html时才生效。其实这种利用过滤器的方法对于任何页面的乱码都同样有效,注意,当你需要过滤器其他文件访问时,请设置&filter-mapping&下的&url-pattern&为相应的文件即可。
三、使用spring内置过滤器
其实,spring自己提供一个编码过滤器,因而只需要在web.xml中将其进行设置即可。
&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-mapping&
&filter-name&encodingFilter&/filter-name&
&url-pattern&/*&/url-pattern&
&/filter-mapping&
JackAndroid
浏览: 113225 次
来自: 杭州
感谢你的博文,这破问题解决了!!谢谢
renjie120 写道yangleilt 写道首选对楼主这种 ...
yangleilt 写道首选对楼主这种乐于分享的精神表示感谢, ...
首选对楼主这种乐于分享的精神表示感谢,祝楼主天天有粉木耳。。之 ...
jd2bs 写道同台机器上可能要 McastService和R ...
(window.slotbydup=window.slotbydup || []).push({
id: '4773203',
container: s,
size: '200,200',
display: 'inlay-fix'Velocity增加自定义标签的方法_百度文库
两大类热门资源免费畅读
续费一年阅读会员,立省24元!
Velocity增加自定义标签的方法
&&Velocity增加自定义标签
阅读已结束,下载文档到电脑
想免费下载本文?
定制HR最喜欢的简历
下载文档到电脑,方便使用
还剩3页未读,继续阅读
定制HR最喜欢的简历
你可能喜欢Spring boot 1.2.5.RELEASE 使用velocity模板中文乱码问题
时间: 21:28:40
&&&& 阅读:547
&&&& 评论:
&&&& 收藏:0
标签: application.properties文件:
spring.velocity.resourceLoaderPath=classpath:/templates/ spring.velocity.prefix= spring.velocity.suffix=.vm spring.velocity.cache=false spring.velocity.check-template-location=true spring.velocity.charset=UTF-8 spring.velocity.content-type=text/html
在使用spring boot开发中,虽然设置了编码,但是spring boot的这个版本还是会产生中文乱码,但是使用freemarker模板就没有问题,解决方法就是写一个VelocityAutoConfiguration.java文件放在工程中,覆盖掉spring boot 中的VelocityAutoConfiguration类,包名:
package org.springframework.boot.autoconfigure.velocity;
文件内容从spring boot源码中拷贝出来放在自己的工程,包名和文件名一样,修改的内容只需要在applyProperties添加一行代码:
velocityProperties.setProperty("input.encoding", this.properties.getCharset()); 修改后的applyProperties方法如下:
protected void applyProperties(VelocityEngineFactory factory) {
factory.setResourceLoaderPath(this.properties.getResourceLoaderPath());
factory.setPreferFileSystemAccess(this.properties.isPreferFileSystemAccess());
Properties velocityProperties = new Properties();
velocityProperties.putAll(this.properties.getProperties());
velocityProperties.setProperty("input.encoding", this.properties.getCharset());
factory.setVelocityProperties(velocityProperties);
文件完整内容,保存到工程中就可以:
* Copyright
the original author or authors.
* Licensed 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.
package org.springframework.boot.autoconfigure.
import java.io.IOE
import java.util.P
import javax.annotation.PostC
import javax.servlet.S
import org.apache.velocity.app.VelocityE
import org.apache.velocity.exception.VelocityE
import org.springframework.beans.factory.annotation.A
import org.springframework.boot.autoconfigure.AutoConfigureA
import org.springframework.boot.autoconfigure.EnableAutoC
import org.springframework.boot.autoconfigure.condition.ConditionalOnC
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingB
import org.springframework.boot.autoconfigure.condition.ConditionalOnNotWebA
import org.springframework.boot.autoconfigure.condition.ConditionalOnP
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebA
import org.springframework.boot.autoconfigure.template.TemplateL
import org.springframework.boot.autoconfigure.web.WebMvcAutoC
import org.springframework.boot.context.properties.EnableConfigurationP
import org.springframework.boot.web.servlet.view.velocity.EmbeddedVelocityViewR
import org.springframework.context.ApplicationC
import org.springframework.context.annotation.B
import org.springframework.context.annotation.C
import org.springframework.ui.velocity.VelocityEngineF
import org.springframework.ui.velocity.VelocityEngineFactoryB
import org.springframework.util.A
import org.springframework.web.servlet.view.velocity.VelocityC
import org.springframework.web.servlet.view.velocity.VelocityC
import org.springframework.web.servlet.view.velocity.VelocityViewR
* {@link EnableAutoConfiguration Auto-configuration} for Velocity.
* @author Andy Wilkinson
* @since 1.1.0
@Configuration
@ConditionalOnClass({ VelocityEngine.class, VelocityEngineFactory.class })
@AutoConfigureAfter(WebMvcAutoConfiguration.class)
@EnableConfigurationProperties(VelocityProperties.class)
public class VelocityAutoConfiguration {
@Autowired
private ApplicationContext applicationC
@Autowired
private VelocityP
@PostConstruct
public void checkTemplateLocationExists() {
if (this.properties.isCheckTemplateLocation()) {
TemplateLocation location = new TemplateLocation(
this.properties.getResourceLoaderPath());
Assert.state(location.exists(this.applicationContext),
"Cannot find template location: " + location
+ " (please add some templates, check your Velocity "
+ "configuration, or set spring.velocity."
+ "checkTemplateLocation=false)");
protected static class VelocityConfiguration {
@Autowired
protected VelocityP
protected void applyProperties(VelocityEngineFactory factory) {
factory.setResourceLoaderPath(this.properties.getResourceLoaderPath());
factory.setPreferFileSystemAccess(this.properties.isPreferFileSystemAccess());
Properties velocityProperties = new Properties();
velocityProperties.putAll(this.properties.getProperties());
velocityProperties.setProperty("input.encoding", this.properties.getCharset());
factory.setVelocityProperties(velocityProperties);
@Configuration
@ConditionalOnNotWebApplication
public static class VelocityNonWebConfiguration extends VelocityConfiguration {
@ConditionalOnMissingBean
public VelocityEngineFactoryBean velocityConfiguration() {
VelocityEngineFactoryBean velocityEngineFactoryBean = new VelocityEngineFactoryBean();
applyProperties(velocityEngineFactoryBean);
return velocityEngineFactoryB
@Configuration
@ConditionalOnClass(Servlet.class)
@ConditionalOnWebApplication
public static class VelocityWebConfiguration extends VelocityConfiguration {
@ConditionalOnMissingBean(VelocityConfig.class)
public VelocityConfigurer velocityConfigurer() {
VelocityConfigurer configurer = new VelocityConfigurer();
applyProperties(configurer);
public VelocityEngine velocityEngine(VelocityConfigurer configurer)
throws VelocityException, IOException {
return configurer.getVelocityEngine();
@ConditionalOnMissingBean(name = "velocityViewResolver")
@ConditionalOnProperty(name = "spring.velocity.enabled", matchIfMissing = true)
public VelocityViewResolver velocityViewResolver() {
EmbeddedVelocityViewResolver resolver = new EmbeddedVelocityViewResolver();
this.properties.applyToViewResolver(resolver);
&&国之画&&&& &&&&chrome插件&&
版权所有 京ICP备号-2
迷上了代码!

我要回帖

更多关于 velocity 引入标签 的文章

 

随机推荐