zoukankan      html  css  js  c++  java
  • SpringMVC整合Freemarker(含Demo源码)(转)

    转自:http://blog.csdn.net/sinat_27535209/article/details/61199452

    整合过程如下:

    1.新建一个maven web工程,使用maven依赖spring,springmvc,freemarker的相关jar,建好后的项目目录如下: 
    这里写图片描述

    pom.xml文件中的配置如下:

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
        <groupId>com.ffcs.oss</groupId>
        <artifactId>SpringMVC_Freemarker</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <packaging>war</packaging>
        <properties>
            <org.springframework.version>4.1.6.RELEASE</org.springframework.version>
            <freemarker.version>2.3.23</freemarker.version>
        </properties>
        <dependencies>
            <dependency>
                <groupId>org.freemarker</groupId>
                <artifactId>freemarker</artifactId>
                <version>${freemarker.version}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-core</artifactId>
                <version>${org.springframework.version}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-beans</artifactId>
                <version>${org.springframework.version}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-aop</artifactId>
                <version>${org.springframework.version}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-context</artifactId>
                <version>${org.springframework.version}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-context-support</artifactId>
                <version>${org.springframework.version}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-web</artifactId>
                <version>${org.springframework.version}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-webmvc</artifactId>
                <version>${org.springframework.version}</version>
            </dependency>
        </dependencies>
    </project>
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54

    2.配置web.xml文件

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns="http://java.sun.com/xml/ns/javaee"
        xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
        id="WebApp_ID" version="3.0">
    
        <servlet>
            <servlet-name>springDispatcherServlet</servlet-name>
            <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
            <init-param>
                <param-name>contextConfigLocation</param-name>
                <param-value>classpath:springmvc_freemarker.xml</param-value>
            </init-param>
            <load-on-startup>1</load-on-startup>
        </servlet>
    
        <servlet-mapping>
            <servlet-name>springDispatcherServlet</servlet-name>
            <url-pattern>/*</url-pattern>
        </servlet-mapping>
    </web-app>
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    3.配置springmvc和freemarker整合的配置文件springmvc_freemarker.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
        xmlns:mvc="http://www.springframework.org/schema/mvc"
        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd
            http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd">
    
        <context:component-scan base-package="com.ffcs"></context:component-scan>
        <mvc:annotation-driven></mvc:annotation-driven>
    
        <bean id="freeMarkerConfigurer"
            class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
            <property name="templateLoaderPath" value="/WEB-INF/ftl/"></property>
            <property name="defaultEncoding" value="utf-8" />
            <property name="freemarkerSettings">
                <props>
                    <prop key="template_update_delay">1</prop>
                    <prop key="locale">zh_CN</prop>
                    <prop key="datetime_format">yyyy-MM-dd</prop><!-- 时间格式化 -->
                    <prop key="date_format">yyyy-MM-dd</prop>
                    <prop key="number_format">#.##</prop>
                </props>
            </property>
        </bean>
    
        <bean id="freeMarkerViewResolver"
            class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
            <property name="cache" value="true" />
            <property name="prefix" value="" /><!-- 上面已经配了,这里就不用配啦 -->
            <property name="suffix" value=".ftl" />
            <property name="contentType" value="text/html;charset=UTF-8" />
            <property name="allowSessionOverride" value="true" />
            <property name="allowRequestOverride" value="true" />
            <property name="exposeSpringMacroHelpers" value="true" />
            <property name="exposeRequestAttributes" value="true" />
            <property name="exposeSessionAttributes" value="true" />
            <property name="requestContextAttribute" value="request" />
        </bean>
    </beans>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41

    4.在WEB-INF/ftl/目录下新建一个freemarker的模板文件may.ftl,内容如下:

    User:<br/>
    ${user.name}--->${user.age}<br/>
    List:<br/>
    <#list list as item >
    <font color="red">${item}</br></font>
    </#list>
    param:</br>
    ${RequestParameters.a}&nbsp;a=${param!}
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    5.编写User和TestFreemarker这两个类,代码如下所示: 
    User类:

    package com.ffcs.oss.domain;
    
    import java.io.Serializable;
    
    public class User implements Serializable {
    
        /**
         * 
         */
        private static final long serialVersionUID = 1L;
        private String name;
        private Integer age;
    
        public User() {
            super();
        }
        public User(String name, Integer age) {
            super();
            this.name = name;
            this.age = age;
        }
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        public Integer getAge() {
            return age;
        }
        public void setAge(Integer age) {
            this.age = age;
        }
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36

    TestFreemarker类:

    package com.ffcs.oss.controller;
    
    import java.util.ArrayList;
    import java.util.List;
    
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestParam;
    import org.springframework.web.bind.annotation.ResponseBody;
    import org.springframework.web.servlet.ModelAndView;
    
    import com.ffcs.oss.domain.User;
    
    @Controller
    @RequestMapping(value = "/Freemarker")
    public class TestFreemarker {
        @RequestMapping(value = "/testFreemarker")
        @ResponseBody
        public ModelAndView testFreemarker(@RequestParam(value = "a", required = false) String a) throws Exception {
            ModelAndView mv = new ModelAndView();
    
            User user = new User("may", 21);
    
            List<String> list = new ArrayList<String>();
            list.add("Jack1");
            list.add("Jack2");
            list.add("Jack3");
    
            mv.addObject("list", list);
            mv.addObject("user", user);
            mv.addObject("param", new String(a.getBytes("iso8859-1"), "utf-8"));
            mv.setViewName("may");
            return mv;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36

    6.将项目布署到tomcat运行起来,项目启动如果没有报错,那么打开浏览器,输入URL:http://localhost:8080/SpringMVC_Freemarker/Freemarker/testFreemarker?a=123 ,回车后效果图如下所示: 
    这里写图片描述

    到此为止,已经成功整合了SpringMVC和Freemarker!下面附上Demo源码的下载地址: 
    链接:http://pan.baidu.com/s/1qYxxEUg 
    密码:0azi

  • 相关阅读:
    android-exploitme(六):基础加密
    错误:error libGL.so: cannot open shared object file: No such file or directory
    android-exploitme(五):不安全的数据存储
    android-exploitme(四):参数篡改
    android-exploitme(三):安全连接
    android-exploitme(二):安装apk熟悉测试环境
    android-exploitme(一):生成apk
    Ubuntu rsync同步
    phantomjs + selenium headless test
    Fatal error: cannot allocate memory for the buffer pool
  • 原文地址:https://www.cnblogs.com/weizhxa/p/7824403.html
Copyright © 2011-2022 走看看