我们的项目在本次试用的时候,不用考虑和springMVC的集成时的页面展示问题,而只需要考虑生成文件的问题。
自己写的工具类
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.Map;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
public class FreeMakerUtil {
/**
* @param freeMarkerConfigurer
* @param parms
* @param templateName
* @param htmlPath
* @param htmlName
* @throws IOException
* @throws TemplateException
*/
public static void useFreemakerMakerHtml(FreeMarkerConfigurer freeMarkerConfigurer, Map<String, Object> parms, String templateName, String htmlPath,String htmlName) throws IOException, TemplateException {
Writer out = null;
try {
File file = new File(htmlPath);
if(!file.exists()){
if(file.isDirectory()){
file.mkdirs();
}else{
file.mkdir();
}
}
Configuration configuer = freeMarkerConfigurer.getConfiguration();
Template template = configuer.getTemplate(templateName);
String fileName =htmlPath+htmlName;
File file1 = new File(fileName);
if(file1.exists()){
file1.delete();
}
out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(htmlPath+"/"+htmlName), "UTF-8"));
template.process(parms, out);
out.flush();
} catch (IOException e) {
e.printStackTrace();
throw e;
} catch (TemplateException e) {
e.printStackTrace();
throw e;
} finally {
if (out != null) {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public ModelAndView index(HttpServletRequest request, HttpServletResponse response) throws Exception {
Map<String,Object> parms =getParms();
String htmlPath = "D:/WEB-INF/view/";
String htmlName ="test1.html";
String templateName ="freemaker.ftl";
// String templateName1 ="freemaker1.ftl";
// String htmlName1 ="test2.html";
FreeMakerUtil.useFreemakerMakerHtml(freeMarkerConfigurer, parms, templateName, htmlPath, htmlName);
// FreeMakerUtil.useFreemakerMakerHtml(freeMarkerConfigurer, parms, templateName1, htmlPath, htmlName1);
return new ModelAndView();
}
public Map<String,Object> getParms(){
Map<String,Object> parms = new HashMap<String, Object>();
parms.put("a2", "test1");
User user1 = new User(1,1,"1");
parms.put("a1", user1);
ArrayList<User>users = new ArrayList<User>();
User user2 = new User(2,2,"2");
User user3 = new User(3,3,"3");
User user4 = new User(4,4,"4");
users.add(user2);
users.add(user3);
users.add(user4);
parms.put("userList", users);
User testNullUser =new User(5,5,"test5");
parms.put("testNullUser", testNullUser);
return parms;
}
private int age;
private int id;
private String name;
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public User(int age, int id, String name) {
this.age = age;
this.id = id;
this.name = name;
}
}
<head >
<meta http-equiv=Content-Type content="text/html;charset=utf-8">
</head>
<table>
this is the others ftl
<tr>
<th>sigleUser</th>
<td>${a1.name}</td>
<td>${a1.age}</td>
<td>${a1.id}</td>
</tr>
<tr>
<#list userList as user>
<tr>
<td>
id:-------:${user.id}
<#if user.id==2> user id是${user.id}
<#elseif user.name=="2">
user的名字是${user.name}
<#else>
user的年纪是${user.age}
</#if>
</td>
</tr>
</#list>
</tr>
<br>
<tr>
<#if a2!="test1">
this is compare to str ,use operater is "==" a2 value is ${a2}
<#else >
this is compare to str a2 value is not ${a2}
</#if>
<br>
判断是否为空值 用两个??进行判断,注意,else里面才是处理的为空的情况
<#if testNullUser.nullValue??>
值不为空
<#else>
<br>
对空值进行处理的方式为在取值后加上!"XXX",在XXX的值为预设值 ${testNullUser.nullValue!"Set this value is NullValue"}
</#if>
</tr>
<tr>
<#if testNullUser.atongmu ??>
testNullUser.atongmu 不是空值
<#else>
${testNullUser.atongmu!"网卡卡就不是空值了"}
</#if>
对于值为 “”的空串,freemaker不认为其为空 ----------
<br>
<#else>
<br>
对于值为 “”的空串,freemaker认为其为空 ${testNullStr!"认为为空"}
</tr>
</table>
</html>
${XX! "YY"} !后面的“YY”为值为空的时候的预设值
<head >
<meta http-equiv=Content-Type content="text/html;charset=utf-8">
</head>
<table>
this is the others ftl
<tr>
<th>sigleUser</th>
<td>1</td>
<td>1</td>
<td>1</td>
</tr>
<tr>
<tr>
<td>
id:-------:2
user id是2
</td>
</tr>
<tr>
<td>
id:-------:3
user的年纪是3
</td>
</tr>
<tr>
<td>
id:-------:4
user的年纪是4
</td>
</tr>
</tr>
<br>
<tr>
this is compare to str a2 value is not test1
<br>
判断是否为空值 用两个??进行判断,注意,else里面才是处理的是不为空的值
<br>
对空值进行处理的方式为在取值后加上!"XXX",在XXX的值为预设值 Set this value is NullValue
</tr>
<tr>
网卡卡就不是空值了
<br>
对于值为 “”的空串,freemaker不认为其为空 ----------
<br>
</tr>
</table>
</html>