Maven+SpringMVC+Freemarker入门Demo
1 参考http://blog.csdn.net/haishu_zheng/article/details/51490299,用第二种方法创建一个名为mavenspringmvcfreemarker的Maven工程。
2 文件目录结构如下图所示
3 在pom.xml中添加springmvc和freemarker的依赖包,添加完之后的完整内容为
- <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.0http://maven.apache.org/xsd/maven-4.0.0.xsd">
- <modelVersion>4.0.0</modelVersion>
- <groupId>mavenspringmvcfreemarker</groupId>
- <artifactId>mavenspringmvcfreemarker</artifactId>
- <version>0.0.1-SNAPSHOT</version>
- <packaging>war</packaging>
- <name>mavenspringmvcfreemarker</name>
- <description />
- <properties>
- <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
- </properties>
- <dependencies>
- <dependency>
- <groupId>org.freemarker</groupId>
- <artifactId>freemarker</artifactId>
- <version>2.3.20</version>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-context-support</artifactId>
- <version>3.2.9.RELEASE</version>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-web</artifactId>
- <version>3.2.9.RELEASE</version>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-webmvc</artifactId>
- <version>3.2.9.RELEASE</version>
- </dependency>
- <dependency>
- <groupId>javax</groupId>
- <artifactId>javaee-api</artifactId>
- <version>7.0</version>
- <scope>provided</scope>
- </dependency>
- <dependency>
- <groupId>org.glassfish.web</groupId>
- <artifactId>javax.servlet.jsp.jstl</artifactId>
- <version>1.2.2</version>
- </dependency>
- </dependencies>
- <build>
- <plugins>
- <plugin>
- <artifactId>maven-compiler-plugin</artifactId>
- <version>2.3.2</version>
- <configuration>
- <source>1.7</source>
- <target>1.7</target>
- </configuration>
- </plugin>
- <plugin>
- <artifactId>maven-war-plugin</artifactId>
- <version>2.2</version>
- <configuration>
- <version>3.1</version>
- <failOnMissingWebXml>false</failOnMissingWebXml>
- </configuration>
- </plugin>
- </plugins>
- </build>
- </project>
4 web.xml中的完整内容为
- <?xml version="1.0"encoding="UTF-8"?>
- <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">
- <display-name>mavenspringmvcfreemarker</display-name>
- <servlet>
- <servlet-name>spring</servlet-name>
- <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
- <load-on-startup>1</load-on-startup>
- </servlet>
- <servlet-mapping>
- <servlet-name>spring</servlet-name>
- <url-pattern>/</url-pattern>
- </servlet-mapping>
- </web-app>
5 spring-servlet.xml中的完整内容为:
- <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"
- xsi:schemaLocation="http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
- http://www.springframework.org/schema/context
- http://www.springframework.org/schema/context/spring-context-3.0.xsd">
- <context:component-scan base-package="com.demo" />
- <bean
- class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" />
- <bean
- class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />
- <!-- 配置Freemarker属性文件路径 -->
- <bean id="freemarkerConfiguration"
- class="org.springframework.beans.factory.config.PropertiesFactoryBean">
- <property name="location" value="classpath:conf/freemarker.properties" />
- </bean>
- <!-- 配置freeMarker模板加载地址 -->
- <bean id="freemarkerConfig"
- class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
- <!-- 视图解析器在/WEB-INF/ftl/路径下扫描视图文件 -->
- <property name="templateLoaderPath" value="/WEB-INF/ftl/" />
- <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="freemakerViewResolver"
- class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
- <property name="viewClass"
- value="org.springframework.web.servlet.view.freemarker.FreeMarkerView" />
- <!-- 扫描路径內所有以ftl結尾的文件 -->
- <property name="viewNames">
- <array>
- <value>*.ftl</value>
- </array>
- </property>
- <property name="contentType" value="text/html; charset=UTF-8" />
- <property name="exposeRequestAttributes" value="true" />
- <property name="exposeSessionAttributes" value="true" />
- <property name="exposeSpringMacroHelpers" value="true" />
- <property name="requestContextAttribute" value="request" />
- <!-- 给视图解析器配置优先級,你可以给之前jsp视图解析器的值配为2 -->
- <property name="order" value="1" />
- </bean>
- </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>下方加代码:
- <init-param>
- <param-name>contextConfigLocation</param-name>
- <param-value>/WEB-INF/applicationContext.xml</param-value>
- </init-param>
6 freemarker.properties中的完整内容为
- 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
7 StudentController.java中的完整代码为:
- package com.demo.controller;
- import org.springframework.stereotype.Controller;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.ui.Model;
- @Controller
- public class StudentController {
- @RequestMapping("/helloWorld")
- public String helloWorld(Model model) {
- String word0 = "Hello ";
- String word1 = "World!";
- //将数据添加到视图数据容器中
- model.addAttribute("word0",word0);
- model.addAttribute("word1",word1);
- return "Hello.ftl";
- }
- }
8 Hello.ftl中的完整代码为:
- <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
- <html>
- <head>
- <meta http-equiv="Content-Type"content="text/html; charset=UTF-8">
- <title>Insert title here</title>
- </head>
- <body>
- <h2>${word0}${word1}</h2>
- </body>
- </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
Maven+SpringMVC+Freemarker入门Demo的更多相关文章
- Freemarker入门Demo
1:工程引入依赖 <dependency> <groupId>org.freemarker</groupId> <artifactId>freemark ...
- IntelliJ IDEA下Maven SpringMVC+Mybatis入门搭建例子
很久之前写了一篇SSH搭建例子,由于工作原因已经转到SpringMVC+Mybatis,就以之前SSH实现简单登陆的例子,总结看看SpringMVC+Mybatis怎么实现. Spring一开始是轻量 ...
- Maven+SpringMVC+Dubbo 简单的入门demo配置
转载自:https://cloud.tencent.com/developer/article/1010636 之前一直听说dubbo,是一个很厉害的分布式服务框架,而且巴巴将其开源,这对于咱们广大程 ...
- 170328、Maven+SpringMVC+Dubbo 简单的入门demo配置
之前一直听说dubbo,是一个很厉害的分布式服务框架,而且巴巴将其开源,这对于咱们广大程序猿来说,真是一个好消息.最近有时间了,打算做一个demo把dubbo在本地跑起来先. 先copy一段dubbo ...
- SpringMVC整合Freemarker(含Demo源码)(转)
转自:http://blog.csdn.net/sinat_27535209/article/details/61199452 整合过程如下: 1.新建一个maven web工程,使用maven依赖s ...
- Spring MVC 环境搭建(maven+SpringMVC+mybatis+Freemarker)
Spring MVC 环境搭建(maven+SpringMVC+mybatis+Freemarker) 一.准备工作 1.Eclipse Java EE IDE(4.4.1) 2.JDK 3.Tomc ...
- SpringMVC框架入门配置 IDEA下搭建Maven项目(zz)
SpringMVC框架入门配置 IDEA下搭建Maven项目 这个不错哦 http://www.cnblogs.com/qixiaoyizhan/p/5819392.html
- SpringMVC框架入门配置 IDEA下搭建Maven项目
初衷:本人初学SpringMVC的时候遇到各种稀奇古怪的问题,网上各种技术论坛上的帖子又参差不齐,难以一步到位达到配置好的效果,这里我将我配置的总结写到这里供大家初学SpringMVC的同僚们共同学习 ...
- [转]SpringMVC框架入门配置 IDEA下搭建Maven项目
初衷:本人初学SpringMVC的时候遇到各种稀奇古怪的问题,网上各种技术论坛上的帖子又参差不齐,难以一步到位达到配置好的效果,这里我将我配置的总结写到这里供大家初学SpringMVC的同僚们共同学习 ...
随机推荐
- Find Peak Element——二分查找的变形
A peak element is an element that is greater than its neighbors. Given an input array where num[i] ≠ ...
- hive的窗口函数1
Hive中提供了越来越多的分析函数,用于完成负责的统计分析.抽时间将所有的分析窗口函数理一遍,将陆续发布.今天先看几个基础的,SUM.AVG.MIN.MAX.用于实现分组内所有和连续累积的统计. 1. ...
- 176. Second Highest Salary
Write a SQL query to get the second highest salary from the Employee table. +----+--------+ | Id | S ...
- PHP必用代码片段
在编写代码的时候有个神奇的工具总是好的!下面这里收集了 50+ PHP 代码片段,可以帮助你开发 PHP 项目. 这些 PHP 片段对于 PHP 初学者也非常有帮助,非常容易学习,让我们开始学习吧- ...
- 模仿jq里的选择器和color样式
(function(){ HTMLElement.prototype.css = function () { var option; if (arguments.length > 0) { op ...
- Js文件中调用其它Js函数的方法
在项目开发过程中,也许你会遇这样的情况.在某一Js文件中需要完成某一功能,但这一功能的大部分代码在另外一个Js文件中已经完成了,自己只需要调用这个方法再加上几句代码就可以实现所需的功能.我们知道,在h ...
- jdk的split 有多坑
先看段 代码: String str = "4117|519951|长信利泰灵活配置混合型证券投资基金|长信利泰|3|3||||156|0||||||||||||||||||||{\&quo ...
- Bzoj1015/洛谷P1197 [JSOI2008]星球大战(并查集)
题面 Bzoj 洛谷 题解 考虑离线做法,逆序处理,一个一个星球的加入.用并查集维护一下连通性就好了. 具体来说,先将被消灭的星球储存下来,先将没有被消灭的星球用并查集并在一起,这样做可以路径压缩,然 ...
- 【C++】析构函数的作用和用法
一.定义1. 作用:对象消亡时,自动被调用,用来释放对象占用的空间2.特点: (1) 名字与类名相同 (2) 在前面需要加上"~" (3) 无参数,无返回值 (4) ...
- If you want to allow applications containing errors to be published on the server
If you want to allow applications containing errors to be published on the server, enable the Allow ...