1. 程式人生 > >Spring MVC 整合 Velocity

Spring MVC 整合 Velocity

當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檔案