1. java 项目中怎样添加freemarker
用ssh啦,那很简单。设置视图类型为freemarker就可以了。不过要加入struts-freemarker插件啊
2. 用FreeMarker方法java生成word文档出现异常
应该是没有成功生成,内部代码里还包含了word无法识别的代码块,所以无法打开,你可以用记事本查看源代码,freenarker转换的word源代码都是xml格式
3. 如何使用Freemarker生成java代码
Freemarker是一个模板框架。我们可以通过Freemarker进行代码生成或页面的静态生成。 现在简单的说一下怎样使用Freemarker Freemarker的主要生成类
public boolean generate(String templateFileName, Map data,
String fileName) {
try {
//取得模板的位置
String templateFileDir=templateFileName.substring(0, templateFileName.lastIndexOf("/"));
//取得模板的名字
String templateFile=templateFileName.substring(templateFileName.lastIndexOf("/"), templateFileName.length());
//取得生成文件的路径
String genFileDir=fileName.substring(0, fileName.lastIndexOf("/"));
Template template = ConfigurationHelper.getConfiguration(templateFileDir).getTemplate(templateFile);
File fileDir=new File(genFileDir);
org.apache.commons.io.FileUtils.forceMkdir(fileDir);
File output = new File(fileName);
if(output.exists()){
//如何代码已存在不重复生成
return false;
}
Writer writer = new FileWriter(output);
template.process(data, writer);
writer.close();
} catch (TemplateException e) {
e.printStackTrace();
return false;
} catch (IOException e) {
e.printStackTrace();
return false;
}
return true;
}
代码中的Map 是模板所需要的数据,我们可以通过面向对像的方法把数据存在模板中public boolean genDaoInterface(String fileName){
DaoModel Model=new DaoModel();
//设置Dao实现类的包名
Model.setPackageName(DaoConstant.PACKAGE);
//取得接口名
String className=StringUtils.substringBefore(fileName,".");
//设置接口名
Model.setClassName(className);
Map<String, Object> data = new HashMap<String, Object>();
data.put("model", Model);
//设置生成的位置
String filePath=new String("src/"+package2path(DaoConstant.PACKAGE)+"/"+fileName);
//代码生成
return super.generate(DaoConstant.INTERFACE_TEMPLATE, data, filePath);
}
data.put("model", Model);由这句代码可看出我们将可以在模板中直接调用这些数据package ${model.packageName};
public interface ${model.className} extends BaseHibernateDao {
}
4. java开发中freemarker这么写是什么含义
! == 就是非等于的意思, 也就是=== 这种的非
== 是equality 等同,=== 是identity 恒等。
==, 两边值类型不同的时候,要先进行类型转换,再比较。
===,不做类型转换,类型不同的一定不等。
5. Java-freemarker怎么判断变量不存在
以下是方法:
6. freemarker中有类似java中split的方法吗
<#list"aaa,bbb,ccc,ddd"?split(",")asstr>${str}</#list>
7. 请教FreeMarker在java web开发中使用方法
在 Web应用中使用FreeMarker
在Web应用中使用FreeMarker跟在Java程序中使用并没有太大的区别. 下面是是一个在Web中使用的例子,用来生成HTML页面的模板文件内容如下:
<html>
<head>
<title>FreeMarker 的HelloWorld</title>
</head>
<body>
${message}
</body>
</html>
我们在Web应用中使用FreeMarker时,应该让Servlet来合并模板和数据,因此,Servlet负责创建Configuration实例,并负责合并模板和数据,下面是Servlet源代码:
package lee;
import java.util.*;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import freemarker.template.*;
public class HelloServlet extends HttpServlet
{
private Configuration cfg;
public void init()
{
//初始化FreeMarker配置
//创建一个Configuration实例
cfg = new Configuration();
//设置 FreeMarker的模版文件位置
cfg.(getServletContext(), "WEB-INF/templates");
}
public void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
//建立数据模型
Map root = new HashMap();
root.put("message", "Hello FreeMarker!");
//取得模版文件
Template t = cfg.getTemplate("test.ftl");
// 开始准备生成输出
// - 使用模版文件的charset作为本页面的charset
// - 使用text/html MIME-type
response.setContentType("text/html; charset=" + t.getEncoding());
Writer out = response.getWriter();
//合并数据模型和模版,并将结果输出到out中
try
{
t.process(root, out);
}
catch (TemplateException e)
{
throw new ServletException("处理Template模版中出现错误", e);
}
}
}
可以看到这个Servlet类的代码与普通的Java程序中使用FreeMarker大致一样,区别有两个:1,设置FreeMarker加载模板的方法不一样,在Servlet中设置加载的方法是,第一个参数是本web应用的 ServletContext,第二个参数是模板文件的路径.;2,结果必须输出到HttpServletResponse中,才能被浏览器加载.
配置Servlet的web.xml文件中的代码如下:
<web-app>
<servlet>
<servlet-name>hello</servlet-name>
<servlet-class>lee.HelloServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>hello</servlet-name>
<url-pattern>/hello</url-pattern>
</servlet-mapping>
</web-app>
8. 如何利用SQL中数据使用FreeMarker生成JAVA实体bean代码
利用freemarker生成JAVA BEAN
Freemarker模板代码如下:
package ${packageName};
/**
* <#if author == "adams"> @author adams </#if>
*/
pulic class ${className} {
<#list attrs as a>
private ${a.type} ${a.field};
</#list>
<#list attrs as a>
public void set${a.field?cap_first}(${a.type} ${a.field}){
this.${a.field} = ${a.field};
}
public ${a.type} get${a.field?cap_first}(){
return this.${a.field};
}
</#list>
}
Java代码如下
package com.my.learn.freemarker;
public class Attr{
public String field;
public String type;
public Attr(String field, String type){
this.field = field;
this.type = type;
}
public String getField(){
return this.field;
}
public String getType(){
return this.type;
}
public void setField(String field){
this.field = field;
}
public void setType(String type){
this.type = type;
}
}
package com.my.learn.freemarker;
import java.io.File; import java.io.IOException; import java.io.StringWriter; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map;
import freemarker.template.Configuration; import freemarker.template.Template; import freemarker.template.TemplateException;
public class FmAppUseage {
public static void main(String[] args){
List<Object> list = new ArrayList<Object>();
list.add(new Attr("username", "String"));
list.add(new Attr("password", "String"));
list.add(new Attr("age", "int"));
list.add(new Attr("hobby", "String"));
Map<String,Object> root = new HashMap<String, Object>();
root.put("packageName", "com.my.learn.freemarker");
root.put("className", "User");
root.put("attrs", list);
root.put("author", "adams");
Configuration cfg = new Configuration();
String path = FmAppUseage.class.getResource("/").getPath()+"template";
try {
cfg.(new File(path));
Template template = cfg.getTemplate("/demo.ftl");
StringWriter out = new StringWriter();
template.process(root, out);
System.out.println(out.toString());
} catch (IOException e) {
System.out.println("Cause==>" + e.getCause());
} catch (TemplateException e) {
System.out.println("Cause==>" + e.getCause());
}
}
}
输出结果如下:
package com.my.learn.freemarker;
/**
* @author adams
*/
pulic class User {
private String username;
private String password;
private int age;
private String hobby;
public void setUsername(String username){
this.username = username;
}
public String getUsername(){
return this.username;
}
public void setPassword(String password){
this.password = password;
}
public String getPassword(){
return this.password;
}
public void setAge(int age){
this.age = age;
}
public int getAge(){
return this.age;
}
public void setHobby(String hobby){
this.hobby = hobby;
}
public String getHobby(){
return this.hobby;
}
}
当在笔者刚做测试时,将Attr的类定义在了FmAppUseage类的内部,导致不能正常运行,只能将其移除单独成一个类时,便能正常运行了。 转载仅供参考,版权属于原作者。祝你愉快,满意请采纳哦
9. FreeMarker里如何调用java代码
方法1:
##定义配置文件 freeerstatic.properties
_Validator=com.longyou.util.Validator
_Functions=com.longyou.util.Functions
_EscapeUtils=com.longyou.util.EscapeUtils
/调用代码
${_Functions.toUpperCase("Hello")}<br>
${_EscapeUtils.escape("狼的原野")}
方法2:${stack.findValue("@package.ClassName@method")}${stack.findValue("@package.ClassName@property")}因为 stack 是webwork结合 freeer 后在 ftl 中可以用的,其他的还有 ${base}等等