当spring mvc 需要集成Velocity 时:application.xml中配置如下:
<!-- 模板信息设置 -->
<bean id="velocityConfigurer"
class="org.springframework.web.servlet.view.velocity.VelocityConfigurer">
<property name="resourceLoaderPath" value="WEB-INF/views" /><!-- 設置模板防止位置-->
<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><!-- 指定输出流的编码 -->
</props>
</property>
</bean>
<!-- 设置视图解析工具 -->
<bean id="viewResolver"
class="org.springframework.web.servlet.view.velocity.VelocityViewResolver">
<property name="suffix" value=".vm" />
<!-- 避免乱码 -->
<property name="contentType" value="text/html;charset=UTF-8" />
<property name="dateToolAttribute" value="dateTool" />
<property name="numberToolAttribute" value="numberTool" />
<property name="exposeRequestAttributes" value="true" />
<property name="exposeSessionAttributes" value="true" />
</bean>
下面是Action类中的一个方法:
/**
* 静态密码登录组件
* @throws Exception
*/
@RequestMapping(value = "/userStaticLogin")
public ModelAndView userStaticLogin(HttpServletRequest request,
HttpServletResponse resp) throws Exception {
String errormessg = request.getParameter("errormessg");
facadeService.getTargetUrl(request,Constants.PORTAL_VERSION_3G);
String ssoRequest = request.getParameter("SAMLRequest");
String ssoRelaystate = request.getParameter("RelayState");
if (StringUtils.isNotBlank(ssoRequest)) {
request.setAttribute("ssoRequest", ssoRequest);
request.setAttribute("ssoRelaystate", ssoRelaystate);
}
request.setAttribute("wap3gPath", configuration.getWap3gPath());
request.setAttribute("serverUrl", configuration.getServerUrl());
request.setAttribute("errormessg", errormessg);
return new ModelAndView(SystemConstants.WAP_3G_VM_PATH+"login/staticlogin");
}
其中上面的
login/staticlogin是WEB-INF/login下面的staticlogin.vm文件