基於註解的Spring MVC + freemarker環境搭建

SpringMvc+freemarker確實是MVC不錯的實現。廢話不多說了,下面是自己利用eclipse搭建的一個簡單的springMVC+freemarker

1  首先用IDE建一個web工程。(這個就不詳細介紹了)

2  引入響應的jar包

3  一切web工程的配置都從web.xml開始,現在就看下web.xml都要配置啥
Java代碼  收藏代碼
  1. <?xml version="1.0" ?>  
  2. <web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  3.     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee ;http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"  
  4.     version="3.0">  
  5.     <!--  Spring 服務層的配置文件 -->  
  6.     <context-param>  
  7.         <param-name>contextConfigLocation</param-name>  
  8.         <param-value>classpath:applicationContext.xml</param-value>  
  9.     </context-param>  
  10.       
  11.     <!--  Spring 容器啓動監聽器 -->  
  12.     <listener>  
  13.         <listener-class>org.springframework.web.context.ContextLoaderListener  
  14.         </listener-class>  
  15.     </listener>  
  16.   
  17.     <servlet>  
  18.         <servlet-name>springmvc</servlet-name>  
  19.         <servlet-class>org.springframework.web.servlet.DispatcherServlet  
  20.         </servlet-class>  
  21.         <load-on-startup>1</load-on-startup>  
  22.     </servlet>  
  23.     <!--爲DispatcherServlet建立映射 -->  
  24.     <servlet-mapping>  
  25.         <servlet-name>springmvc</servlet-name>  
  26.         <url-pattern>/</url-pattern>  
  27.     </servlet-mapping>  
  28. </web-app>  


4 SpringMVC另外一個重要的配置文件。
DispatcherServlet會根絕web.xml中配置的<servlet-name>去找<servlet-name>-servlet.xml的文件來加載spring的一些配置信息。我這裏就應該取名叫springmvc-servlet.xml
Java代碼  收藏代碼
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans   
  3.     xmlns="http://www.springframework.org/schema/beans"   
  4.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  5.     xmlns:p="http://www.springframework.org/schema/p"   
  6.     xmlns:context="http://www.springframework.org/schema/context"  
  7.     xsi:schemaLocation="http://www.springframework.org/schema/beans   
  8.     http://www.springframework.org/schema/beans/spring-beans-2.5.xsd  
  9.     http://www.springframework.org/schema/context   
  10.     http://www.springframework.org/schema/context/spring-context-2.5.xsd">  
  11.       
  12.     <!--對web包中的所有類進行掃描,以完成Bean創建和自動依賴注入的功能 -->  
  13.     <context:component-scan base-package="com.liba.spring.mvc"/>  
  14.       
  15.     <!-- 啓動Spring MVC的註解功能,完成請求和註解POJO的映射   請求映射-->  
  16.     <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />  
  17.       
  18.         <!--以下三種視圖配置根據需要任選一種即可 -->  
  19.   
  20.     <!--  一般的視圖配置 -->  
  21.     <!--<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"   
  22.         p:prefix="/WEB-INF/view/" p:suffix=".jsp"/>-->  
  23.       
  24.     <!-- 針對freemarker的視圖配置 -->  
  25.     <bean id="viewResolver"  
  26.         class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">  
  27.         <property name="cache" value="true" />  
  28.         <property name="prefix" value="" />  
  29.         <property name="suffix" value=".ftl" />  
  30.         <property name="contentType" value="text/html;charset=UTF-8"></property>  
  31.         <property name="requestContextAttribute" value="request" />  
  32.         <property name="exposeSpringMacroHelpers" value="true" />  
  33.         <property name="exposeRequestAttributes" value="true" />  
  34.         <property name="exposeSessionAttributes" value="true" />  
  35.     </bean>  
  36.           
  37.           
  38.     <!-- View resolvers can also be configured with ResourceBundles or XML files.   
  39.         If you need different view resolving based on Locale, you have to use the   
  40.         resource bundle resolver. -->  
  41.     <!-- 這個是針對返回視圖還是json值的視圖配置   來分別處理同步和異步請求 -->  
  42.     <!--<bean  
  43.             class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">  
  44.             <property name="mediaTypes">  
  45.                 <map>  
  46.                     <entry key="html" value="text/html" />  
  47.                     <entry key="json" value="application/json" />  
  48.                 </map>  
  49.             </property>  
  50.             <property name="favorParameter" value="true" />  
  51.             <property name="viewResolvers">  
  52.                 <list>  
  53.                     <bean class="org.springframework.web.servlet.view.BeanNameViewResolver" />  
  54.                     <bean id="viewResolver"  
  55.                         class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">  
  56.                         <property name="cache" value="true" />  
  57.                         <property name="prefix" value="" />  
  58.                         <property name="suffix" value=".ftl" />  
  59.                         <property name="contentType" value="text/html;charset=UTF-8"></property>  
  60.                         <property name="requestContextAttribute" value="request" />  
  61.                         <property name="exposeSpringMacroHelpers" value="true" />  
  62.                         <property name="exposeRequestAttributes" value="true" />  
  63.                         <property name="exposeSessionAttributes" value="true" />  
  64.                     </bean>  
  65.                 </list>  
  66.             </property>  
  67.             <property name="defaultContentType" value="text/html" />  
  68.         </bean>  
  69.         -->  
  70. </beans>  


如果是使用freemarker最爲視圖模板需要再spring的配置文件applicationContext.xml中加入以下配置
Java代碼  收藏代碼
  1. <bean id="freemarkerConfig"  
  2.         class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">  
  3.         <property name="templateLoaderPath" value="/WEB-INF/view/" />  
  4.         <property name="freemarkerSettings">  
  5.             <props>  
  6.                 <prop key="template_update_delay">0</prop>  
  7.                 <prop key="default_encoding">UTF-8</prop>  
  8.                 <prop key="number_format">0.##########</prop>  
  9.                 <prop key="datetime_format">yyyy-MM-dd HH:mm:ss</prop>  
  10.                 <prop key="classic_compatible">true</prop>  
  11.                 <prop key="template_exception_handler">ignore</prop>  
  12.             </props>  
  13.         </property>  
  14.     </bean>  



5  Controller建立
Java代碼  收藏代碼
  1. import javax.servlet.http.HttpServletRequest;  
  2.   
  3. import org.springframework.stereotype.Controller;  
  4. import org.springframework.web.bind.annotation.RequestMapping;  
  5. import org.springframework.web.bind.annotation.RequestMethod;  
  6. import org.springframework.web.servlet.ModelAndView;  
  7.   
  8. @Controller  
  9. public class SpringMvcController {  
  10.   
  11.     @RequestMapping(value="/welcome",method={RequestMethod.GET})   
  12.     public ModelAndView getFirstPage(HttpServletRequest request) {  
  13.                 //welcom就是視圖的名稱(welcom.ftl)  
  14.         ModelAndView mv = new ModelAndView("welcom");  
  15.         mv.addObject("name""My First Spring Mvc");  
  16.         return mv;  
  17.     }  
  18. }  


在url上敲http://localhost:8080/welcome就會到WEB-INF/view/welcom.ftl頁面渲染數據

welcom.ftl頁面:
Java代碼  收藏代碼
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
  2. <html>  
  3. <head>  
  4. <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">  
  5. <title>Insert title here</title>  
  6. </head>  
  7. <body>  
  8. Hello ${name}  
  9. </body>  
  10. </html>  




頁面出來的效果:

Hello My First Spring Mvc
發佈了32 篇原創文章 · 獲贊 9 · 訪問量 6萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章