需要使用spring mvc將.html中的UI頁面轉換爲.vm(velocity模板)文件。嘗試在其他站點上搜索,但沒有任何不錯的信息。如何使用spring mvc將Html文件轉換爲.vm(velocity模板)文件
任何有用的網站/幫助將不勝感激。 謝謝
需要使用spring mvc將.html中的UI頁面轉換爲.vm(velocity模板)文件。嘗試在其他站點上搜索,但沒有任何不錯的信息。如何使用spring mvc將Html文件轉換爲.vm(velocity模板)文件
任何有用的網站/幫助將不勝感激。 謝謝
首先,您需要清楚的是,當您使用MVC時,您可以以任何您想要的方式提供頁面。這是您的問題的一個可能的解決方案,這是我自己的應用程序的實際代碼。
您可能想要像這樣提供* .html請求。
的web.xml
<servlet>
<servlet-name>appServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>
PATH/TO/YOUR/SERVLET-CONTEXT.xml
</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>appServlet</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>
然後在你的servlet-context.xml中,您可以配置VelocityViewResolver
這樣。
<bean id="viewResolver" class="org.springframework.web.servlet.view.velocity.VelocityViewResolver">
<property name="cache" value="true"/>
<property name="prefix" value="PATH/TO/YOUR/VIEWS/FOLDER"/>
<property name="suffix" value=".vm"/>
</bean>
然後從你的控制器,返回要呈現的視圖的名稱,這不應該包括擴展。
return "template"; //Return the name of view to be found in the views folder.
//The template.vm should be present in your views folder.
這會解決您的問題。
請清楚,在MVC中,視圖根據您的要求的類型返回,文件類型的沒有實際發生轉換。
在你的問題這個html到.vm轉換的東西似乎有點誤導,無論它可能是 - .vm(速度模板),.jsp,.ftl(freemarker模板)等,當呈現在前面結束它只是HTML。
我相信,你想知道如何配置彈簧mvc使用速度作爲你的視圖層。
爲此,您需要將視圖解析器配置爲VelocityViewResolver。
轉到您的'mvc-dispatcher-servlet.xml'或'-servlet.xml',它默認包含用於jsp文件的spring的InternalResourceViewResolver配置。 您需要添加這樣的速度具體配置:
<bean id="viewResolver" class="org.springframework.web.servlet.view.velocity.VelocityViewResolver">
<property name="cache" value="true"/>
<property name="prefix" value=""/>
<property name="suffix" value=".vm"/>
</bean>
這link可能是很好的幫助。
你究竟想要做什麼?如果它只是一個普通的.html文件,說「index.html」,你可以將它保存爲「index.vm」,就完成了。 –
我有html文件,我需要使用spring mvc.Not bale來轉換/配置速度模板,以找出速度模板中tool.xml的用法。 – ajay