SpringMVC本身支持多个视图的配置,只需添加视图解析器(ViewResolver)即可。原来的配置如下:
<!--jsp视图解析器--> <bean id="jspViewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver"> <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/> <property name="prefix" value="/WEB-INF/jsp/"/> <property name="suffix" value=".jsp"/> </bean>
增加一个velocity视图解析器,同时要注意order参数的配置(解析器的调用顺序)。
<!--jsp视图解析器--> <bean id="jspViewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver"> <property name="order" value="1"/> <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/> <property name="prefix" value="/WEB-INF/jsp/"/> <property name="suffix" value=".jsp"/> </bean> <!-- velocity环境配置 --> <bean id="velocityConfig" class="org.springframework.web.servlet.view.velocity.VelocityConfigurer"> <!-- velocity配置文件路径 或者直接用velocityProperties属性 --> <property name="configLocation" value="classpath:velocity.properties"/> <!-- <property name="velocityProperties"> <props> <prop key="input.encoding">utf-8</prop> <prop key="output.encoding">utf-8</prop> </props> </property> --> <!-- velocity模板路径 --> <property name="resourceLoaderPath" value="/WEB-INF/templates/"/> </bean> <!-- velocity视图解析器 --> <bean id="velocityViewResolver" class="org.springframework.web.servlet.view.velocity.VelocityLayoutViewResolver"> <property name="order" value="0"/> <property name="contentType" value="text/html;charset=UTF-8"/> <property name="cache" value="true"/> <property name="prefix" value="/"/><!-- 因为velocityConfig已经指定模板路径! 所以该行可注释掉--> <property name="suffix" value=".vm"/> <property name="layoutUrl" value="layout/layout.vm"/> <property name="exposeSpringMacroHelpers" value="true" /><!--是否使用spring对宏定义的支持--> <property name="exposeSessionAttributes" value="true" /><!--是否开放request属性--> <property name="requestContextAttribute" value="request"/><!--request属性引用名称--> <property name="dateToolAttribute" value="dateTool"/> <property name="numberToolAttribute" value="numberTool"/> </bean>
velocity配置文件:velocity.properties
#设置字符集 #encoding input.encoding =UTF-8 output.encoding=UTF-8 contentType=text/html;charset=UTF-8 #autoreload when vm changed file.resource.loader.cache=false file.resource.loader.modificationCheckInterval =1 velocimacro.library.autoreload=false
利用 layout.vm 可以将大量公共的页面框架写成一个独立的文件,让最终的视图文件去引用。即可以大量简化代码,又可以让程序员专注与当前的页面逻辑。
模板文件--layout.vm:
<html> <head> <title>$!page_title</title> #parse("default/header.vm") </head> <body class="bgclf3f3f3"> <div> <!-- View abc.vm is inserted here --> <!-- Noi dung View abc.vm se duoc tren tai day --> $screen_content </div> </body> </html>公有头文件--header.vm:
<meta charset="utf-8"> <meta name="viewport" content="width=device-width,initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no"/> <meta content="yes" name="apple-mobile-web-app-capable"/> <meta content="black" name="apple-mobile-web-app-status-bar-style"/> <meta content="telephone=no" name="format-detection"/> <link rel="icon" href="/static/img/favicon.ico"> <link href="/static/lib/bootstrap/css/bootstrap.min.css" rel="stylesheet"> <!-- Jquery and Bootstap core js files --> <script type="text/javascript" src="/static/lib/jquery/jquery-1.12.3.js"></script> <script type="text/javascript" src="/static/lib/bootstrap/js/bootstrap.js"></script>
实际业务视图--error.vm:
#set( $page_title = "错误信息") #set($ex=$request.getAttribute("ex")) #set($err="Err:") #if($ex.message.startsWith("Err:")) #set($message = $ex.message.substring($err.length(), $ex.message.length())) #end <section class="wd640"> <!-- 信息反馈部分 --> <div class="content"> <h3 class="title">信息反馈如下</h3> <p class="txt"><strong>错误代码:</strong> #if($message) ${message} #else $ex.message #end</p> <p class="txt"><strong>错误描述:</strong> #if($message) #if ($ex.message == "Err:0000") 非法请求 #elseif ($ex.message == "Err:0001") 连接超时 #end #else $ex.message #end </p> </div> </section>
velocity和javamail的整合
javamail可以使用velocity作为模板来发送模板邮件
可参考:http://gundumw100.iteye.com/blog/515346