首页 > 其他分享 >Spring整合velocity

Spring整合velocity

时间:2023-03-20 20:01:49浏览次数:40  
标签:index VelocityEngine Spring javamail 整合 velocity org VelocityConfigurer


版本要1.4 , 1.6版本出现问题:Failed to load Directive: org.apache.velocity.tools.generic.directive.Ifnull, 不知道怎么解决.


[url]http://yixiandave.iteye.com/blog/1880371[/url]
五、velocity和spring mvc的整合
本篇资料五花八门,但是大多是spring2.5的资料了
配置文件不用多说,首先是velocity的配置信息

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


设定细节


<bean id="velocityConfigurer" class="org.springframework.web.servlet.view.velocity.VelocityConfigurer"> 
<property name="resourceLoaderPath">
<value>WEB-INF/velocity/</value>
</property>
<property name="velocityProperties">
<props>
<prop key="directive.foreach.counter.name">loopCounter</prop>
<prop key="directive.foreach.counter.initial.value">0</prop>
</props>
</property>
</bean>



然后引入ViewResolver


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




要补充的是Spring3 MVC中的Controller大多都是普通类了,我也不喜欢在一个映射方法中添加一堆莫名其妙的参数


实际上Spring支持直接的字符串解析:


@Controller  
@RequestMapping("/")
public class IndexController {
@RequestMapping("/index.htm")
public String showIndex(){
return "index";
}
}



这里index会直接映射到WEB-INF/velocity/index.vm文件。


注意return字符串中不需要添加.vm字符串



###################补充2013-06-02#######################


也可以直接将配置信息写入properties文件


在VelocityConfigurer类的bean中添加


<property name="configLocation" value="classpath:velocity.properties"/>



#########################################################


-----------------------------------------------------------


六、velocity和javamail的整合


javamail可以使用velocity作为模板来发送模板邮件


具体在这里已经很清楚了:http://gundumw100.iteye.com/blog/515346


博主在这里要补充的是里面引用的是org.springframework.ui.velocity.VelocityEngineFactoryBean来创建一个VelocityEngine对象。


但是velocity和spring mvc的整合则使用了org.springframework.web.servlet.view.velocity.VelocityConfigurer


我们没必要为了javamail还单独配置一个VelocityEngine方法。


通过阅读源码发现org.springframework.web.servlet.view.velocity.VelocityConfigurer实际有一个getVelocityEngine()方法。


因此我们可以直接把Confirgurer注入到javamail发送类


private VelocityEngine velocityEngine;  
@Resource(name = "velocityConfigurer")
private void setVelocityConfigurer(VelocityConfigurer velocityConfigurer){
velocityEngine = velocityConfigurer.getVelocityEngine();
}



这个和前面的方法是等效的


标签:index,VelocityEngine,Spring,javamail,整合,velocity,org,VelocityConfigurer
From: https://blog.51cto.com/u_3871599/6138555

相关文章

  • springmvc中获得HttpServletRequest request方法
    在web。xml中配置一个监听<listener><listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>......
  • Spring容器管理的配置Bean转换对象为json字符串时StackOverflowError问题
    背景项目中某配置类XxxConfig定义了很多配置参数,通过Spring的@Value注解与配置中心的项目yml里的配置项关联。@Slf4j@Getter@Setter@RefreshScope@Configurationpub......
  • SSM整合
    整合流程1、创建工程  pom.xnl文件中导入相应的坐标<packaging>war</packaging><dependencies><dependency><groupId>org.springframewor......
  • Spring MVC3: Controller接受Form数据
    SpringMVC3:Controller接受Form数据<formaction="./saveIntoDatabase.do"method="post"name="saveIntoDatabase"><inputname="name"t......
  • spring-servlet.xml
    WEB-INF目录下面建一个ascweb-servlet.xml文件,其实这个文件的命名就是Web.xml中servlet-name的名字加-servlet.xml.其文件内容如下:<?xmlversion="......
  • Spring容器加载完之后执行特定任务
    有两个服务器类,需要SpringContext在InitAfterSpringContextService之前初始化:1、SpringContextspring容器的上线文环境packagecom.cdelabcare.pubservice;importorg.sp......
  • Spring MVC防重复提交
    如何在SpringMVC里面解决此问题(其它框架也一样,逻辑一样,思想一样,和具体框架没什么关系)。要解决重复提交,有很多办法,比如说在提交完成后redirect一下,也可以用本文提到的使用to......
  • Springboot项目密码加密器jasypt
    最新版依赖<dependency> <groupId>com.github.ulisesbocchio</groupId> <artifactId>jasypt-spring-boot-starter</artifactId> <version>3.0.5</version></dependenc......
  • spring工具类
    spring工具类获取bean编写packagecom.cloudiip.security.utils;importorg.springframework.stereotype.Component;importorg.springframework.context.Applicatio......
  • org.springframework.core.metrics.ApplicationStartup
    日志Exceptioninthread"main"java.lang.NoClassDefFoundError:org/springframework/core/metrics/ApplicationStartup atorg.springframework.boot.SpringApplicat......