SpringBoot 整合 freemarker
一:用idea 创建 springboot 项目:
详情请参考:《使用IDEA创建一个springboot项目》
二:具体代码内容:
1:代码结构
2:启动项目
3:访问项目
UserController
package com.alancode.springboot.controller; /** * @author Alan_liu * @Create 2021/2/16 21:40 */ import com.alancode.springboot.pojo.Users; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; import java.util.ArrayList; import java.util.List; /** *@description: SpringBoot 整合 jsp *@author: Alan_Liu *@create: 2021-02-16 21:40 */ @Controller /**做view视图 跳转的 所有这里不能使用@RestController ***/ public class UserController { @RequestMapping("/showUserInfo") public String showUser(Model model){ List<Users> list=new ArrayList<>(); list.add(new Users(1,"张三",20)) ; list.add(new Users(2,"李四",20)) ; list.add(new Users(3,"王五",20)) ; list.add(new Users(4,"赵六",20)) ; //需要一个 model 对象 model.addAttribute("list",list); //视图跳转 return "userList"; } }
HelloWorld
package com.alancode.springboot.controller;/** * @author Alan_liu * @Create 2021/2/16 23:33 */ import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import java.util.HashMap; import java.util.Map; /** *@program: SpringBootAddJsp *@description: HelloWorld *@author: Alan_Liu *@create: 2021-02-16 23:33 */ public class HelloWorld { /** * @return * @throws * @Param: [] * @author Alan_Liu * @date 2021/2/14 21:52 **/ @RequestMapping("/hello") @ResponseBody public Map<String, Object> showHelloWolrd() { Map<String, Object> map = new HashMap<String, Object>(); map.put("msg", "HelloWorld"); return map; } @RequestMapping("/index") public String index(Map<String,String>map){ map.put("name","hello freemarker!"); return "index"; } @RequestMapping("/welcome") public String welcome(Map<String,String>map){ map.put("name","welcom to freemarker!"); return "welcome"; } @RequestMapping("/toError") public String toError(Map<String,String>map){ map.put("name","hello freemarker!"); int a=1/0;//异常 return "index"; } }
ExceptionHandler
package com.alancode.springboot.exception; /** * @author Alan_liu * @Create 2021/2/18 0:08 */ import org.springframework.web.servlet.ModelAndView; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** *@program: SpringBootAddFreemarker *@description: ExceptionHandler *@author: Alan_Liu *@create: 2021-02-18 00:08 */ public class ExceptionHandler { /*** *定义要跳转的错误页面 *@author Alan_Liu *@date 2021/2/18 0:16 **/ public static final String MY_ERROR_PAGE="error"; @org.springframework.web.bind.annotation.ExceptionHandler(Exception.class) public Object errorHandler(HttpServletRequest request, HttpServletResponse response,Exception e){ //打印异常 System.out.println("捕获异常:"); e.printStackTrace(); ModelAndView m=new ModelAndView(); //添加异常信息 m.addObject("exception",e.getMessage()); m.addObject("url",request.getRequestURI()); //设置视图 m.setViewName(MY_ERROR_PAGE); return m; } }
Users
package com.alancode.springboot.pojo; /** * @author Alan_liu * @Create 2021/2/16 21:44 */ /** *@program: SpringBootAddJsp *@description: 用户 *@author: Alan_Liu *@create: 2021-02-16 21:44 */ public class Users { private Integer userid; private String username; private Integer userage; public Integer getUserid() { return userid; } public void setUserid(Integer userid) { this.userid = userid; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public Integer getUserage() { return userage; } public void setUserage(Integer userage) { this.userage = userage; } @Override public String toString() { return "Users{" + "userid=" + userid + ", username='" + username + '\'' + ", userage=" + userage + '}'; } public Users() { } public Users(Integer userid, String username, Integer userage) { this.userid = userid; this.username = username; this.userage = userage; } public Users(Integer userid) { this.userid = userid; } public Users(Integer userid, String username) { this.userid = userid; this.username = username; } public Users(String username) { this.username = username; } public Users(String username, Integer userage) { this.username = username; this.userage = userage; } }
FreemarkerApplication
package com.alancode.springboot; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; /** * @author Alan_Liu */ @SpringBootApplication public class FreemarkerApplication { public static void main(String[] args) { SpringApplication.run(FreemarkerApplication.class, args); } }
error.ftl
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>错误页面</title> </head> <body> 错误:${exception} <br/> 地址:${url} $END$ </body> </html>
errorPage.ftl
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>错误页面</title> </head> <body> 错误:${exception} <br/> 地址:${url} $END$ </body> </html>
index.ftl
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"/> <title>Insert title here</title> </head> <body> ${name} </body> </html>
userList.ftl
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>展示用户信息</title> </head> <body> <table border="1" align="center" width="50%"> <tr> <th>ID</th> <th>Name</th> <th>Age</th> </tr> <#list list as user > <tr> <td>${user.userid}</td> <td>${user.username}</td> <td>${user.userage}</td> </tr> </#list> </table> </body> </html>
welcome.ftl
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"/> <title>Insert title here</title> </head> <body> ${name} </body> </html>
application.properties
spring.freemarker.template-loader-path=classpath:/templates/ spring.freemarker.cache=false spring.freemarker.settings.template_update_delay=0 spring.freemarker.charset=utf-8 spring.freemarker.check-template-location=true spring.freemarker.content-type=text/html spring.freemarker.expose-request-attributes=false spring.freemarker.expose-session-attributes=false spring.freemarker.request-context-attribute=request spring.freemarker.suffix=.ftl
pom.xml
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.4.2</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.alanCode.springBoot</groupId> <artifactId>SpringBootAddFreemarker</artifactId> <version>0.0.1-SNAPSHOT</version> <name>SpringBootAddFreemarker</name> <description>SpringBoot 整合 Freemarker</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <!--freemarker 启动器的坐标--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-freemarker</artifactId> </dependency> <!--使用 freemarker时必须引入 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <optional>true</optional> <!-- 这个需要为 true 热部署才有效 --> <!-- 热部署启动后,只要重新编译工程 即可自动重新启动--> </dependency> <!--Springboot 的启动器 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <fork>true</fork> </configuration> </plugin> </plugins> </build> </project>
为人:谦逊、激情、博学、审问、慎思、明辨、 笃行
学问:纸上得来终觉浅,绝知此事要躬行
为事:工欲善其事,必先利其器。
态度:道阻且长,行则将至;行而不辍,未来可期
.....................................................................
------- 桃之夭夭,灼灼其华。之子于归,宜其室家。 ---------------
------- 桃之夭夭,有蕡其实。之子于归,宜其家室。 ---------------
------- 桃之夭夭,其叶蓁蓁。之子于归,宜其家人。 ---------------
=====================================================================
* 博客文章部分截图及内容来自于学习的书本及相应培训课程以及网络其他博客,仅做学习讨论之用,不做商业用途。
* 如有侵权,马上联系我,我立马删除对应链接。 * @author Alan -liu * @Email no008@foxmail.com
转载请标注出处! ✧*꧁一品堂.技术学习笔记꧂*✧. ---> https://www.cnblogs.com/ios9/
学问:纸上得来终觉浅,绝知此事要躬行
为事:工欲善其事,必先利其器。
态度:道阻且长,行则将至;行而不辍,未来可期
.....................................................................
------- 桃之夭夭,灼灼其华。之子于归,宜其室家。 ---------------
------- 桃之夭夭,有蕡其实。之子于归,宜其家室。 ---------------
------- 桃之夭夭,其叶蓁蓁。之子于归,宜其家人。 ---------------
=====================================================================
* 博客文章部分截图及内容来自于学习的书本及相应培训课程以及网络其他博客,仅做学习讨论之用,不做商业用途。
* 如有侵权,马上联系我,我立马删除对应链接。 * @author Alan -liu * @Email no008@foxmail.com
转载请标注出处! ✧*꧁一品堂.技术学习笔记꧂*✧. ---> https://www.cnblogs.com/ios9/