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