2012-04-03 70 views
1

我想實現這樣的模板功能:的Spring MVC和速度:模板結構

  • 有定義頁眉,頁腳和完整的(X)HTML網頁的公共部分的模板
  • 當從@Controller返回字符串,將定義包括像這樣的模板

的特定部分的觀點:

@Controller

@RequestMapping(value = "/", method = RequestMethod.GET) 
public String home(Locale locale, Model model) { 
    return "home_view"; 
} 

的意見/ home_view.vm

<h2>Content title</h2> 
<p>Content text</p> 

的意見/ template.vm

<html> 
    <head> 
     <title></title> 
    </head> 
    <body> 
     <!-- Header of page --> 
     #include({context variable which contains "home_view"} + ".vm"); 
     <!-- Footer of page --> 
    </body> 
</html> 

如果有人知道CakePHP的,這類似於其模板系統

我該怎麼做?

+1

可能是一個使用方面的地方。使用'@ RequestMapping'註釋將所有控制器定位到目標位置,然後將返回'template.vm'並將'home_view.vm'放入'Model'中的方法包裝進去,其中'template.vm'可以將其取出並執行包括。否則,也許你可以重寫DispatcherServlet - http://static.springsource.org/spring/docs/3.0.x/javadoc-api/org/springframework/web/servlet/DispatcherServlet.html#resolveViewName%28java.lang.String, %20java.util.Map,%20java.util.Locale,%20javax.servlet.http.HttpServletRequest%29 – 2012-04-03 08:53:30

+0

@PaulGrime,但這意味着每個頁面都必須有單個控制器實例,不是嗎?但我喜歡那個壓倒一切的東西。 – 2012-04-03 08:53:32

+1

如果你想返回除'template.vm'之外的東西,那麼是的。 – 2012-04-03 08:55:13

回答

4

最終解決方案基於@Nathan Bubna建議。

項目基於Spring MVC模板,Maven受控資源。
Spring版本3.1.1.RELEASE

/WEB-INF/web.xml

<?xml version="1.0" encoding="UTF-8"?> 
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> 

    <!-- The definition of the Root Spring Container shared by all Servlets and Filters --> 
    <context-param> 
     <param-name>contextConfigLocation</param-name> 
     <param-value>/WEB-INF/spring/root-context.xml</param-value> 
    </context-param> 

    <!-- Creates the Spring Container shared by all Servlets and Filters --> 
    <listener> 
     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 
    </listener> 

    <!-- Processes application requests --> 
    <servlet> 
     <servlet-name>mainServlet</servlet-name> 
     <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
     <init-param> 
      <param-name>contextConfigLocation</param-name> 
      <param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value> 
     </init-param> 
     <load-on-startup>1</load-on-startup> 
    </servlet> 

    <servlet-mapping> 
     <servlet-name>mainServlet</servlet-name> 
     <url-pattern>/*</url-pattern> 
    </servlet-mapping> 

</web-app> 

/WEB-INF/spring/servlet/servlet-context.xml

<?xml version="1.0" encoding="UTF-8"?> 
<beans:beans xmlns="http://www.springframework.org/schema/mvc" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:beans="http://www.springframework.org/schema/beans" 
    xmlns:context="http://www.springframework.org/schema/context" 
    xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd 
     http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
     http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> 

    <!-- DispatcherServlet Context: defines this servlet's request-processing 
     infrastructure --> 

    <!-- Enables the Spring MVC @Controller programming model --> 
    <annotation-driven /> 

    <!-- Handles HTTP GET requests for /resources/** by efficiently serving 
     up static resources in the ${webappRoot}/resources directory --> 
    <resources mapping="/resources/**" location="/resources/" /> 
    <resources location="/resources/favicon.ico" mapping="/favicon.ico"/> 
    <resources location="/resources/favicon.ico" mapping="/favicon.png"/> 

    <beans:bean id="velocityConfig" 
     class="org.springframework.web.servlet.view.velocity.VelocityConfigurer"> 
     <beans:property name="resourceLoaderPath" value="/WEB-INF/views/" /> 
    </beans:bean> 

    <!-- View resolvers can also be configured with ResourceBundles or XML files. 
     If you need different view resolving based on Locale, you have to use the 
     resource bundle resolver. --> 
    <beans:bean id="viewResolver" 
     class="org.springframework.web.servlet.view.velocity.VelocityLayoutViewResolver"> 
     <beans:property name="cache" value="true" /> 
     <beans:property name="prefix" value="" /> 
     <beans:property name="layoutUrl" value="layout.vm"></beans:property> 
     <beans:property name="suffix" value=".vm" /> 
    </beans:bean> 

    <context:component-scan base-package="com.mypackage.subpackage" /> 

</beans:beans> 

重要的pom.xml行

<dependency> 
    <groupId>org.apache.velocity</groupId> 
    <artifactId>velocity</artifactId> 
    <version>1.7</version> 
</dependency> 
<dependency> 
    <groupId>org.apache.velocity</groupId> 
    <artifactId>velocity-tools</artifactId> 
    <version>2.0</version> 
</dependency> 
+1

你怎麼做的包括?如何獲得佈局頁面上請求的視圖名稱?謝謝 – renanlf 2013-02-28 17:19:38

2

我認爲Spring支持VelocityLayoutView。或者,我認爲他們做到了。

+0

我會感興趣的是爲什麼這個問題downvoted。我現在試圖實現這個解決方案。 – 2012-04-06 08:02:45

+1

我不知道。我沒有倒下任何東西。 – 2012-04-06 16:03:47

+0

你顯然不能;-)我試圖打電話給誰做的;-) – 2012-04-06 17:00:04