SpringMVC整合jsp與velocity

本來(lái)的項(xiàng)目是純SpringMVC的,沒(méi)辦法,只會(huì)java。后來(lái)覺(jué)得是不是可以用其它東西來(lái)替換jsp,然后發(fā)現(xiàn)velocity的東西。

velocity是一種模板語(yǔ)言,與它類(lèi)似的還有freemark。veocity的優(yōu)點(diǎn)的快,缺點(diǎn)的功能不算特別強(qiáng)大;freemark的功能超級(jí)強(qiáng)大,但速度慢一些。我偏向使用簡(jiǎn)潔快速的velocity,另外velocity的layout功能也是我決定用它來(lái)替換jsp的一個(gè)原因。

整合多視圖

原來(lái)還用著的jsp不可能馬上就干掉,所以需要在SpringMVC里配置多個(gè)視圖解析器(ViewResolver)

好在SpringMVC本身就支持多個(gè)視圖的配置,原來(lái)的配置如下:

<!--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>

現(xiàn)在要增加一個(gè)velocity視圖解析器,增加一個(gè)VelocityLayoutViewResolver的,同時(shí)要注意order參數(shù)的配置。SpringMVC不知道每個(gè)解析器的調(diào)用順序,需要我們自己在配置時(shí)就配置好:

    <!--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="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="/"/>
        <property name="layoutUrl" value="layout/default.vm"/>
        <property name="exposeSpringMacroHelpers" value="true" />
        <property name="dateToolAttribute" value="dateTool"/>
        <property name="numberToolAttribute" value="numberTool"/>
    </bean>

因?yàn)橐嫒菰瓉?lái)的jsp視圖,所以我將jsp配置成了優(yōu)先級(jí)最高。對(duì)于velocity還要增加一些配置,以下是velocity必須要有的配置

    <!-- velocity環(huán)境配置 -->
    <bean id="velocityConfig" class="org.springframework.web.servlet.view.velocity.VelocityConfigurer">
        <!-- velocity配置文件路徑 -->
        <property name="configLocation" value="/WEB-INF/velocity.properties"/>
        <!-- velocity模板路徑 -->
        <property name="resourceLoaderPath" value="/WEB-INF/vm/"/>
    </bean>

以及velocity.properties的配置

#設(shè)置字符集
input.encoding=UTF-8
output.encoding=UTF-8

velocity的layout用法

如果只是簡(jiǎn)單的把jsp文件改成用vm文件來(lái)寫(xiě)就沒(méi)有什么意思了,layout是一個(gè)比較有意思的用法,它可以將大量公共的頁(yè)面框架寫(xiě)成一個(gè)獨(dú)立的文件,讓最終的視圖文件去引用。即可以大量簡(jiǎn)化代碼,又可以讓程序員專(zhuān)注與當(dāng)前的頁(yè)面邏輯。

模板文件如下:

## file=template.vm
<html>
<head>
  <title>$!page_title</title>
</head>
<body>
  $screen_content
</body>
</html>

業(yè)務(wù)文件代碼如下:

#set( $layout = "template.vm" )
#set( $page_title = "this is the test page")
#define($screen_content)
    this is buisin content
#end

看多簡(jiǎn)單!

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容