├── .gitignore ├── .mvn └── wrapper │ ├── maven-wrapper.jar │ └── maven-wrapper.properties ├── mvnw ├── mvnw.cmd ├── pom.xml └── src ├── main ├── java │ ├── com │ │ └── spring │ │ │ ├── SpringBootFreemarker1Application.java │ │ │ ├── controller │ │ │ ├── FreemarkerController.java │ │ │ ├── FreemarkerController1.java │ │ │ ├── FreemarkerController2.java │ │ │ ├── FreemarkerController3.java │ │ │ ├── FreemarkerController4.java │ │ │ └── FreemarkerController5.java │ │ │ └── method │ │ │ ├── RoleDirectiveModel.java │ │ │ └── SortMethod.java │ └── 项目说明 └── resources │ ├── application.properties │ ├── doc │ ├── 1.png │ ├── Freemarker快速入门.pdf │ ├── freemarker中文手册.pdf │ └── 不错的FreeMarker教程.pdf │ └── templates │ ├── free1.html │ ├── free2.html │ ├── free3.html │ ├── free4.html │ ├── free5.html │ ├── free6.html │ ├── free7.html │ └── index.html └── test └── java └── com └── spring └── SpringBootFreemarker1ApplicationTests.java /.gitignore: -------------------------------------------------------------------------------- 1 | target/ 2 | !.mvn/wrapper/maven-wrapper.jar 3 | 4 | ### STS ### 5 | .apt_generated 6 | .classpath 7 | .factorypath 8 | .project 9 | .settings 10 | .springBeans 11 | 12 | ### IntelliJ IDEA ### 13 | .idea 14 | *.iws 15 | *.iml 16 | *.ipr 17 | 18 | ### NetBeans ### 19 | nbproject/private/ 20 | build/ 21 | nbbuild/ 22 | dist/ 23 | nbdist/ 24 | .nb-gradle/ -------------------------------------------------------------------------------- /.mvn/wrapper/maven-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JavaCodeMood/freemarker/90ccb6ae60ebb598efcefa9651affe9dee977b94/.mvn/wrapper/maven-wrapper.jar -------------------------------------------------------------------------------- /.mvn/wrapper/maven-wrapper.properties: -------------------------------------------------------------------------------- 1 | distributionUrl=https://repo1.maven.org/maven2/org/apache/maven/apache-maven/3.3.9/apache-maven-3.3.9-bin.zip 2 | -------------------------------------------------------------------------------- /mvnw: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | # ---------------------------------------------------------------------------- 3 | # Licensed to the Apache Software Foundation (ASF) under one 4 | # or more contributor license agreements. See the NOTICE file 5 | # distributed with this work for additional information 6 | # regarding copyright ownership. The ASF licenses this file 7 | # to you under the Apache License, Version 2.0 (the 8 | # "License"); you may not use this file except in compliance 9 | # with the License. You may obtain a copy of the License at 10 | # 11 | # http://www.apache.org/licenses/LICENSE-2.0 12 | # 13 | # Unless required by applicable law or agreed to in writing, 14 | # software distributed under the License is distributed on an 15 | # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16 | # KIND, either express or implied. See the License for the 17 | # specific language governing permissions and limitations 18 | # under the License. 19 | # ---------------------------------------------------------------------------- 20 | 21 | # ---------------------------------------------------------------------------- 22 | # Maven2 Start Up Batch script 23 | # 24 | # Required ENV vars: 25 | # ------------------ 26 | # JAVA_HOME - location of a JDK home dir 27 | # 28 | # Optional ENV vars 29 | # ----------------- 30 | # M2_HOME - location of maven2's installed home dir 31 | # MAVEN_OPTS - parameters passed to the Java VM when running Maven 32 | # e.g. to debug Maven itself, use 33 | # set MAVEN_OPTS=-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8000 34 | # MAVEN_SKIP_RC - flag to disable loading of mavenrc files 35 | # ---------------------------------------------------------------------------- 36 | 37 | if [ -z "$MAVEN_SKIP_RC" ] ; then 38 | 39 | if [ -f /etc/mavenrc ] ; then 40 | . /etc/mavenrc 41 | fi 42 | 43 | if [ -f "$HOME/.mavenrc" ] ; then 44 | . "$HOME/.mavenrc" 45 | fi 46 | 47 | fi 48 | 49 | # OS specific support. $var _must_ be set to either true or false. 50 | cygwin=false; 51 | darwin=false; 52 | mingw=false 53 | case "`uname`" in 54 | CYGWIN*) cygwin=true ;; 55 | MINGW*) mingw=true;; 56 | Darwin*) darwin=true 57 | # 58 | # Look for the Apple JDKs first to preserve the existing behaviour, and then look 59 | # for the new JDKs provided by Oracle. 60 | # 61 | if [ -z "$JAVA_HOME" ] && [ -L /System/Library/Frameworks/JavaVM.framework/Versions/CurrentJDK ] ; then 62 | # 63 | # Apple JDKs 64 | # 65 | export JAVA_HOME=/System/Library/Frameworks/JavaVM.framework/Versions/CurrentJDK/Home 66 | fi 67 | 68 | if [ -z "$JAVA_HOME" ] && [ -L /System/Library/Java/JavaVirtualMachines/CurrentJDK ] ; then 69 | # 70 | # Apple JDKs 71 | # 72 | export JAVA_HOME=/System/Library/Java/JavaVirtualMachines/CurrentJDK/Contents/Home 73 | fi 74 | 75 | if [ -z "$JAVA_HOME" ] && [ -L "/Library/Java/JavaVirtualMachines/CurrentJDK" ] ; then 76 | # 77 | # Oracle JDKs 78 | # 79 | export JAVA_HOME=/Library/Java/JavaVirtualMachines/CurrentJDK/Contents/Home 80 | fi 81 | 82 | if [ -z "$JAVA_HOME" ] && [ -x "/usr/libexec/java_home" ]; then 83 | # 84 | # Apple JDKs 85 | # 86 | export JAVA_HOME=`/usr/libexec/java_home` 87 | fi 88 | ;; 89 | esac 90 | 91 | if [ -z "$JAVA_HOME" ] ; then 92 | if [ -r /etc/gentoo-release ] ; then 93 | JAVA_HOME=`java-config --jre-home` 94 | fi 95 | fi 96 | 97 | if [ -z "$M2_HOME" ] ; then 98 | ## resolve links - $0 may be a link to maven's home 99 | PRG="$0" 100 | 101 | # need this for relative symlinks 102 | while [ -h "$PRG" ] ; do 103 | ls=`ls -ld "$PRG"` 104 | link=`expr "$ls" : '.*-> \(.*\)$'` 105 | if expr "$link" : '/.*' > /dev/null; then 106 | PRG="$link" 107 | else 108 | PRG="`dirname "$PRG"`/$link" 109 | fi 110 | done 111 | 112 | saveddir=`pwd` 113 | 114 | M2_HOME=`dirname "$PRG"`/.. 115 | 116 | # make it fully qualified 117 | M2_HOME=`cd "$M2_HOME" && pwd` 118 | 119 | cd "$saveddir" 120 | # echo Using m2 at $M2_HOME 121 | fi 122 | 123 | # For Cygwin, ensure paths are in UNIX format before anything is touched 124 | if $cygwin ; then 125 | [ -n "$M2_HOME" ] && 126 | M2_HOME=`cygpath --unix "$M2_HOME"` 127 | [ -n "$JAVA_HOME" ] && 128 | JAVA_HOME=`cygpath --unix "$JAVA_HOME"` 129 | [ -n "$CLASSPATH" ] && 130 | CLASSPATH=`cygpath --path --unix "$CLASSPATH"` 131 | fi 132 | 133 | # For Migwn, ensure paths are in UNIX format before anything is touched 134 | if $mingw ; then 135 | [ -n "$M2_HOME" ] && 136 | M2_HOME="`(cd "$M2_HOME"; pwd)`" 137 | [ -n "$JAVA_HOME" ] && 138 | JAVA_HOME="`(cd "$JAVA_HOME"; pwd)`" 139 | # TODO classpath? 140 | fi 141 | 142 | if [ -z "$JAVA_HOME" ]; then 143 | javaExecutable="`which javac`" 144 | if [ -n "$javaExecutable" ] && ! [ "`expr \"$javaExecutable\" : '\([^ ]*\)'`" = "no" ]; then 145 | # readlink(1) is not available as standard on Solaris 10. 146 | readLink=`which readlink` 147 | if [ ! `expr "$readLink" : '\([^ ]*\)'` = "no" ]; then 148 | if $darwin ; then 149 | javaHome="`dirname \"$javaExecutable\"`" 150 | javaExecutable="`cd \"$javaHome\" && pwd -P`/javac" 151 | else 152 | javaExecutable="`readlink -f \"$javaExecutable\"`" 153 | fi 154 | javaHome="`dirname \"$javaExecutable\"`" 155 | javaHome=`expr "$javaHome" : '\(.*\)/bin'` 156 | JAVA_HOME="$javaHome" 157 | export JAVA_HOME 158 | fi 159 | fi 160 | fi 161 | 162 | if [ -z "$JAVACMD" ] ; then 163 | if [ -n "$JAVA_HOME" ] ; then 164 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 165 | # IBM's JDK on AIX uses strange locations for the executables 166 | JAVACMD="$JAVA_HOME/jre/sh/java" 167 | else 168 | JAVACMD="$JAVA_HOME/bin/java" 169 | fi 170 | else 171 | JAVACMD="`which java`" 172 | fi 173 | fi 174 | 175 | if [ ! -x "$JAVACMD" ] ; then 176 | echo "Error: JAVA_HOME is not defined correctly." >&2 177 | echo " We cannot execute $JAVACMD" >&2 178 | exit 1 179 | fi 180 | 181 | if [ -z "$JAVA_HOME" ] ; then 182 | echo "Warning: JAVA_HOME environment variable is not set." 183 | fi 184 | 185 | CLASSWORLDS_LAUNCHER=org.codehaus.plexus.classworlds.launcher.Launcher 186 | 187 | # For Cygwin, switch paths to Windows format before running java 188 | if $cygwin; then 189 | [ -n "$M2_HOME" ] && 190 | M2_HOME=`cygpath --path --windows "$M2_HOME"` 191 | [ -n "$JAVA_HOME" ] && 192 | JAVA_HOME=`cygpath --path --windows "$JAVA_HOME"` 193 | [ -n "$CLASSPATH" ] && 194 | CLASSPATH=`cygpath --path --windows "$CLASSPATH"` 195 | fi 196 | 197 | # traverses directory structure from process work directory to filesystem root 198 | # first directory with .mvn subdirectory is considered project base directory 199 | find_maven_basedir() { 200 | local basedir=$(pwd) 201 | local wdir=$(pwd) 202 | while [ "$wdir" != '/' ] ; do 203 | if [ -d "$wdir"/.mvn ] ; then 204 | basedir=$wdir 205 | break 206 | fi 207 | wdir=$(cd "$wdir/.."; pwd) 208 | done 209 | echo "${basedir}" 210 | } 211 | 212 | # concatenates all lines of a file 213 | concat_lines() { 214 | if [ -f "$1" ]; then 215 | echo "$(tr -s '\n' ' ' < "$1")" 216 | fi 217 | } 218 | 219 | export MAVEN_PROJECTBASEDIR=${MAVEN_BASEDIR:-$(find_maven_basedir)} 220 | MAVEN_OPTS="$(concat_lines "$MAVEN_PROJECTBASEDIR/.mvn/jvm.config") $MAVEN_OPTS" 221 | 222 | # Provide a "standardized" way to retrieve the CLI args that will 223 | # work with both Windows and non-Windows executions. 224 | MAVEN_CMD_LINE_ARGS="$MAVEN_CONFIG $@" 225 | export MAVEN_CMD_LINE_ARGS 226 | 227 | WRAPPER_LAUNCHER=org.apache.maven.wrapper.MavenWrapperMain 228 | 229 | exec "$JAVACMD" \ 230 | $MAVEN_OPTS \ 231 | -classpath "$MAVEN_PROJECTBASEDIR/.mvn/wrapper/maven-wrapper.jar" \ 232 | "-Dmaven.home=${M2_HOME}" "-Dmaven.multiModuleProjectDirectory=${MAVEN_PROJECTBASEDIR}" \ 233 | ${WRAPPER_LAUNCHER} "$@" 234 | -------------------------------------------------------------------------------- /mvnw.cmd: -------------------------------------------------------------------------------- 1 | @REM ---------------------------------------------------------------------------- 2 | @REM Licensed to the Apache Software Foundation (ASF) under one 3 | @REM or more contributor license agreements. See the NOTICE file 4 | @REM distributed with this work for additional information 5 | @REM regarding copyright ownership. The ASF licenses this file 6 | @REM to you under the Apache License, Version 2.0 (the 7 | @REM "License"); you may not use this file except in compliance 8 | @REM with the License. You may obtain a copy of the License at 9 | @REM 10 | @REM http://www.apache.org/licenses/LICENSE-2.0 11 | @REM 12 | @REM Unless required by applicable law or agreed to in writing, 13 | @REM software distributed under the License is distributed on an 14 | @REM "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 | @REM KIND, either express or implied. See the License for the 16 | @REM specific language governing permissions and limitations 17 | @REM under the License. 18 | @REM ---------------------------------------------------------------------------- 19 | 20 | @REM ---------------------------------------------------------------------------- 21 | @REM Maven2 Start Up Batch script 22 | @REM 23 | @REM Required ENV vars: 24 | @REM JAVA_HOME - location of a JDK home dir 25 | @REM 26 | @REM Optional ENV vars 27 | @REM M2_HOME - location of maven2's installed home dir 28 | @REM MAVEN_BATCH_ECHO - set to 'on' to enable the echoing of the batch commands 29 | @REM MAVEN_BATCH_PAUSE - set to 'on' to wait for a key stroke before ending 30 | @REM MAVEN_OPTS - parameters passed to the Java VM when running Maven 31 | @REM e.g. to debug Maven itself, use 32 | @REM set MAVEN_OPTS=-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8000 33 | @REM MAVEN_SKIP_RC - flag to disable loading of mavenrc files 34 | @REM ---------------------------------------------------------------------------- 35 | 36 | @REM Begin all REM lines with '@' in case MAVEN_BATCH_ECHO is 'on' 37 | @echo off 38 | @REM enable echoing my setting MAVEN_BATCH_ECHO to 'on' 39 | @if "%MAVEN_BATCH_ECHO%" == "on" echo %MAVEN_BATCH_ECHO% 40 | 41 | @REM set %HOME% to equivalent of $HOME 42 | if "%HOME%" == "" (set "HOME=%HOMEDRIVE%%HOMEPATH%") 43 | 44 | @REM Execute a user defined script before this one 45 | if not "%MAVEN_SKIP_RC%" == "" goto skipRcPre 46 | @REM check for pre script, once with legacy .bat ending and once with .cmd ending 47 | if exist "%HOME%\mavenrc_pre.bat" call "%HOME%\mavenrc_pre.bat" 48 | if exist "%HOME%\mavenrc_pre.cmd" call "%HOME%\mavenrc_pre.cmd" 49 | :skipRcPre 50 | 51 | @setlocal 52 | 53 | set ERROR_CODE=0 54 | 55 | @REM To isolate internal variables from possible post scripts, we use another setlocal 56 | @setlocal 57 | 58 | @REM ==== START VALIDATION ==== 59 | if not "%JAVA_HOME%" == "" goto OkJHome 60 | 61 | echo. 62 | echo Error: JAVA_HOME not found in your environment. >&2 63 | echo Please set the JAVA_HOME variable in your environment to match the >&2 64 | echo location of your Java installation. >&2 65 | echo. 66 | goto error 67 | 68 | :OkJHome 69 | if exist "%JAVA_HOME%\bin\java.exe" goto init 70 | 71 | echo. 72 | echo Error: JAVA_HOME is set to an invalid directory. >&2 73 | echo JAVA_HOME = "%JAVA_HOME%" >&2 74 | echo Please set the JAVA_HOME variable in your environment to match the >&2 75 | echo location of your Java installation. >&2 76 | echo. 77 | goto error 78 | 79 | @REM ==== END VALIDATION ==== 80 | 81 | :init 82 | 83 | set MAVEN_CMD_LINE_ARGS=%* 84 | 85 | @REM Find the project base dir, i.e. the directory that contains the folder ".mvn". 86 | @REM Fallback to current working directory if not found. 87 | 88 | set MAVEN_PROJECTBASEDIR=%MAVEN_BASEDIR% 89 | IF NOT "%MAVEN_PROJECTBASEDIR%"=="" goto endDetectBaseDir 90 | 91 | set EXEC_DIR=%CD% 92 | set WDIR=%EXEC_DIR% 93 | :findBaseDir 94 | IF EXIST "%WDIR%"\.mvn goto baseDirFound 95 | cd .. 96 | IF "%WDIR%"=="%CD%" goto baseDirNotFound 97 | set WDIR=%CD% 98 | goto findBaseDir 99 | 100 | :baseDirFound 101 | set MAVEN_PROJECTBASEDIR=%WDIR% 102 | cd "%EXEC_DIR%" 103 | goto endDetectBaseDir 104 | 105 | :baseDirNotFound 106 | set MAVEN_PROJECTBASEDIR=%EXEC_DIR% 107 | cd "%EXEC_DIR%" 108 | 109 | :endDetectBaseDir 110 | 111 | IF NOT EXIST "%MAVEN_PROJECTBASEDIR%\.mvn\jvm.config" goto endReadAdditionalConfig 112 | 113 | @setlocal EnableExtensions EnableDelayedExpansion 114 | for /F "usebackq delims=" %%a in ("%MAVEN_PROJECTBASEDIR%\.mvn\jvm.config") do set JVM_CONFIG_MAVEN_PROPS=!JVM_CONFIG_MAVEN_PROPS! %%a 115 | @endlocal & set JVM_CONFIG_MAVEN_PROPS=%JVM_CONFIG_MAVEN_PROPS% 116 | 117 | :endReadAdditionalConfig 118 | 119 | SET MAVEN_JAVA_EXE="%JAVA_HOME%\bin\java.exe" 120 | 121 | set WRAPPER_JAR="".\.mvn\wrapper\maven-wrapper.jar"" 122 | set WRAPPER_LAUNCHER=org.apache.maven.wrapper.MavenWrapperMain 123 | 124 | %MAVEN_JAVA_EXE% %JVM_CONFIG_MAVEN_PROPS% %MAVEN_OPTS% %MAVEN_DEBUG_OPTS% -classpath %WRAPPER_JAR% "-Dmaven.multiModuleProjectDirectory=%MAVEN_PROJECTBASEDIR%" %WRAPPER_LAUNCHER% %MAVEN_CMD_LINE_ARGS% 125 | if ERRORLEVEL 1 goto error 126 | goto end 127 | 128 | :error 129 | set ERROR_CODE=1 130 | 131 | :end 132 | @endlocal & set ERROR_CODE=%ERROR_CODE% 133 | 134 | if not "%MAVEN_SKIP_RC%" == "" goto skipRcPost 135 | @REM check for post script, once with legacy .bat ending and once with .cmd ending 136 | if exist "%HOME%\mavenrc_post.bat" call "%HOME%\mavenrc_post.bat" 137 | if exist "%HOME%\mavenrc_post.cmd" call "%HOME%\mavenrc_post.cmd" 138 | :skipRcPost 139 | 140 | @REM pause the script if MAVEN_BATCH_PAUSE is set to 'on' 141 | if "%MAVEN_BATCH_PAUSE%" == "on" pause 142 | 143 | if "%MAVEN_TERMINATE_CMD%" == "on" exit %ERROR_CODE% 144 | 145 | exit /B %ERROR_CODE% -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | com.spring 7 | springboot 8 | 0.0.1-SNAPSHOT 9 | jar 10 | 11 | Spring-Boot-freemarker1 12 | Demo project for Spring Boot 13 | 14 | 15 | org.springframework.boot 16 | spring-boot-starter-parent 17 | 1.5.1.RELEASE 18 | 19 | 20 | 21 | 22 | UTF-8 23 | UTF-8 24 | 1.8 25 | 26 | 27 | 28 | 29 | org.springframework.boot 30 | spring-boot-starter-freemarker 31 | 32 | 33 | org.springframework.boot 34 | spring-boot-devtools 35 | runtime 36 | 37 | 38 | org.springframework.boot 39 | spring-boot-starter-web 40 | 41 | 42 | 43 | org.springframework.boot 44 | spring-boot-starter-test 45 | test 46 | 47 | 48 | org.springframework.boot 49 | spring-boot-starter-tomcat 50 | provided 51 | 52 | 53 | 54 | 55 | 56 | 57 | org.springframework.boot 58 | spring-boot-maven-plugin 59 | 60 | 61 | 62 | 63 | 64 | 65 | -------------------------------------------------------------------------------- /src/main/java/com/spring/SpringBootFreemarker1Application.java: -------------------------------------------------------------------------------- 1 | package com.spring; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.EnableAutoConfiguration; 5 | import org.springframework.boot.autoconfigure.SpringBootApplication; 6 | import org.springframework.context.annotation.ComponentScan; 7 | import org.springframework.context.annotation.Configuration; 8 | 9 | @SpringBootApplication 10 | @Configuration 11 | @EnableAutoConfiguration 12 | @ComponentScan(basePackages={"com.spring"}) 13 | public class SpringBootFreemarker1Application { 14 | 15 | public static void main(String[] args) { 16 | SpringApplication.run(SpringBootFreemarker1Application.class, args); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /src/main/java/com/spring/controller/FreemarkerController.java: -------------------------------------------------------------------------------- 1 | package com.spring.controller; 2 | 3 | import org.springframework.stereotype.Controller; 4 | import org.springframework.web.bind.annotation.RequestMapping; 5 | import org.springframework.web.servlet.ModelAndView; 6 | 7 | @Controller 8 | @RequestMapping("/") 9 | public class FreemarkerController { 10 | 11 | @RequestMapping(value="/index") 12 | public ModelAndView index(){ 13 | ModelAndView mv = new ModelAndView(); 14 | mv.addObject("username","你好!Freemarker!"); 15 | return mv; 16 | } 17 | 18 | @RequestMapping(value="/free6") 19 | public ModelAndView free6(){ 20 | ModelAndView mv6 = new ModelAndView(); 21 | return mv6; 22 | } 23 | 24 | @RequestMapping(value="/free7") 25 | public ModelAndView free7(){ 26 | ModelAndView mv7 = new ModelAndView(); 27 | return mv7; 28 | } 29 | 30 | } 31 | 32 | //http://localhost:8080/index 33 | -------------------------------------------------------------------------------- /src/main/java/com/spring/controller/FreemarkerController1.java: -------------------------------------------------------------------------------- 1 | package com.spring.controller; 2 | 3 | import java.util.Date; 4 | 5 | import org.springframework.stereotype.Controller; 6 | import org.springframework.web.bind.annotation.RequestMapping; 7 | import org.springframework.web.servlet.ModelAndView; 8 | 9 | @Controller 10 | @RequestMapping("/") 11 | public class FreemarkerController1 { 12 | 13 | //freemarker取值,插值 14 | @RequestMapping("/free1") 15 | public ModelAndView free1(){ 16 | ModelAndView mv1 = new ModelAndView(); 17 | mv1.addObject("intVar",100); 18 | mv1.addObject("LongVar",10000000000000000L); 19 | mv1.addObject("doubleVar",3.141592675d); 20 | mv1.addObject("StringVar","我是freemarker,是字符串!"); 21 | mv1.addObject("booleanVar", true); 22 | mv1.addObject("dateVar1",new Date()); 23 | mv1.addObject("nullVar1",null); 24 | mv1.addObject("nullVar","我是空"); 25 | return mv1; 26 | } 27 | 28 | } 29 | 30 | //http://localhost:8080/free1 31 | -------------------------------------------------------------------------------- /src/main/java/com/spring/controller/FreemarkerController2.java: -------------------------------------------------------------------------------- 1 | package com.spring.controller; 2 | 3 | import java.util.ArrayList; 4 | import java.util.HashMap; 5 | import java.util.List; 6 | import java.util.Map; 7 | 8 | import org.springframework.stereotype.Controller; 9 | import org.springframework.web.bind.annotation.RequestMapping; 10 | import org.springframework.web.servlet.ModelAndView; 11 | 12 | @Controller 13 | @RequestMapping("/") 14 | public class FreemarkerController2 { 15 | 16 | @RequestMapping("/free2") 17 | public ModelAndView free2(){ 18 | ModelAndView mv2 = new ModelAndView(); 19 | List list = new ArrayList(); 20 | list.add("java"); 21 | list.add("JavaScript"); 22 | list.add("python"); 23 | list.add("php"); 24 | list.add("Html"); 25 | mv2.addObject("myList",list); 26 | return mv2; 27 | } 28 | 29 | } 30 | 31 | 32 | //http://localhost:8080/free2 -------------------------------------------------------------------------------- /src/main/java/com/spring/controller/FreemarkerController3.java: -------------------------------------------------------------------------------- 1 | package com.spring.controller; 2 | 3 | import java.util.HashMap; 4 | import java.util.Map; 5 | 6 | import org.springframework.stereotype.Controller; 7 | import org.springframework.web.bind.annotation.RequestMapping; 8 | import org.springframework.web.servlet.ModelAndView; 9 | 10 | @Controller 11 | @RequestMapping("/") 12 | public class FreemarkerController3 { 13 | @RequestMapping(value="/free3") 14 | public ModelAndView free3(){ 15 | ModelAndView mv3 = new ModelAndView(); 16 | Map map = new HashMap(); 17 | map.put("Java","你好Java"); 18 | map.put("address","北京"); 19 | map.put("身高",172); 20 | map.put("money", 100.5); 21 | mv3.addObject("map",map); 22 | return mv3; 23 | } 24 | 25 | } 26 | 27 | //http://localhost:8080/free3 -------------------------------------------------------------------------------- /src/main/java/com/spring/controller/FreemarkerController4.java: -------------------------------------------------------------------------------- 1 | package com.spring.controller; 2 | 3 | import org.springframework.stereotype.Controller; 4 | import org.springframework.web.bind.annotation.RequestMapping; 5 | import org.springframework.web.servlet.ModelAndView; 6 | 7 | import com.spring.method.SortMethod; 8 | 9 | @Controller 10 | @RequestMapping("/") 11 | public class FreemarkerController4 { 12 | 13 | @RequestMapping("/free4") 14 | public ModelAndView free4(){ 15 | ModelAndView mv4 = new ModelAndView(); 16 | mv4.addObject("sort_int",new SortMethod()); 17 | return mv4; 18 | } 19 | 20 | } 21 | 22 | 23 | //http://localhost:8080/free4 24 | -------------------------------------------------------------------------------- /src/main/java/com/spring/controller/FreemarkerController5.java: -------------------------------------------------------------------------------- 1 | package com.spring.controller; 2 | 3 | import org.springframework.stereotype.Controller; 4 | import org.springframework.web.bind.annotation.RequestMapping; 5 | import org.springframework.web.servlet.ModelAndView; 6 | 7 | @Controller 8 | @RequestMapping("/") 9 | public class FreemarkerController5 { 10 | 11 | @RequestMapping("/free5") 12 | public ModelAndView free5(){ 13 | ModelAndView mv5 = new ModelAndView("free5"); 14 | return mv5; 15 | } 16 | 17 | } 18 | 19 | //本例子不成功,报错,原因是我没有配置自定义指令的xml文件。 20 | -------------------------------------------------------------------------------- /src/main/java/com/spring/method/RoleDirectiveModel.java: -------------------------------------------------------------------------------- 1 | package com.spring.method; 2 | 3 | import java.io.IOException; 4 | import java.util.ArrayList; 5 | import java.util.List; 6 | import java.util.Map; 7 | 8 | import org.springframework.stereotype.Service; 9 | 10 | import freemarker.core.Environment; 11 | import freemarker.template.SimpleSequence; 12 | import freemarker.template.TemplateBooleanModel; 13 | import freemarker.template.TemplateDirectiveBody; 14 | import freemarker.template.TemplateDirectiveModel; 15 | import freemarker.template.TemplateException; 16 | import freemarker.template.TemplateModel; 17 | import freemarker.template.TemplateScalarModel; 18 | 19 | @Service 20 | public class RoleDirectiveModel implements TemplateDirectiveModel{ 21 | 22 | /* 23 | *env:环境变量 24 | *params:指令参数(存储你所需要的值,随便是什么key-Value你懂的) 25 | *loopVars:循环变量 26 | *body:指令内容 27 | *除了params外,其他的都使用null 28 | */ 29 | @Override 30 | public void execute(Environment env, Map params, TemplateModel[] loopVars, TemplateDirectiveBody body) 31 | throws TemplateException, IOException { 32 | System.out.println("自定义指令需要实现TemplateDirectiveModel接口"); 33 | //获取参数 34 | TemplateScalarModel user = (TemplateScalarModel) params.get("user"); 35 | TemplateScalarModel role = (TemplateScalarModel) params.get("role"); 36 | 37 | if("123456".equals(user.getAsString()) && "admin".equals(role.getAsString())){ 38 | //用户id 39 | loopVars[0] = TemplateBooleanModel.TRUE; 40 | } 41 | 42 | //创建用户的权限 43 | List otherRights = new ArrayList(); 44 | otherRights.add("add"); 45 | otherRights.add("delete"); 46 | otherRights.add("update"); 47 | loopVars[1] = new SimpleSequence(otherRights); 48 | 49 | body.render(env.getOut()); 50 | } 51 | 52 | } 53 | -------------------------------------------------------------------------------- /src/main/java/com/spring/method/SortMethod.java: -------------------------------------------------------------------------------- 1 | package com.spring.method; 2 | 3 | import java.math.BigDecimal; 4 | import java.util.Collections; 5 | import java.util.Comparator; 6 | import java.util.List; 7 | 8 | import freemarker.template.SimpleSequence; 9 | import freemarker.template.TemplateMethodModelEx; 10 | import freemarker.template.TemplateModelException; 11 | /** 12 | * 实现排序 freemarker要实现TemplateMethodModelEx接口 13 | * @author dell 14 | * 15 | */ 16 | public class SortMethod implements TemplateMethodModelEx{ 17 | 18 | @Override 19 | public Object exec(List arguments) throws TemplateModelException { 20 | //获取第一个参数 21 | SimpleSequence arg0 = (SimpleSequence)arguments.get(0); 22 | List list = arg0.toList(); 23 | //Comparator接口 24 | Collections.sort(list,new Comparator(){ 25 | 26 | @Override 27 | public int compare(BigDecimal o1, BigDecimal o2) { 28 | 29 | return o1.intValue() - o2.intValue(); //升序 30 | } 31 | 32 | }); 33 | return list; 34 | } 35 | 36 | } 37 | -------------------------------------------------------------------------------- /src/main/java/项目说明: -------------------------------------------------------------------------------- 1 | 1.@ComponentScan(basePackages={"com.spring"}) 这个注解的意思是通过spring容 2 | 器去加载以com.spring开头的包下面的内容。 3 | 4 | 2.<#assign var="hello spring boot"/> 5 | freemarker标签用于声明一个变量 6 | 7 | 3.@Configuration注解会默认加载resources下的配置文件。 8 | 9 | 4.freemarker不支持boolean类型的true/false。输出是会抛出异常! 10 | 但是可以转化:布尔值: ${booleanVar?string('yes','no')} 11 | 12 | 13 | 5.<#if myList??> 表示判断myList变量是不是存在或是不是空值 14 | 等同于<#if myList?exists> 15 | 16 | 6.freemarker内建指令以#开始,自定义指令以@开始 17 | <@role user='123456' role='admin';result1,result2> 18 | user用户id,role用户权限 19 | 20 | 7.Freemarker的内建函数就是直接可以使用的函数,是freemarker封装好的函数。 21 | 有处理字符串,数字,list的内建函数。 22 | 处理字符串内建函数: 23 | substring 字符串的截取 24 | cap_first 首字母变大写 25 | ends_with 以...结尾的函数 26 | contains 字符串是否包含目标字符串的函数 27 | 如何把一个字符串转化为date 、datetime 、time类型的函数 date datetime time; 28 | starts_with 字符串以...开始 29 | index_of 字符串所在的位置 30 | last_index_of 字符串最后所在的位置 31 | split 分割字符串 32 | trim 去掉字符串两头的空格 33 | 等函数 34 | 35 | 处理数字的内建函数 36 | string x?string("0.##") 对数字进行格式化 37 | round 四舍五入 38 | floor 把小数点去掉 39 | ceiling 数字进1,变成整数 40 | 41 | 处理list内建函数: 42 | first 取list第一个值 43 | last 取list最后一个值 44 | seq_contains 这个序列是否包含 45 | seq_index_of 这个序列所在的位置 46 | size list长度 47 | reverse 倒序 48 | sort 升序排序 49 | sort_by 根据属性排序 50 | trunk 把字符串分块处理 51 | 52 | 其他内建函数 53 | is函数:判断变量的类型 54 | is_string 字符串 55 | is_number 整数 56 | is_method 方法 57 | 58 | () 对变量进行判断 59 | hs_content 判断对象是否是空值,是不是有内容 60 | eval 求值函数 61 | 62 | 8.<#nested param1,"我的nested参数"> 用来定义模板片段 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | -------------------------------------------------------------------------------- /src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | #\u670d\u52a1\u7aef\u53e3 2 | server.port=8080 3 | #freemarker\u914d\u7f6e 4 | spring.freemarker.cache=false 5 | spring.freemarker.checkTemplateLocation=true 6 | spring.freemarker.contentType=text/html 7 | spring.freemarker.suffix=.html 8 | spring.freemarker.templateEncoding=UTF-8 9 | spring.freemarker.templateLoaderPath=classpath:/templates/ -------------------------------------------------------------------------------- /src/main/resources/doc/1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JavaCodeMood/freemarker/90ccb6ae60ebb598efcefa9651affe9dee977b94/src/main/resources/doc/1.png -------------------------------------------------------------------------------- /src/main/resources/doc/Freemarker快速入门.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JavaCodeMood/freemarker/90ccb6ae60ebb598efcefa9651affe9dee977b94/src/main/resources/doc/Freemarker快速入门.pdf -------------------------------------------------------------------------------- /src/main/resources/doc/freemarker中文手册.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JavaCodeMood/freemarker/90ccb6ae60ebb598efcefa9651affe9dee977b94/src/main/resources/doc/freemarker中文手册.pdf -------------------------------------------------------------------------------- /src/main/resources/doc/不错的FreeMarker教程.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JavaCodeMood/freemarker/90ccb6ae60ebb598efcefa9651affe9dee977b94/src/main/resources/doc/不错的FreeMarker教程.pdf -------------------------------------------------------------------------------- /src/main/resources/templates/free1.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | freemarker 6 | 7 | 8 |

取值:

9 |
    10 |
  • 整数:${intVar}
  • 11 |
  • 长整数:${LongVar}
  • 12 |
  • 双精度:${doubleVar}
  • 13 |
  • 字符串:${StringVar}
  • 14 |
  • 布尔值: ${booleanVar?string('yes','no')}
  • 15 |
  • 日期类型Java.Util.Date:${dateVar1?string('yyyy-MM-dd HH:mm:ss')}
  • 16 |
  • 空值:${nullVar1!}
  • 17 |
  • 空值:${nullVar!'我是默认值'}
  • 18 |
  • 不存在的变量:${变量!'我是默认变量'}
  • 19 |
  • 不存在的变量1:${free!'我是默认变量'}
  • 20 |
21 |
22 |
    23 |
  • 赋值&运算
  • 24 | <#assign a=100/> 25 | a = ${a}
    26 | a+100=${a+100}
    27 |
28 | 29 | -------------------------------------------------------------------------------- /src/main/resources/templates/free2.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | freemarker 6 | 11 | 12 | 13 |

freemarker实现遍历list集合

14 |
    15 |
  • 集合list
  • 16 | <#list myList as item> 17 | ${item}
    18 | 19 |
20 | 21 | 22 |
23 |

if表达式

24 |
    25 |
  • if
  • 26 | <#assign var = 99/> 27 | <#if var == 99> 28 | var=99 29 | 30 |
    31 | 32 | <#if var == 99> 33 | var=99 34 | <#else> 35 | var!=99 36 | 37 |
    38 | 39 | <#if var > 99> 40 | var大于99 41 | <#elseif var==99> 42 | var=99 43 | <#else> 44 | var小于99 45 | 46 |
    47 | 48 |
  • if多条件 ||,&&,!
  • 49 | <#assign var='python'> 50 | <#if var =='python' || var =='Java'> 51 | python or java
    52 | 53 | 54 | <#if var =='python' && var?length == 6> 55 | python length 6
    56 | 57 | 58 | <#if (var =='python' && var?length == 6) || (var == 'java')> 59 | python length 6或者Java

    60 | 61 | 62 | <#if !((var =='python' && var?length == 6) || (var == 'java'))> 63 | python length 6或者Java

    64 | 65 | 66 |
67 | 68 | -------------------------------------------------------------------------------- /src/main/resources/templates/free3.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | freemarker 6 | 11 | 12 | 13 |

freemarker遍历map集合

14 |
    15 |
  • 集合map
  • 16 | <#list map?keys as key> 17 | ${key}:${map[key]}
    18 | 19 |
20 |
21 |

switch语句

22 |
    23 |
  • switch,case,break,default
  • 24 | <#assign var = 8/> 25 | <#switch var> 26 | <#case 1>星期一 <#break> 27 | <#case 2>星期二 <#break> 28 | <#case 3>星期三 <#break> 29 | <#case 4>星期四 <#break> 30 | <#case 5>星期五 <#break> 31 | <#case 6>星期六 <#break> 32 | <#case 7>星期日 <#break> 33 | <#default>无效的星期 34 | 35 |
36 |
37 |
    38 |
  • switch,case,break,default
  • 39 | <#assign var = 'java'/> 40 | <#switch var> 41 | <#case 'java'>我是java <#break> 42 | <#case 'python'>我是python <#break> 43 | <#case 'C'>我是C语言 <#break> 44 | <#default>我是谁?谁是我? 45 | 46 |
47 | 48 | -------------------------------------------------------------------------------- /src/main/resources/templates/free4.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | freemarker 6 | 7 | 8 |

if表达式

9 |
    10 |
  • if
  • 11 | <#assign var = 99/> 12 | <#if var == 99> 13 | var=99 14 | 15 |
    16 | 17 | <#if var == 99> 18 | var=99 19 | <#else> 20 | var!=99 21 | 22 |
    23 | 24 | <#if var > 99> 25 | var大于99 26 | <#elseif var==99> 27 | var=99 28 | <#else> 29 | var小于99 30 | 31 |
    32 | 33 |
  • if多条件 ||,&&,!
  • 34 | <#assign var='python'> 35 | <#if var =='python' || var =='Java'> 36 | python or java
    37 | 38 | 39 | <#if var =='python' && var?length == 6> 40 | python length 6
    41 | 42 | 43 | <#if (var =='python' && var?length == 6) || (var == 'java')> 44 | python length 6或者Java

    45 | 46 | 47 | <#if !((var =='python' && var?length == 6) || (var == 'java'))> 48 | python length 6或者Java

    49 | 50 |
51 |
52 | 53 |

自定义函数

54 |

1.自定义函数(整数排序sort_int)

55 |
    56 | <#assign myList=[2,5,9,1,0,6,3,1,10,4]/> 57 |
  • 未排序
  • 58 | <#list myList as item> 59 | ${item}, 60 | 61 |
    62 |
  • 排序
  • 63 | <#list sort_int(myList) as item> 64 | ${item}, 65 | 66 |
67 |
68 | 69 |

List的指令

70 |

1,list常用指令sort升序

71 |
    72 | <#assign myList=[0,3,7,9,1,4,8,6,2,5]/> 73 | <#list myList?sort as item> 74 | ${item_index}:${item}
    75 | 76 | 77 |
    78 |
79 |
80 | 81 |

List的指令

82 |

2,list常用指令resverse倒序

83 |
    84 | <#assign myList1=[0,3,7,9,1,4,8,6,2,5]/> 85 | <#list myList1?sort?reverse as item> 86 | ${item_index}:${item}
    87 | 88 | 长度:${myList1?size}
    89 | 下标取值:${myList1[3]}
    90 |
    91 |
92 | 93 | -------------------------------------------------------------------------------- /src/main/resources/templates/free5.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Insert title here 6 | 7 | 8 |

自定义指令

9 |

1.用户123456 是否拥有admin角色,并且返回admin的权限

10 |
    11 | <@role user='123456' role='admin';result1,result2> 12 | <#if result1> 13 | 我的角色是admin
    14 | 15 | 我拥有的权限是: 16 | 17 | <#list result2 as item> 18 | ${item}, 19 | 20 |
    21 | 22 |
23 | 24 | -------------------------------------------------------------------------------- /src/main/resources/templates/free6.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | freemarker内建函数 6 | 7 | 8 |

内建函数

9 |
    10 |

    1,字符串内建函数

    11 | <#list "a|b|c|d"?split("|") as item> 12 |
  • ${item}
  • 13 | 14 |
15 |
16 | 17 |

字符串转日期

18 |
    19 | <#assign var1="23/02/2017" ?date("MM/dd/yyyy")/> 20 | <#assign var2="11:34:33" ?time("HH:mm:ss")/> 21 | <#assign var3="2017-02-23 11:35 PM" ?datetime("yyyy-MM-dd hh:mm")/> 22 |
  • ${var1}
  • 23 |
  • ${var2}
  • 24 |
  • ${var3}
  • 25 |
26 |
27 | 28 |

数字类型的内建函数

29 |
    30 | <#assign numVar1=314.5662789 /> 31 |
  • 格式化:${numVar1?string("0.##")}
  • 32 |
  • 四舍五入:${numVar1?round}
  • 33 |
  • 去掉小数点:${numVar1?floor}
  • 34 |
  • 进1:${numVar1?ceiling}
  • 35 |
36 |
37 | 38 |

list内建函数

39 |
    40 | <#assign listVar1=[1,2,3,4,11,12,13,14,21,22,23,24] /> 41 | 42 |
  • 分块处理取长度:${listVar1?chunk(4)?size}
  • 43 |
  • 长度:${listVar1?size}
  • 44 | <#list listVar1?chunk(4)?last as item> 45 |
  • ${item}
  • 46 | 47 |
  • 取list第一个值:${listVar1?first}
  • 48 |
  • 取list最后一个值:${listVar1?last}
  • 49 |
50 |
51 | 52 |

其他内建函数

53 |
    54 | <#assign sVar = 'helloworld'/> 55 |
  • ${sVar?is_number?string('yes','no')}
  • 56 |
  • ${sVar?has_content?string('yes','no')}
  • 57 |
  • ${"1"+"2"?eval}
  • 58 |
  • ${(1+2)?eval}
  • 59 |
  • ${"1"+"2"?is_string?string('yes','no')}
  • 60 |
  • ${("1"+"2")?is_string?string('yes','no')}
  • 61 |
62 | 63 | -------------------------------------------------------------------------------- /src/main/resources/templates/free7.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Insert title here 6 | 7 | 8 |

macro,nested,return:实战demo

9 |

1.macro:宏指令

10 |
    11 |
  • FreeMarker:无参数的macro
  • 12 |
    13 | <#macro test> 14 | 我是无参数的macro 15 | 16 | <@test/> 17 |
    18 |
  • Freemarker2:有参数的macro
  • 19 |
    20 | <#macro test param1 param2> 21 | 我是有参数的macro,paeam1=${param1},param2=${param2} 22 |
    23 | 24 | <@test param1="java" param2="python"/> 25 |
    26 |
  • Freemarker3:有参数的macro
  • 27 |
    28 | <#macro test param1 param2="JavaScript"> 29 | 我是有参数的macro,paeam1=${param1},param2=${param2} 30 |
    31 | 32 | <@test param1="java" param2="hello python"/> 33 |
    34 |
  • Freemarker4:有多个参数的macro
  • 35 |
    36 | <#macro test param1 param2="python" paramExt...> 37 | 我是有参数的macro,paeam1=${param1},param2=${param2} 38 |
    39 | ${paramExt['param3']} 40 | ${paramExt['param4']} 41 | 42 | <@test param1="java" param2="python" param3="nodejs" param4="html"/> 43 |
    44 |
45 |
46 |

2,nested

47 |
48 |
    49 | <#macro test param1="java"> 50 | ${param1}
    51 | <#nested param1,"我的nested参数">
    52 | 53 | 54 |
  • 调用
  • 55 |
    56 | <@test param1="java";loopVar1,loopVar2> 57 | hello ${loopVar1},${loopVar2}
    58 | 59 | 60 | <@test param1="python"; loopVar1> 61 | hello ${loopVar1}
    62 | 63 |
    64 |
65 |
66 |
67 |

3,函数

68 |
    69 | <#function doAdd param1 param2> 70 | <#return param1+param2> 71 | 72 |
  • 调用
  • 73 |
    你好,我是调用${doAdd(100,100)}
    74 |
75 |
76 |
77 | 78 | -------------------------------------------------------------------------------- /src/main/resources/templates/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | freemarker 6 | 12 | 13 | 14 |

${username}

15 | <#assign var="hello spring boot 学习你很开心!" /> 16 | ${var} 17 |
18 | 19 |

字符串

20 |

字符串常用内建函数

21 |
    22 | <#assign a='hello'> <#assign b='world!'> 23 |
  • 连接
  • 24 | ${a+b} 25 |
    26 |
  • 截取
  • 27 | ${(a+b)?substring(5,8)} 28 |
    29 |
  • 长度
  • 30 | ${(a+b)?length} 31 |
    32 |
  • 大写
  • 33 | ${(a+b)?upper_case} 34 |
    35 |
  • 小写
  • 36 | ${(a+b)?lower_case} 37 |
    38 |
  • index_of
  • 39 | ${(a+b)?index_of('w')} 40 |
    41 |
  • replace
  • 42 | ${(a+b)?replace('o','abc')} 43 |
    44 |
  • last_index_of
  • 45 | ${(a+b)?last_index_of('o')} 46 |
    47 |
48 | 49 | -------------------------------------------------------------------------------- /src/test/java/com/spring/SpringBootFreemarker1ApplicationTests.java: -------------------------------------------------------------------------------- 1 | package com.spring; 2 | 3 | import org.junit.Test; 4 | import org.junit.runner.RunWith; 5 | import org.springframework.boot.test.context.SpringBootTest; 6 | import org.springframework.test.context.junit4.SpringRunner; 7 | 8 | @RunWith(SpringRunner.class) 9 | @SpringBootTest 10 | public class SpringBootFreemarker1ApplicationTests { 11 | 12 | @Test 13 | public void contextLoads() { 14 | } 15 | 16 | } 17 | --------------------------------------------------------------------------------