• Maven+SpringMVC+Freemarker入门Demo


    1 参考http://blog.csdn.net/haishu_zheng/article/details/51490299,用第二种方法创建一个名为mavenspringmvcfreemarker的Maven工程。

     

    2 文件目录结构如下图所示

    3 在pom.xml中添加springmvc和freemarker的依赖包,添加完之后的完整内容为

     

    [html] view plain copy
     
     在CODE上查看代码片派生到我的代码片
    1. <project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
    2.     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0http://maven.apache.org/xsd/maven-4.0.0.xsd">  
    3.     <modelVersion>4.0.0</modelVersion>  
    4.     <groupId>mavenspringmvcfreemarker</groupId>  
    5.     <artifactId>mavenspringmvcfreemarker</artifactId>  
    6.     <version>0.0.1-SNAPSHOT</version>  
    7.     <packaging>war</packaging>  
    8.     <name>mavenspringmvcfreemarker</name>  
    9.     <description />  
    10.     <properties>  
    11.         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>  
    12.     </properties>  
    13.     <dependencies>  
    14.         <dependency>  
    15.             <groupId>org.freemarker</groupId>  
    16.             <artifactId>freemarker</artifactId>  
    17.             <version>2.3.20</version>  
    18.         </dependency>  
    19.         <dependency>  
    20.             <groupId>org.springframework</groupId>  
    21.             <artifactId>spring-context-support</artifactId>  
    22.             <version>3.2.9.RELEASE</version>  
    23.         </dependency>  
    24.         <dependency>  
    25.             <groupId>org.springframework</groupId>  
    26.             <artifactId>spring-web</artifactId>  
    27.             <version>3.2.9.RELEASE</version>  
    28.         </dependency>  
    29.         <dependency>  
    30.             <groupId>org.springframework</groupId>  
    31.             <artifactId>spring-webmvc</artifactId>  
    32.             <version>3.2.9.RELEASE</version>  
    33.         </dependency>  
    34.         <dependency>  
    35.             <groupId>javax</groupId>  
    36.             <artifactId>javaee-api</artifactId>  
    37.             <version>7.0</version>  
    38.             <scope>provided</scope>  
    39.         </dependency>  
    40.         <dependency>  
    41.             <groupId>org.glassfish.web</groupId>  
    42.             <artifactId>javax.servlet.jsp.jstl</artifactId>  
    43.             <version>1.2.2</version>  
    44.         </dependency>  
    45.     </dependencies>  
    46.     <build>  
    47.         <plugins>  
    48.             <plugin>  
    49.                 <artifactId>maven-compiler-plugin</artifactId>  
    50.                 <version>2.3.2</version>  
    51.                 <configuration>  
    52.                     <source>1.7</source>  
    53.                     <target>1.7</target>  
    54.                 </configuration>  
    55.             </plugin>  
    56.             <plugin>  
    57.                 <artifactId>maven-war-plugin</artifactId>  
    58.                 <version>2.2</version>  
    59.                 <configuration>  
    60.                     <version>3.1</version>  
    61.                     <failOnMissingWebXml>false</failOnMissingWebXml>  
    62.                 </configuration>  
    63.             </plugin>  
    64.         </plugins>  
    65.     </build>  
    66. </project>  

    4 web.xml中的完整内容为

     

    [html] view plain copy
     
     在CODE上查看代码片派生到我的代码片
    1. <?xml version="1.0"encoding="UTF-8"?>  
    2. <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns="http://xmlns.jcp.org/xml/ns/javaee"xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaeehttp://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">  
    3.   <display-name>mavenspringmvcfreemarker</display-name>  
    4.   <servlet>  
    5.     <servlet-name>spring</servlet-name>  
    6.     <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  
    7.     <load-on-startup>1</load-on-startup>  
    8.   </servlet>  
    9.   <servlet-mapping>  
    10.     <servlet-name>spring</servlet-name>  
    11.     <url-pattern>/</url-pattern>  
    12.   </servlet-mapping>  
    13. </web-app>  

    spring-servlet.xml中的完整内容为:

     

    [html] view plain copy
     
     在CODE上查看代码片派生到我的代码片
    1. <beans xmlns="http://www.springframework.org/schema/beans"  
    2.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"  
    3.     xsi:schemaLocation="http://www.springframework.org/schema/beans  
    4.        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
    5.         http://www.springframework.org/schema/context  
    6.        http://www.springframework.org/schema/context/spring-context-3.0.xsd">  
    7.   
    8.     <context:component-scan base-package="com.demo" />  
    9.   
    10.     <bean  
    11.         class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" />  
    12.     <bean  
    13.         class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />  
    14.   
    15.     <!-- 配置Freemarker属性文件路径 -->  
    16.     <bean id="freemarkerConfiguration"  
    17.         class="org.springframework.beans.factory.config.PropertiesFactoryBean">  
    18.         <property name="location" value="classpath:conf/freemarker.properties" />  
    19.     </bean>  
    20.     <!-- 配置freeMarker模板加载地址 -->  
    21.     <bean id="freemarkerConfig"  
    22.         class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">  
    23.         <!-- 视图解析器在/WEB-INF/ftl/路径下扫描视图文件 -->  
    24.         <property name="templateLoaderPath" value="/WEB-INF/ftl/" />  
    25.         <property name="freemarkerVariables">  
    26.             <map>  
    27.                 <entry key="xml_escape" value-ref="fmXmlEscape" />  
    28.             </map>  
    29.         </property>  
    30.     </bean>  
    31.     <bean id="fmXmlEscape" class="freemarker.template.utility.XmlEscape" />  
    32.     <!-- 配置freeMarker视图解析器 -->  
    33.     <bean id="freemakerViewResolver"  
    34.         class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">  
    35.         <property name="viewClass"  
    36.             value="org.springframework.web.servlet.view.freemarker.FreeMarkerView" />  
    37.         <!-- 扫描路径內所有以ftl結尾的文件 -->  
    38.         <property name="viewNames">  
    39.             <array>  
    40.                 <value>*.ftl</value>  
    41.             </array>  
    42.         </property>  
    43.         <property name="contentType" value="text/html; charset=UTF-8" />  
    44.         <property name="exposeRequestAttributes" value="true" />  
    45.         <property name="exposeSessionAttributes" value="true" />  
    46.         <property name="exposeSpringMacroHelpers" value="true" />  
    47.         <property name="requestContextAttribute" value="request" />  
    48.         <!-- 给视图解析器配置优先級,你可以给之前jsp视图解析器的值配为2 -->  
    49.         <property name="order" value="1" />  
    50.     </bean>  
    51.   
    52. </beans>  

    注意:web.xml中,有个<servlet-name>为spring,所以这个文件起名为spring-servlet.xml。这样,程序自动会去加载spring-servlet.xml。也就说<xxx>-servlet.xml中的<xxx>必须与<servlet-name>的值完全对应。

    不对应当然也可以,但是要在web.xml中显示指定加载。

    比如:把spring-servlet.xml改名为applicationContext.xml,则要在web.xml中的</servlet-class>下方加代码:

     

    [html] view plain copy
     
     在CODE上查看代码片派生到我的代码片
    1. <init-param>  
    2.     <param-name>contextConfigLocation</param-name>  
    3.     <param-value>/WEB-INF/applicationContext.xml</param-value>  
    4. </init-param>  

    6 freemarker.properties中的完整内容为

     

    [html] view plain copy
     
     在CODE上查看代码片派生到我的代码片
    1. tag_syntax=auto_detect  
    2. template_update_delay=2  
    3. default_encoding=UTF-8  
    4. output_encoding=UTF-8  
    5. locale=zh_CN  
    6. date_format=yyyy-MM-dd  
    7. time_format=HH:mm:ss  
    8. datetime_format=yyyy-MM-dd HH:mm:ss  


    7 StudentController.java中的完整代码为:

    [java] view plain copy
     
     在CODE上查看代码片派生到我的代码片
    1. package com.demo.controller;  
    2.    
    3. import org.springframework.stereotype.Controller;  
    4. import org.springframework.web.bind.annotation.RequestMapping;  
    5. import org.springframework.ui.Model;  
    6.    
    7. @Controller  
    8. public class StudentController {  
    9.    
    10.     @RequestMapping("/helloWorld")  
    11.     public String helloWorld(Model model) {  
    12.         String word0 = "Hello ";  
    13.         String word1 = "World!";  
    14.         //将数据添加到视图数据容器中  
    15.         model.addAttribute("word0",word0);  
    16.         model.addAttribute("word1",word1);  
    17.         return "Hello.ftl";  
    18.     }  
    19. }  

    8 Hello.ftl中的完整代码为:

     

    [html] view plain copy
     
     在CODE上查看代码片派生到我的代码片
    1. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
    2. <html>  
    3. <head>  
    4. <meta http-equiv="Content-Type"content="text/html; charset=UTF-8">  
    5. <title>Insert title here</title>  
    6. </head>  
    7. <body>  
    8. <h2>${word0}${word1}</h2>  
    9. </body>  
    10. </html>  

    9 将mavenspringmvcfreemarker添加进Tomcat 7中并运行

    在浏览器中输入

    http://localhost:8080/mavenspringmvcfreemarker/helloWorld

    显示结果为

    10 源码下载地址

    CSDN:http://download.csdn.net/detail/haishu_zheng/9533679

    Github:https://github.com/zhenghaishu/Maven-SpringMVC-Freemarker-Demo

  • 相关阅读:
    spring retry注解
    安装mongodb并配置
    spring boot Hello World
    Linux命令echo
    Linux vi命令
    查看linux是ubuntu还是centos
    Linux系统时间, 硬件BIOS时间的校准与同步
    MySQL优化查询 5.7版本
    战略由谁来制定
    VS2015快捷键
  • 原文地址:https://www.cnblogs.com/grimm/p/6732745.html
Copyright © 2020-2023  润新知