1.freemarker
1.1 pom.xml 引入包
<!-- 模板引擎 freemarker 依賴 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-freemarker</artifactId> </dependency>
1.2 application.properties
#Servlet端口號
server.port=8088
## FREEMARKER (FreeMarkerAutoConfiguration)
##Set whether HttpServletRequest attributes are allowed to override (hide) controller generated model attributes of the same name.
spring.freemarker.allow-request-override=false
#
## Set whether HttpSession attributes are allowed to override (hide) controller generated model attributes of the same name.
spring.freemarker.allow-session-override=false
#
## Enable template caching.
spring.freemarker.cache=false
#
## Template encoding.
spring.freemarker.charset=UTF-8
#
## Check that the templates location exists.
spring.freemarker.check-template-location=true
## Content-Type value.
spring.freemarker.content-type=text/html
#
## Enable MVC view resolution for this technology.
spring.freemarker.enabled=true
#
## Set whether all request attributes should be added to the model prior to merging with the template.
spring.freemarker.expose-request-attributes=false
#
## Set whether all HttpSession attributes should be added to the model prior to merging with the template.
#spring.freemarker.expose-session-attributes=false
#
## Set whether to expose a RequestContext for use by Spring's macro library, under the name "springMacroRequestContext".
#spring.freemarker.expose-spring-macro-helpers=true
#
## Prefer file system access for template loading. File system access enables hot detection of template changes.
#spring.freemarker.prefer-file-system-access=true
#
## Prefix that gets prepended to view names when building a URL.
#包名
spring.freemarker.prefix= default/
#
## Name of the RequestContext attribute for all views.
#spring.freemarker.request-context-attribute=
#
## Well-known FreeMarker keys which will be passed to FreeMarker's Configuration.
#spring.freemarker.settings.*=
#
## Suffix that gets appended to view names when building a URL.
# 文件后綴名
spring.freemarker.suffix=.ftl
#
## Comma-separated list of template paths.
# 存放位置
spring.freemarker.template-loader-path=classpath:/templates/
#
## White list of view names that can be resolved.
#spring.freemarker.view-names=
# 尋址路徑= ${spring.freemarker.template-loader-path}${spring.freemarker.prefix}${returnValue}${spring.freemarker.suffix}
1.3 Application.java
package com.guilf; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; /** * 注:啟動類不要放在main/java 根目錄下,啟動會報錯 */ @SpringBootApplication public class Application { public static void main(String[] args) { SpringApplication.run(Application.class,args); } }
1.4 HelloFreemarkerController.java
package com.guilf.mvc; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import java.util.Map; @Controller public class HelloFreemarkerController { @RequestMapping("/freemarker") public String index(Map<String,Object> map){ map.put("name","guilf"); map.put("Freemarker","Freemarker"); return "index"; } }
1.5 index.ftl
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> hello ${name} in freemarker </body> </html>
2. jsp (這里不一樣,因為jsp還是以前的,所有要建一個web項目,然后會有web.xml文件)
2.1 pom.xml 引包
<!-- servlet 依賴. --> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <scope>provided</scope> </dependency> <!-- JSTL(JSP Standard Tag Library,JSP標准標簽庫)是一個不斷完善的開放源代碼的JSP標簽庫,是由apache的jakarta小組來維護的。 JSTL只能運行在支持JSP1.2和Servlet2.3規范的容器上,如tomcat 4.x。在JSP 2.0中也是作為標准支持的。 不然報異常信息: javax.servlet.ServletException: Circular view path [/helloJsp]: would dispatch back to the current handler URL [/helloJsp] again. Check your ViewResolver setup! (Hint: This may be the result of an unspecified view, due to default view name generation.) --> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> </dependency> <!-- tomcat 的支持.--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> <scope>provided</scope> </dependency> <dependency> <groupId>org.apache.tomcat.embed</groupId> <artifactId>tomcat-embed-jasper</artifactId> <scope>provided</scope> </dependency>
1.2 application.properties
# 頁面默認前綴目錄 spring.mvc.view.prefix=/WEB-INF/jsp/ # 響應頁面默認后綴 spring.mvc.view.suffix=.jsp
1.3 HelloJspController.java
@Controller public class HelloJspController { @RequestMapping("/hello") public String index(Model model){ model.addAttribute("name","hong"); return "index"; } }
jsp
<html> <body> <h2>Hello ${name} in jsp!</h2> </body> </html>
web.xml(沒有內容)
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd" > <web-app> <display-name>Archetype Created Web Application</display-name> </web-app>
3.velovity (spring-boot 版本1.5.x不支持了,如果用要改成1.5以下的)
3.1 pom.xml
<!-- 引入velocity的依賴包--> <!-- spring boot 1.5.x(Spring4.3 ) 默認不再支持,需要手動將spring boot 依賴版本下降到1.4.x左右 測試 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-velocity</artifactId> <version>1.3.6.RELEASE</version> </dependency>
3.2 application.properties
# ## Set whether HttpServletRequest attributes are allowed to override (hide) controller generated model attributes of the same name. spring.velocity.allow-request-override=false # ## Set whether HttpSession attributes are allowed to override (hide) controller generated model attributes of the same name. spring.velocity.allow-session-override=false # ## Enable template caching. spring.velocity.cache=false # ## Template encoding. spring.velocity.charset=UTF-8 # ## Check that the templates location exists. spring.velocity.check-template-location=true # ## Content-Type value. spring.velocity.content-type=text/html # ## Name of the DateTool helper object to expose in the Velocity context of the view. ##spring.velocity.date-tool-attribute= # ## Enable MVC view resolution for this technology. spring.velocity.enabled=true # ## Set whether all request attributes should be added to the model prior to merging with the template. spring.velocity.expose-request-attributes=false # ## Set whether all HttpSession attributes should be added to the model prior to merging with the template. spring.velocity.expose-session-attributes=false # ## Set whether to expose a RequestContext for use by Spring's macro library, under the name "springMacroRequestContext". #spring.velocity.expose-spring-macro-helpers=true # ## Name of the NumberTool helper object to expose in the Velocity context of the view. ##spring.velocity.number-tool-attribute= # ## Prefer file system access for template loading. File system access enables hot detection of template changes. #spring.velocity.prefer-file-system-access=true # ## Prefix that gets prepended to view names when building a URL. spring.velocity.prefix= default/ #spring.velocity.prefix= # ## Additional velocity properties. #spring.velocity.properties.*= # ## Name of the RequestContext attribute for all views. #spring.velocity.request-context-attribute= # ## Template path. spring.velocity.resource-loader-path=classpath:/templates/ #spring.velocity.resource-loader-path=/templates/ # ## Suffix that gets appended to view names when building a URL. spring.velocity.suffix=.vm # ## Velocity Toolbox config location. For instance `/WEB-INF/toolbox.xml` #spring.velocity.toolbox-config-location= # ## White list of view names that can be resolved. #spring.velocity.view-names= logging.level.root=debug # 尋址路徑= ${spring.velocity.resource-loader-path}${spring.velocity.prefix}${returnValue}${spring.velocity.suffix}
3.3 HelloVelocityController.java
package com.guilf.mvc; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; @Controller public class HelloVelocityController { @RequestMapping("/velocity") public String index(Model model){ model.addAttribute("name","guilf"); return "velocity1"; } }
3.4 velocity1.vm
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <p> hello ${name} in velocity</p> </body> </html>