导航:首页 > 编程语言 > freemarker调用java方法调用

freemarker调用java方法调用

发布时间:2023-03-02 15:12:33

❶ FreeMarker里如何调用java代码

不能使用java代码,只能使用el表达式,来输出展示数据..

freemarker 的原则或初衷:
数据+模板=输出

模板就是 freemarker了,这样程序员就不能把大量java代码写入, 美工和程序员 工作就可以独立了

❷ 如何在freemarker中调用java的方法

解决方案 »

freeer可以声明变量,可以写一些函数,可以调用自己的函数
可~~要实例化java类并且调用它的方法~~~没遇到过....
帮忙顶!期待高手

急啊,我也想在ftl中调用一个类的静态方法...

可以,可以将你写的类当成对象假如到MAP中
然后在再用map?key的方法获得该类
JAVA代码
maplist.put("typeTool",this);
FREEMARKER
<#assign name=typeTool.getTypeName(key)>${name}

方法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}等等

❸ 如何使用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 {

}

❹ 如何利用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类的内部,导致不能正常运行,只能将其移除单独成一个类时,便能正常运行了。 转载仅供参考,版权属于原作者。祝你愉快,满意请采纳哦

❺ 我在java的freemarker文件里有写个excel按钮,在action里面调用方法没实现

在excel方法第一行打断点,进入到断点中了吗?
如没有,检查jsp上提交的那段method.在browser的状态栏或debug 脚本可以查看是否进入到action.
如解决不了且不涉及隐私,可以发给我,大家互相找找原因.

阅读全文

与freemarker调用java方法调用相关的资料

热点内容
安卓手机为什么没有桌面插件 浏览:346
鼎新app是什么 浏览:113
程序员和施工员哪个有前途 浏览:614
浙江百兆服务器云空间 浏览:827
php和html5学哪个比较好 浏览:388
瑜伽pdf下载地址 浏览:252
小花猫app改名叫什么 浏览:837
编程器固件修改 浏览:523
沈阳技校有程序员吗 浏览:300
各行app的基金有什么不一样吗 浏览:62
python编译器怎么装 浏览:73
新浪php笔试题 浏览:138
新时达服务器如何自学习 浏览:27
程序中什么命令用来输入单字符 浏览:59
php是否有数据 浏览:141
虚拟云服务器是什么意思 浏览:279
vb是编译性语言吗 浏览:209
json格式用什么编译器 浏览:319
word转pdf代码 浏览:802
单片机中如何编程 浏览:739