1.Demo
(1)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 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.mxj</groupId> <artifactId>08-spring-boot-view-jsp</artifactId> <version>1.0-SNAPSHOT</version> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.21.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <properties> <java.version>1.8</java.version> </properties> <dependencies> <!--springBoot启动器--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!--jstl--> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> </dependency> <!--jasper--> <dependency> <groupId>org.apache.tomcat.embed</groupId> <artifactId>tomact-embed-jasper</artifactId> <scope>provided</scope> </dependency> </dependencies> </project>
(2)配置Controller
@Controller public class Helloworld { @RequestMapping("/hello") @ResponseBody public Map<String,Object> showHelloWorld(){ Map<String,Object> map = new HashMap<>(); map.put("msg","HelloWorld"); return map; } }
(3)编写启动器
注意:启动器存放的位置可以和Controller位于同一个包下,或者位于Controller的上一级包中,但是不能放在Controller的平级以及子包下
/** * SpringBoot 启动类 */ @SpringBootApplication public class App { public static void main(String[] args){ SpringApplication.run(App.class,args); } }
2.SpringBoot整合Servlet
(1)通过注解扫描完成Servlet组件的注册
编写Servlet
@WebServlet(name="FirstServlet",urlPatterns="/first") public class FirstServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { super.doGet(req, resp); } }
编写启动类
@SpringBootApplication @ServletComponentScan //在springBoot启动时会扫描@WebServlet,并将该类实例化 public class App { public static void main(String[] args){ SpringApplication.run(App.class,args); } }
(2)通过方法完成Servlet组件的注册
编写Servlet
public class SecondServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { System.out.println("SecondServlet.............."); } }
编写启动类
@SpringBootApplication public class App2 { public static void main(String[] args){ SpringApplication.run(App2.class,args); } @Bean public ServletRegistrationBean getServletRegistrationBean(){ ServletRegistrationBean bean = new ServletRegistrationBean(new SecondServlet()); bean.addUrlMappings("/second"); return bean; } }
3. SpringBoot整合Filter
(1)通过注解扫描完成Filter组件的注册
编写Filter
//@WebFilter(filterName = "FirstFilter",urlPatterns = {"*.do","*.jsp"}) @WebFilter(filterName="FirstFilter",urlPatterns={"/first"}) public class FirstFilter implements Filter { @Override public void init(FilterConfig filterConfig) throws ServletException { } @Override public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { System.out.println("进入Filter"); filterChain.doFilter(servletRequest,servletResponse); System.out.println("离开Filter"); } @Override public void destroy() { } }
编写启动类
@SpringBootApplication @ServletComponentScan public class App { public static void main(String[] args){ SpringApplication.run(App.class,args); } }
(2)通过方法完成Filter组件的注册
编写Filter
public class SecondFilter implements Filter { @Override public void init(FilterConfig filterConfig) throws ServletException { } @Override public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { System.out.println("进入SecondFilter"); filterChain.doFilter(servletRequest,servletResponse); System.out.println("离开SecondFilter"); } @Override public void destroy() { } }
编写启动类
@SpringBootApplication public class App2 { public static void main(String[] args){ SpringApplication.run(App2.class,args); } /** * 注册Servlet */ @Bean public ServletRegistrationBean getervletRegistrationBean(){ ServletRegistrationBean bean = new ServletRegistrationBean(new SecondServlet()); bean.addUrlMappings("/second"); return bean; } /** * 注册Filter */ @Bean public FilterRegistrationBean getFilterRegistrationBean(){ FilterRegistrationBean bean = new FilterRegistrationBean(new SecondFilter()); //bean.addUrlPatterns(new String[]{"*.do","*.jsp"}); bean.addUrlPatterns("/second"); return bean; } }
4.SpringBoot整合Listener
(1)通过注解扫描完成Listener组件注册
编写Listener
@WebListener public class FirstListener implements ServletContextListener { public void contextInitialized(ServletContextEvent sce) { } public void contextDestroyed(ServletContextEvent sce) { System.out.println("Listener......init......"); } }
编写启动类
@SpringBootApplication @ServletComponentScan public class App { public static void main(String[] args){ SpringApplication.run(App.class,args); } }
(2)通过方法完成Listener组件注册
编写Listener
public class SecondListener implements ServletContextListener { public void contextInitialized(ServletContextEvent sce) { } public void contextDestroyed(ServletContextEvent sce) { System.out.println("SecondListener......init......"); } }
编写启动类
@SpringBootApplication public class App2 { public static void main(String[] args){ SpringApplication.run(App2.class,args); } /** * 注册listener */ public ServletListenerRegistrationBean<SecondListener> getServletListenerRegistrationBean(){ ServletListenerRegistrationBean<SecondListener> bean= new ServletListenerRegistrationBean<SecondListener>(new SecondListener()); return bean; } }
5.SpringBoot访问静态资源
(1)classpath/static目录
(2)ServletContext根目录(src/main/webapp)
6.SpringBoot文件上传
前端
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>文件上传</title> </head> <body> <from action="fileUploadController" method="post" enctype="multipart/form-data"> 上传文件:<input type="file" name="filename"/><br/> <input type="submit" value="提交"/> </from> </body> </html>
编写Controller
@RestController //表示该类下的方法的返回值会自动做json格式的转换 public class FileUploadcontroller { /** * 处理文件上传 */ @RequestMapping("/fileUploadController") public Map<String,Object> fileUpload(MultipartFile filename) throws IOException { System.out.println(filename.getOriginalFilename()); filename.transferTo(new File("d:/"+filename.getOriginalFilename())); Map<String,Object> map = new HashMap<>(); map.put("msg","ok"); return map; } }
编写启动类
@SpringBootApplication public class App { public static void main(String[] args){ SpringApplication.run(App.class,args); } }
设置上传文件的默认值,main/resources/application.properties
spring.http.multipart.max-file-size=200MB //设置单个上传文件的大小
spring.http.multipart.max-request-size=200MB //设置一次请求上传文件的总容量
7.SpringBoot整合jsp技术
(1)创建工程
(2)修改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 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.mxj</groupId> <artifactId>08-spring-boot-view-jsp</artifactId> <version>1.0-SNAPSHOT</version> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.21.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <properties> <java.version>1.8</java.version> </properties> <dependencies> <!--springBoot启动器--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <!--jstl--> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> </dependency> <!--jasper--> <dependency> <groupId>org.apache.tomcat.embed</groupId> <artifactId>tomact-embed-jasper</artifactId> <scope>provided</scope> </dependency> </dependencies> </project>
(3)创建SpringBoot的全局配置文件:application.properties
spring.mvc.view.prefix=/WEB-INF/jsp/
spring.mvc.view.suffix=.jsp
(4)创建Controller
@Controller public class UserController { /** * 处理请求,产生数据 */ @RequestMapping("/showUser") public ModelAndView showUser(Model model){ List<Users> list = new ArrayList<>(); list.add(new Users(1,"张三",20)); list.add(new Users(2,"李四",22)); list.add(new Users(3,"王五",24)); System.out.println("UserController启动"); //Model对象 model.addAttribute("list",list); ModelAndView mav = new ModelAndView("/WEB-INF/jsp/userList.jsp"); //跳转视图 return mav; } }
(5)创建jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> <html> <head> <title>Title</title> </head> <body> <table border="1" align="center" width="50%"> <tr> <th>Id</th> <th>Name</th> <th>Age</th> </tr> <c:forEach items="${list}" var="user"> <tr> <td>${user.userid}</td> <td>${user.username}</td> <td>${user.userage}</td> </tr> </c:forEach> </table> </body> </html>
(6)创建启动类
@SpringBootApplication public class App { public static void main(String[] args){ SpringApplication.run(App.class,args); } }
8.SpringBoot整合Freemarker
(1)创建项目
(2)修改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 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.mxj</groupId> <artifactId>08-spring-boot-view-freemarker</artifactId> <version>1.0-SNAPSHOT</version> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.6.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!--freemarker启动器坐标--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-freemarker</artifactId> </dependency> </dependencies> </project>
(3)编写视图
注意:SpringBoot要求模板形式的视图层文件必须放到src/main/resources/templates文件夹下
<html> <head> <title>展示用户</title> <meta charset="utf-8"></meta> </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>
(4)编写Controller
@Controller public class UserController { /** * 处理请求,产生数据 */ @RequestMapping("/showUser") public String showUser(Model model){ List<Users> list = new ArrayList<>(); list.add(new Users(1,"张三",20)); list.add(new Users(2,"李四",22)); list.add(new Users(3,"王五",24)); //Model对象 model.addAttribute("list",list); //跳转视图 return "userList"; } }
(5)创建启动器
@SpringBootApplication public class App { public static void main(String[] args){ SpringApplication.run(App.class,args); } }