My development environment
Framework: springmvc+freemarker
Development tool: springsource-tool-suite-2.9.0
JDK version: 1.6.0_29
tomcat version: apache-tomcat-7.0 .26
step1. Write the controller file, the code is as follows:
package www.asuan.com.controller; import java.util.ArrayList; import java.util.Date; import java.util.HashMap; import java.util.List; import java.util.Map; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; @Controller public class HelloWorldController { @RequestMapping("/helloWorld") public String helloWorld(Model model) { // 示例一 int flag = 0; model.addAttribute("flag", flag); // 示例二 List<String> noExistList = new ArrayList<String>(); noExistList = null; model.addAttribute("noExistList", noExistList); // 示例三 List<String> strList = new ArrayList<String>(); strList.add("www."); strList.add("cnblogs."); strList.add("com/sunang"); model.addAttribute("strList", strList); // 示例四 Map<String, String> strMap = new HashMap<String, String>(); strMap.put("mapKey0", "www."); strMap.put("mapKey1", "cnblogs."); strMap.put("mapKey2", "com/sunang"); model.addAttribute("strMap", strMap); // 示例五 Date nowTime = new Date(); model.addAttribute("nowTime", nowTime);//傳輸時(shí)間對(duì)象 return "helloWorld.ftl"; } }
step2. Write the ftl file, the code is as follows:
<html> <body> 示例一輸出結(jié)果: <p> <#-- if指令的用法--> <#-- 在指令標(biāo)籤內(nèi)直接使用變量名得到文本值--> <#if flag == 1> flag = 1 <#elseif flag ==2> flag = 2 <#else> <#-- 在指令標(biāo)籤外使用 ${變量名} 的格式來(lái)得到文本值--> flag!=1 && flag!=2 flag的值為:${flag} </#if> </p> <p>----------------------------------------------------------</p> 示例二輸出結(jié)果: <p> <#-- 判斷變量是否存在--> <#if noExistList??> List存在 <#else> List不存在 </#if> </p> <p>----------------------------------------------------------</p> 示例三輸出結(jié)果: <p> <#-- list指令的用法,as可設(shè)置別名--> <#list strList as sl> <#-- 在變量名后加 _index 得到變量在容器中的序號(hào),從0開(kāi)始--> <#if sl_index == 0> 我的博客地址是:${sl} <#else> ${sl} </#if> </#list> </p> <p><p></p> 直接使用下標(biāo)訪問(wèn)List:${strList[0]}${strList[1]}${strList[2]} </p> <p>----------------------------------------------------------</p> 示例四輸出結(jié)果: <p> <#-- 使用 ${變量名.變量名} 獲取容器對(duì)象的子對(duì)象--> ${strMap.mapKey0}${strMap.mapKey1}${strMap.mapKey2} </p> <p>----------------------------------------------------------</p> 示例五輸出結(jié)果: <p> <#-- 當(dāng)變量是日期對(duì)象時(shí),可使用函數(shù)使其按格式輸出--> ${nowTime?string("yyyy-MM-dd")} </p> </body> </html>
step3. Run and debug
Deploy the project to tomcat and run it, enter in the browser: http://localhost:8080/the project name you set/helloWorld.htm
For more Freemarker common instruction usage examples and related articles, please pay attention to the PHP Chinese website!

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)
