FreeMarker整合Spring 3

2023-10-29

开发环境:

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包如下:

clip_image002

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>

如果运行无错误,并输出正确的结果就整合成功!~.~

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

FreeMarker整合Spring 3 的相关文章

随机推荐

  • 富文本 解决HTML字段

    转载自 https blog csdn net qq 36467463 article details 79072428 RichText Android平台下的富文本解析器 流式操作 低侵入性 支持Html和Markdown格式文本 支持
  • USRP B210常用软件安装

    系统需求 硬件配置 intel i5或以上 USB3 0接口 树莓派也可以 操作系统 ubuntu 20 04 ubuntu 18 04环境配置有些差异 如果按照这里的命令操作可能会出问题 可以用虚拟机吗 当然没问题 虚拟机是多好的发明 等
  • Flutter实现CombineExecutor进行多个异步分组监听,监听第一个异步执行的开始和最后一个异步执行结束时机。

    1 场景 我们在调用接口时 很多时候会同时调用多个接口 接口都是异步执行 我们很难知道调用的多个接口哪个会最后执行完成 我们有时候需要对最后一个接口执行完成的时机监听 所以基于该需求 设计了CombineExecutor 对类似的需求进行监
  • [pytest源码5]-pluggy深挖调用逻辑

    前言 本篇将详细分析如何通过PluginManager hook调用plugin实现 个人拙见 有错请各位指出 如果的我的文章对您有帮助 不符动动您的金手指给个Star 予人玫瑰 手有余香 不胜感激 GitHub pytest pluggy
  • PostgreSQL启动失败:本地计算机上的postgresq-x64-11服务启动后停止。某些服务或程序使用时将自动停止。

    PostgreSQL启动失败 本地计算机上的postgresq x64 11服务启动后停止 某些服务或程序使用时将自动停止 问题描述 安装PostgreSQL数据库后 启动服务时提示 本地计算机上的postgresq x64 11服务启动后
  • 云存储技术分析和架构框架设计

    摘要 随着互联网类新兴业务的激增 业务数据快速增长 云存储技术应运而生 本文深入剖析了云存储通用框架 硬件架构以及其底层原理这三个技术层面的差异性 为云存储架构框架设计提供了理论依据 再结合细分行业及其业务应用场景的差异性需求 最终确定了满
  • 小程序充值功能总结

    1 功能介绍 1 1 充值 功能描述 拉起小程序的支付组件 1 1 1 实现代码 1 1 2 注意要点 充值的关键点是openId和appId能匹配上 因为公众号和小程序生成的openId是不一样的 1 1 3 报错提示 1 request
  • 深度学习面试题总结-2022总结

    背景 社招 我估摸着校招聘一样会问 主要方向 自动驾驶 祝愿每一个求职者都可以顺利 谢谢大家的支持 我刷题的思路就是repeat repeat repeat 背 背 背 背着背着就懂了 刷的dp 二叉树目前一题没考 好苦 地平线1面 自我介
  • 用Android Studio制作一个简易的计算器,Android开发经验的有效总结

    首先建立按钮 这只是单单的建议按钮 连接按钮的代码在底下 public class MainActivity extends AppCompatActivity implements View OnClickListener private
  • spring boot + sql server大数据量批量新增

    先发出来 注释慢慢完善 获取类中所有属性注解 TableId和 TableField param instance return throws NoSuchFieldException public static Map
  • 安装ubuntu22.04系统,配置国内源以及ssh远程登录

    一 安装ubuntu22 04系统 原文连接 Ubuntu操作系统22 04版本安装教程 VMware虚拟机 wx63f86e949a470的技术博客 51CTO博客 1 点击界面左侧的开启此虚拟机 即可进入Ubuntu操作系统安装界面 点
  • Maven:搭建私服Nexus

    Nexus 安装 下载地址 小诀窍 使用迅雷下载比直接用浏览器下载快很多 https download sonatype com nexus 3 latest unix tar gz 上传 解压 上传到 Linux 系统 解压后即可使用 不
  • 【Blog 5】软件构造落幕,计算人生启程

    经历了7周的学习 软件构造课落下帷幕 但我知道 这对我而言 才只是一个开始 最后几章介绍面向可复用性 可维护性 正确性与健壮性的软件构造的技术 过程等等 看似是不同的角度 其实内部联系密切 尤其是关于继承 委托 SOLID原则等有关知识 都
  • FindObjectsOfType返回场景中所有该类型的组件集合

    做一个简单的demo 场景中准备七个空物体 层级关系如下 查找场景中所有出现的gggg组件 然后把test这个类挂在MainCamera这个物体上 然后运行场景 控制台打印结果为 3 总结 FindObjectsOfType返回场景中所有改
  • 测试产品说明书

    本篇文档是来自csdn 我觉得比较好 于是就收录了 尽管测试产品说明书不是所以软测人员都有机会去做 但还是值得一谈的 如果有幸在项目早期介入软件开发 并有一定的话语权的话 就相当有用了 在软件开发初始阶段发现软件缺陷将可能为项目节省大笔的开
  • 数据结构:队列Queue详解

    文章目录 一 队列的概念和特点 二 队列的使用 三 队列的简单实现 四 循环队列 一 队列的概念和特点 队列 只允许在一端进行插入数据操作 在另一端进行删除数据操作的特殊线性表 进行插入操作的一端称为队尾 删除操作的一端称队头 入队列 进行
  • 管理系统的设计与实现方法总结

    项目总结 1 项目开发背景 目前 国内外毕业论文选题一般采用两种方式 一种将毕业设计存在软盘上交 另一种则存放到教师的电脑上的一个共享目录内 但这两种方法都有各自的弊端 前一种方法不方便携带 速度慢 容量小 易损坏 后一种方法虽然解决了软盘
  • 关于互联网思维与技术团队的一些总结

    2017 7 4更 真正在底层工作的人员 跟站在高层的人看到的东西都是两个东西 真正的从底层走到高层才能看的更精准 同样的 从底层走到高层的人 也没有一直处在高层的远见与见识 我信奉公司处于什么阶段用什么样的人 没必要一开始就弄高精尖的人和
  • 基于Docker的Hadoop集群搭建

    基于Docker的Hadoop集群搭建 本文为在阿里云服务器上基于docker的Hadoop集群搭建 安装思路为 安装docker gt 运行docker导入ubuntu镜像 gt 运行ubuntu系统 gt 在系统中配置好单个节点 gt
  • FreeMarker整合Spring 3

    开发环境 System Windows WebBrowser IE6 Firefox3 JavaEE Server tomcat5 0 2 8 tomcat6 IDE eclipse MyEclipse 8 开发依赖库 JavaEE5 Sp