`
osacar
  • 浏览: 211568 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

FreeMarker整合Spring 3

阅读更多
开发环境:

System:Windows

WebBrowser:IE6+、Firefox3+

JavaEE Server:tomcat5.0.2.8、tomcat6

IDE:eclipse、MyEclipse 8

开发依赖库:

JavaEE5、Spring 3.0.0.M4、FreeMarker 2.3.16

Email:hoojo_@126.com

Blog:http://blog.csdn.net/IBM_hoojo

http://hoojo.cnblogs.com/



1、 新建WebProject,工程名称是SpringFreemarker;然后手动添加jar包,需要的jar包如下:



SpringFramework jar包下载地址:

http://ebr.springsource.com/repository/app/library/version/detail?name=org.springframework.spring&version=3.0.5.RELEASE

FreeMarker library下载地址:

http://ebr.springsource.com/repository/app/bundle/version/detail?name=com.springsource.freemarker&version=2.3.15

当然你也可以去官方下载

2、 在web.xml中添加如下配置:

<!-- 加载Spring容器配置 -->
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 设置Spring容器加载配置文件路径 -->
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath*:applicationContext-*.xml</param-value>
</context-param>
 
<servlet>
    <servlet-name>dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/dispatcher.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>
 
<servlet-mapping>
    <servlet-name>dispatcher</servlet-name>
    <url-pattern>*.do</url-pattern>
</servlet-mapping>


上面分别是添加Spring的监听器、以及配置Spring的配置文件、还有SpringMVC的控制器;

3、 在WEB-INF中添加文件dispatcher.xml,和web.xml中的对应。内容如下:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:util="http://www.springframework.org/schema/util" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans >
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/mvc
    http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context-3.0.xsd
    http://www.springframework.org/schema/util
    http://www.springframework.org/schema/util/spring-util-3.0.xsd"
 
    <context:component-scan base-package="com.hoo" />
 
    <!-- annotation的方法映射适配器 
    <bean id="handlerAdapter" class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/>
    -->
    <!--  annotation默认的方法映射适配器 -->
    <bean id="handlerMapping" class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"/>
    
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
    </bean>
     
</beans>


上面是SpringMVC的基本配置

4、 在src中添加applicationContext-beans.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:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx" 
    xmlns:util="http://www.springframework.org/schema/util"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans >
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/aop 
    http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
    http://www.springframework.org/schema/tx 
    http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
    http://www.springframework.org/schema/util 
    http://www.springframework.org/schema/util/spring-util-3.0.xsd
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context-3.0.xsd"
</beans>


里面可以添加一些bean的配置

5、 在src目录添加freemarker.properties配置文件,这个文件是freemarker一些常用的转换,内容如下:
tag_syntax=auto_detect
template_update_delay=2
default_encoding=UTF-8
output_encoding=UTF-8
locale=zh_CN
date_format=yyyy-MM-dd
time_format=HH:mm:ss
datetime_format=yyyy-MM-dd HH:mm:ss


6、 在dispatcher.xml中添加freemarker的配置,配置如下:
<!-- 设置freeMarker的配置文件路径 -->
<bean id="freemarkerConfiguration" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
    <property name="location" value="classpath:freemarker.properties"/>
</bean>
 
<!-- 配置freeMarker的模板路径 -->
<bean id="freemarkerConfig" class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
    <!--property name="freemarkerSettings" ref="freemarkerConfiguration"/-->
    <property name="templateLoaderPath">
        <value>/WEB-INF/ftl/</value>
    </property>
    <property name="freemarkerVariables">
        <map>
            <entry key="xml_escape" value-ref="fmXmlEscape" />
        </map>
    </property>
</bean>
 
<bean id="fmXmlEscape" class="freemarker.template.utility.XmlEscape"/>
 
<!-- 配置freeMarker视图解析器 -->
<bean id="viewResolver" class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
    <property name="viewClass" value="org.springframework.web.servlet.view.freemarker.FreeMarkerView"/>
    <property name="viewNames" value="*.ftl"/>
    <property name="contentType" value="text/html; charset=utf-8"/>
    <property name="cache" value="true" />
    <property name="prefix" value="" />
    <property name="suffix" value="" />
    <property name="order" value="2"/>
</bean>


上面最关键的就是freeMarker的视图解析器viewResolver的配置,viewClass是使用哪个视图解析器,这里是类路径;其他的和jsp的视图解析器都很类似。

7、 下面在WEB-INF中添加2个ftl模板,在WEB-INF添加ftl,然后添加hello.ftl/hi.ftl,内容分别是:

hello.ftl

<html>
<body>
    <h1>say hello ${name}</h1><br/>
    ${(1 == 1)?string("yes", "no")}
</body>
</html>

hi.ftl
<html>
<body>
    <h1>say hello ${name}</h1><br/>
    ${(1 != 1)?string("yes", "no")}
</body>
</html>


8、 添加Controller控制器,代码如下:
package com.hoo.controller;
 
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
 
/**
 * <b>function:</b> FreeMarker示例控制器
 * @author hoojo
 * @createDate 2011-3-3 下午04:50:10
 * @file HelloWorldController.java
 * @package com.hoo.controller
 * @project SpringFreemarker
 * @version 1.0
 */
@Controller
@RequestMapping("/freeMarker")
public class HelloWorldController {
    
    @RequestMapping("/hello")
    public String sayHello(ModelMap map) {
        System.out.println("say Hello ……");
        map.addAttribute("name", " World!");
        return "/hello.ftl";
    }
    
    @RequestMapping("/hi")
    public String sayHi(ModelMap map) {
        System.out.println("say hi ……");
        map.put("name", "jojo");
        return "/hi.ftl";
    }
    
    @RequestMapping("/jsp")
    public String jspRequest(ModelMap map) {
        System.out.println("jspRequest ……");
        map.put("name", "jsp");
        return "/temp.jsp";
    }
}


9、 添加index.jsp的测试链接或temp.jsp的内容:

index.jsp
<body>
      <a href="freeMarker/hello.do">say hello</a><br/>
      <a href="freeMarker/hi.do">say hi</a><br/>
      <a href="freeMarker/jsp.do">jspRequest</a>
</body>

temp.jsp
<body>
    <h3>${name }</h3>
</body>

如果运行无错误,并输出正确的结果就整合成功!~.~
分享到:
评论

相关推荐

    FreeMarker整合Spring_3

    ### FreeMarker与Spring 3整合知识点详解 #### 一、FreeMarker与Spring 3整合概述 **FreeMarker**是一款强大的模板引擎,它被广泛应用于Web应用中,用于生成动态页面内容。而**Spring框架**是Java领域中最受欢迎的...

    FreeMarker整合Spring 3.0

    **FreeMarker整合Spring 3.0** 在Java Web开发中,FreeMarker是一个强大的模板引擎,它与Spring框架的集成能够帮助开发者实现动态视图的渲染。这篇文章将深入探讨如何将FreeMarker与Spring 3.0进行整合,以及在这个...

    Freemarker整合spring实例详解

    在实际开发中,将Freemarker整合到Spring中可以实现灵活的视图渲染。 首先,我们需要在Spring配置文件(如`applicationContext.xml`)中配置Freemarker的相关设置。这包括添加`FreeMarkerConfigurer` bean来设置...

    spring与freemarker整合 示例源码

    在IT行业中,Spring框架是Java应用开发中的一个核心组件,它提供了一个全面的编程...通过这个示例源码,你可以了解到Spring与Freemarker整合的具体实现,以及在实际项目中如何运用这一组合来构建高效、灵活的Web应用。

    Freemarker整合Spring

    Freemarker整合Spring是Web开发中常见的技术组合,主要用于实现动态网页生成。在Java Web应用中,Freemarker是一个强大的模板引擎,而Spring框架则提供了全面的依赖注入和控制反转功能,两者结合可以构建出高效、...

    Freemarker2 整合 Spring3

    在本教程中,我们将探讨如何将FreeMarker2与Spring3框架进行整合,以便在JavaEE应用中实现动态模板渲染。这个过程主要分为几个步骤,包括环境配置、项目结构设置、库依赖、web.xml配置以及Spring配置文件的创建。 ...

    整合 freemarker +spring security + spring MVC + spring DATA jpa 前端jtable 简单的crud

    本项目结合了Freemarker、Spring Security、Spring MVC和Spring Data JPA,旨在实现前端JTable的简单CRUD(创建、读取、更新、删除)功能。以下是这些技术的详细介绍及其在项目中的应用。 **Freemarker** 是一个...

    基于Annoation的spring 2.5+ hibernate + freemarker整合

    本项目"基于Annoation的Spring 2.5+ Hibernate + Freemarker整合"是将这三个框架集成在一起,实现了一个高效、灵活的Web应用程序开发环境。让我们详细探讨这三个框架及其在该项目中的整合应用。 首先,Spring 2.5是...

    spring-boot-freemarker整合源码

    spring-boot-freemarker 整合源码

    Struts2+Spring2.5+Hibernate3+Freemarker整合

    整合S2SH+Freemarker,后台用Spring管理各个bean,Hibernate做数据库持久化,viewer用Freemarker。整合中对Struts2,Hibernate,Spring都采用Annotation进行注解类。

    spring与freemarker整合 示例源码S

    整合Spring和Freemarker的过程主要包括以下几个步骤: 1. **添加依赖**:在项目的构建文件(如Maven的pom.xml或Gradle的build.gradle)中,需要引入Spring和Freemarker的相关依赖。确保包含Spring的web模块和...

    Freemarker+Spring整合

    Freemarker+Spring整合是将流行的模板引擎FreeMarker与强大的Spring框架相结合,以实现动态页面渲染和业务逻辑的解耦。FreeMarker是一个基于Java的模板引擎,它将数据模型与HTML或其他格式的模板分离,使得开发者...

    Spring3.1整合FreeMarker2.3.19

    标题“Spring3.1整合FreeMarker2.3.19”指的是在Spring 3.1版本的框架中集成FreeMarker 2.3.19模板引擎的过程和相关知识点。FreeMarker是一个开源的Java库,用于生成动态HTML、XML或其他类型的文本,常用于Web应用...

    spring mvc 3.0-mybatis-freemarker整合

    在本项目中,我们主要探讨的是如何将Spring MVC 3.0、MyBatis 3 和 Freemarker 2.3 这三个强大的技术框架整合在一起,以构建一个高效且灵活的Web应用程序。以下是对这些技术及其整合过程的详细说明: **Spring MVC ...

    SpringMVC-Spring-Mybatis-Freemarker整合

    【SpringMVC-Spring-Mybatis-Freemarker整合】是一个常见的Java Web开发技术栈,主要涉及了四个关键组件:Spring MVC(模型-视图-控制器)、Spring(核心框架)、Mybatis(持久层框架)以及Freemarker(模板引擎)。...

    freemarker+spring+maven代码

    **FreeMarker + Spring + Maven 整合指南** 在软件开发中,使用合适的工具和技术栈能够极大地提高开发效率和代码质量。本指南将详细介绍如何利用Maven进行版本控制,结合Spring框架与FreeMarker模板引擎,搭建一个...

    SpringDataJpa整合FreeMarker源码样例

    《SpringDataJpa整合FreeMarker源码解析》 在当今的软件开发中,Spring Boot、Spring Data JPA和FreeMarker的整合已经成为了构建高效、简洁Web应用的常见选择。本篇将深入探讨如何将这三个强大的工具结合在一起,...

    Spring MVC + Tiles + FreeMarker 的整合

    在本文中,我们将深入探讨...通过以上步骤,我们成功地将Spring MVC、Tiles和FreeMarker整合在一起,实现了动态内容和页面布局的灵活管理。这样的整合使得Web应用程序的开发更加高效,提高了代码的可维护性和可扩展性。

    最新ssm:Spring + SpringMVC + Mybatis + FreeMarker 整合示例

    Spring + SpringMVC + Mybatis + FreeMarker 整合示例。所用jar包均是目前位置最新版本:201710最新版: spring mvc4.3.12 , mybatis: 3.4.5 , FreeMarker : 2.3.26。 有mysql数据库脚本。导入即可使用。有个简单的...

    SSH+Freemarker整合

    在进行SSH+Freemarker整合时,首先需要将相关的jar包引入到项目中。这包括Struts2、Spring、Hibernate以及Freemarker的库文件。这些库文件包含了执行MVC操作和模板渲染所需的类和接口。 2. **配置Web.xml**: - *...

Global site tag (gtag.js) - Google Analytics