Skip to content

Commit

Permalink
First version with freeMarker
Browse files Browse the repository at this point in the history
  • Loading branch information
xujijun committed Jun 1, 2015
1 parent 81ef3d7 commit 880f3d9
Show file tree
Hide file tree
Showing 15 changed files with 318 additions and 0 deletions.
32 changes: 32 additions & 0 deletions .classpath
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" output="target/classes" path="src/main/java">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry excluding="**" kind="src" output="target/classes" path="src/main/resources">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" output="target/test-classes" path="src/test/java">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
<attributes>
<attribute name="maven.pomderived" value="true"/>
<attribute name="org.eclipse.jst.component.dependency" value="/WEB-INF/lib"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.7">
<attributes>
<attribute name="owner.project.facets" value="java"/>
</attributes>
</classpathentry>
<classpathentry kind="output" path="target/classes"/>
</classpath>
47 changes: 47 additions & 0 deletions .project
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>MyJavaStudio</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.wst.jsdt.core.javascriptValidator</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.wst.common.project.facet.core.builder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.wst.validation.validationbuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>com.genuitec.eclipse.j2eedt.core.DeploymentDescriptorValidator</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.m2e.core.maven2Builder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jem.workbench.JavaEMFNature</nature>
<nature>org.eclipse.wst.common.modulecore.ModuleCoreNature</nature>
<nature>org.eclipse.jdt.core.javanature</nature>
<nature>org.eclipse.m2e.core.maven2Nature</nature>
<nature>org.eclipse.wst.common.project.facet.core.nature</nature>
<nature>org.eclipse.wst.jsdt.core.jsNature</nature>
</natures>
</projectDescription>
12 changes: 12 additions & 0 deletions .settings/.jsdtscope
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src/main/webapp"/>
<classpathentry kind="con" path="org.eclipse.wst.jsdt.launching.JRE_CONTAINER"/>
<classpathentry kind="con" path="org.eclipse.wst.jsdt.launching.WebProject">
<attributes>
<attribute name="hide" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.wst.jsdt.launching.baseBrowserLibrary/StandardBrowser/html5"/>
<classpathentry kind="output" path=""/>
</classpath>
12 changes: 12 additions & 0 deletions .settings/org.eclipse.jdt.core.prefs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.7
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
org.eclipse.jdt.core.compiler.compliance=1.7
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
org.eclipse.jdt.core.compiler.debug.localVariable=generate
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning
org.eclipse.jdt.core.compiler.source=1.7
4 changes: 4 additions & 0 deletions .settings/org.eclipse.m2e.core.prefs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
activeProfiles=
eclipse.preferences.version=1
resolveWorkspaceProjects=true
version=1
11 changes: 11 additions & 0 deletions .settings/org.eclipse.wst.common.component
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<project-modules id="moduleCoreId" project-version="1.5.0">
<wb-module deploy-name="MyJavaStudio">
<wb-resource deploy-path="/WEB-INF/classes" source-path="/src/main/java"/>
<wb-resource deploy-path="/" source-path="/target/m2e-jee/web-resources"/>
<wb-resource deploy-path="/" source-path="/src/main/webapp" tag="defaultRootSource"/>
<wb-resource deploy-path="/WEB-INF/classes" source-path="/src/main/resources"/>
<property name="context-root" value="MyJavaStudio"/>
<property name="java-output-path" value="/MyJavaStudio/target/classes"/>
</wb-module>
</project-modules>
8 changes: 8 additions & 0 deletions .settings/org.eclipse.wst.common.project.facet.core.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<faceted-project>
<fixed facet="wst.jsdt.web"/>
<installed facet="jst.web" version="2.3"/>
<installed facet="maven" version="1.0"/>
<installed facet="wst.jsdt.web" version="1.0"/>
<installed facet="java" version="1.7"/>
</faceted-project>
1 change: 1 addition & 0 deletions .settings/org.eclipse.wst.jsdt.ui.superType.container
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
org.eclipse.wst.jsdt.launching.baseBrowserLibrary
1 change: 1 addition & 0 deletions .settings/org.eclipse.wst.jsdt.ui.superType.name
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Window
30 changes: 30 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.xjj</groupId>
<artifactId>MyJavaStudio</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>MyJavaStudio Maven Webapp</name>
<url>http://maven.apache.org</url>

<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
<version>2.3.22</version>
</dependency>

</dependencies>

<build>
<finalName>MyJavaStudio</finalName>
</build>
</project>
104 changes: 104 additions & 0 deletions src/main/java/com/xjj/freemarker/FreeMarkerUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
package com.xjj.freemarker;

import java.io.BufferedWriter;
import java.io.File;
import java.io.IOException;
import java.io.StringWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import com.xjj.freemarker.bean.Notice;

import freemarker.template.Configuration;
import freemarker.template.DefaultObjectWrapperBuilder;
import freemarker.template.Template;
import freemarker.template.Version;

public class FreeMarkerUtils {
//模板路径
public static final String PATH = "com/xjj/freemarker/template";

public static Version version = new Version("2.3.22");
public static Configuration config;

// 模板对象
public static Template template;

public static DefaultObjectWrapperBuilder defaultObjectWrapperBuilder;

// 创建 Configuration 对象
static {
//final File file = new File(ClassPathUtil.getClassPath(PATH));
final File file = new File(FreeMarkerUtils.class.getResource("/"+PATH).getPath());
config = new Configuration(version);
// 设置默认编码 (至关重要 - 解决中文乱码问题)
config.setDefaultEncoding("UTF-8");

defaultObjectWrapperBuilder = new DefaultObjectWrapperBuilder(version);
// 对象包装器
//final ObjectWrapper objectWrapper = defaultObjectWrapperBuilder.build(); //= new DefaultObjectWrapper();

try {
// 设置模板的目录
config.setDirectoryForTemplateLoading(file);
// 设置对象包装器
config.setObjectWrapper(defaultObjectWrapperBuilder.build());
} catch (IOException e) {
e.printStackTrace();
}
}

/**
* 根据template和dataMap生成文档内容
* @param templateName 模板文件名
* @param dataMap 需要填充的数据
* @return 生成的文档内容
*/
public static <T> String buildContent(final String templateName, final Map<String, T> dataMap) {
// 返回结果
String result = null;
try {
// 从模板目录下获取指定 ftl 模板文件
template = config.getTemplate(templateName);
// 设置文件编码
//template.setEncoding("UTF-8");
// 字符串 字符输出流
final StringWriter stringWriter = new StringWriter();
final BufferedWriter bufferedWriter = new BufferedWriter(stringWriter);
// 合并 数据模型 与 模板
template.process(dataMap, bufferedWriter);

// 获取输出的字符串 (调用其 toString方法)
result = stringWriter.toString();
// 刷新
bufferedWriter.flush();
// 关闭输出流
bufferedWriter.close();

} catch (Exception e) {
e.printStackTrace();
}

return result;
}

//测试
public static void main(String[] args) {
Notice notice = new Notice();
notice.setRecipient("张三");
notice.setPublisher("系统管理员");
notice.setContent("这是通知内容,请记住。\n不要问我是谁!");
SimpleDateFormat df = new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒");
notice.setDateTime(df.format(new Date()));

Map<String, Notice> dataMap = new HashMap<>();
dataMap.put("data", notice);

String templateName = "Notice.ftl";
String content = FreeMarkerUtils.buildContent(templateName, dataMap);

System.out.print(content);
}
}
33 changes: 33 additions & 0 deletions src/main/java/com/xjj/freemarker/bean/Notice.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.xjj.freemarker.bean;

public class Notice {
private String recipient;
private String content;
private String publisher;
private String dateTime;

public String getRecipient() {
return recipient;
}
public void setRecipient(String recipient) {
this.recipient = recipient;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public String getPublisher() {
return publisher;
}
public void setPublisher(String publisher) {
this.publisher = publisher;
}
public String getDateTime() {
return dateTime;
}
public void setDateTime(String dateTime) {
this.dateTime = dateTime;
}
}
11 changes: 11 additions & 0 deletions src/main/java/com/xjj/freemarker/template/Notice.ftl
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
亲爱的${data.recipient}

您好!

请查看如下通知:
${data.content}

感谢您的阅读!

- ${data.publisher}
- ${data.dateTime}
7 changes: 7 additions & 0 deletions src/main/webapp/WEB-INF/web.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
<display-name>Archetype Created Web Application</display-name>
</web-app>
5 changes: 5 additions & 0 deletions src/main/webapp/index.jsp
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<html>
<body>
<h2>Hello World!</h2>
</body>
</html>

0 comments on commit 880f3d9

Please sign in to comment.