详情请参考:《使用IDEA创建一个springboot项目》
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"; } }
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"; } }
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; } }
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; } }
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); } }
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>错误页面</title> </head> <body> 错误:${exception} <br/> 地址:${url} $END$ </body> </html>
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>错误页面</title> </head> <body> 错误:${exception} <br/> 地址:${url} $END$ </body> </html>
原文:https://www.cnblogs.com/ios9/p/14413619.html