配置过程中走了一些弯路 ... 将代码留下,备案.
velocityTool summary (有详细的toolsapi):
http://velocity.apache.org/tools/releases/2.0/summary.html
dispatcher-servlet.xml 配置如下:
<bean id="velocityConfig" class="org.springframework.web.servlet.view.velocity.VelocityConfigurer">
<property name="resourceLoaderPath" value="/web/default/velocity/"/>
<property name="velocityProperties">
<props>
<prop key="directive.foreach.counter.name">loopCounter</prop>
<prop key="directive.foreach.counter.initial.value">0</prop>
<prop key="input.encoding">UTF-8</prop>
<prop key="output.encoding">UTF-8</prop>
<prop key="directive.foreach.counter.name">velocityCount</prop>
<prop key="directive.foreach.counter.initial.value">1</prop>
<prop key="velocimacro.library.autoreload">true</prop>
</props>
</property>
</bean>
<!-- spring 视图解析器配置,这里是我自己写的解析器 -->
<bean id="viewResolver" class="spring.tool.MultipleViewResolver">
<property name="resolvers">
<map>
<entry key="jsp">
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/web/default/screen/"/>
<property name="suffix" value=".jsp"/>
</bean>
</entry>
<entry key="vm">
<bean class="org.springframework.web.servlet.view.velocity.VelocityViewResolver">
<property name="exposeRequestAttributes" value="true" />
<property name="contentType" value="text/html;charset=UTF-8" />
<property name="prefix" value=""/>
<property name="suffix" value=".vm"/>
</bean>
</entry>
</map>
</property>
</bean>
改造后的viewresolver,可以支持多种模板渲染,根据文件后缀区分
package spring.tool;
import java.util.Locale;
import java.util.Map;
import org.springframework.web.servlet.View;
import org.springframework.web.servlet.ViewResolver;
/**
* @author hp
*
*/
public class MultipleViewResolver implements ViewResolver {
private Map<String, ViewResolver> resolvers;
public void setResolvers(Map<String, ViewResolver> resolvers)
{
this.resolvers = resolvers;
}
/* (non-Javadoc)
* @see org.springframework.web.servlet.ViewResolver#resolveViewName(java.lang.String, java.util.Locale)
*/
@Override
public View resolveViewName(String viewName, Locale locale) throws Exception {
int n = viewName.lastIndexOf(".");
if(n==(-1)) return null;
String suffix = viewName.substring(n+1);
ViewResolver resolver = resolvers.get(suffix);
if(resolver!=null)
return resolver.resolveViewName(viewName.substring(0, n), locale);
return null;
}
}
web.xml配置
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath*:/applicationContext.xml
</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<servlet>
<servlet-name>springdispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springdispatcher</servlet-name>
<url-pattern>*.htm</url-pattern>
</servlet-mapping>
exposeRequestAttributes:默认值false,设置是否所有的request属性在与模板进行合并之前添加到model中。(可以理解为request范围内包含的所有对象,而不是一个真正的Request对象。)
exposeSessionAttributes:默认值false,设置是否所有的session属性在与模板进行合并之前添加到model中。
本文详细介绍了如何在Spring MVC框架中配置Velocity模板引擎,包括VelocityConfigurer的使用、配置文件解析器、web.xml配置以及exposeRequestAttributes的含义。
993

被折叠的 条评论
为什么被折叠?



