Spring框架(十六)SpringBoot框架+SpringBoot MVC应用(springMVC)+SpringBoot MVC扩展

目录
SpringBoot MVC应用(springMVC)
开发Restful服务
开发JSP应用
开发Thymeleaf应用
SpringBoot MVC扩展
SpringBoot MVC拦截器
SpringBoot MVC异常处理
SpringBoot MVC静态资源处理

SpringBoot MVC应用(springMVC)
开发Restful服务

在pom.xml导入spring-boot-starter-web工具集

<dependency>     <groupId>org.springframework.boot</groupId>     <artifactId>spring-boot-starter-web</artifactId>     <version>2.0.5.RELEASE</version> </dependency> 

在application.properties定义参数

server.port=8888 #server.servlet.contextPath=/boot 

编写一个主启动类

@SpringBootApplication public class RunBoot {      public static void main(String[] args) {         SpringApplication.run(RunBoot.class, args);     }  } 

追加一个HelloController及其请求处理

@RestController public class HelloController {      @GetMapping("/hello")     public String say(){         return "Hello SpringBoot";     }      //@RequestMapping(value="/hello1",produces="text/html;charset=UTF-8")     @GetMapping("/hello1")     public String say1(){         return "你好 SpringBoot";     }  } 

开发JSP应用

在pom.xml引入web工具集合

<dependencies>     <dependency>         <groupId>org.springframework.boot</groupId>         <artifactId>spring-boot-starter-web</artifactId>         <version>2.0.5.RELEASE</version>     </dependency>     <dependency>         <groupId>org.apache.tomcat.embed</groupId>         <artifactId>tomcat-embed-jasper</artifactId>         <version>8.5.29</version>     </dependency>     <dependency>         <groupId>jstl</groupId>         <artifactId>jstl</artifactId>         <version>1.2</version>     </dependency> </dependencies> 

在application.properties配置参数

server.port=8888  #viewResolver spring.mvc.view.prefix=/ spring.mvc.view.suffix=.jsp 

定义启动类

@SpringBootApplication public class RunBoot {      public static void main(String[] args) {         SpringApplication.run(RunBoot.class, args);     }  } 

编写HelloController

@Controller public class HelloController {      @GetMapping("/hello.do")     public ModelAndView say(){         ModelAndView mav = new ModelAndView();         mav.setViewName("hello");///webapp/hello.jsp         mav.getModel().put("msg", "Hello SpringBoot");         List<String> friends = new ArrayList<String>();         friends.add("小马哥");         friends.add("杰哥");         friends.add("郭哥");         mav.getModel().put("friends", friends);         return mav;     }  } 

在src\main\webapp下编写hello.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>     <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>     <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">     <html>     <head>     <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">     <title>Insert title here</title>     </head>     <body>     <h1>${msg }</h1>     <ul>         <c:forEach items="${friends}" var="f">         <li>${f}</li>         </c:forEach>     </ul>     </body> </html> 

开发Thymeleaf应用

Thymeleaf模板技术是替代JSP,可以生成HTML界面。

velocity、Freemarker、Thymeleaf等。

    模板文件+模型数据=HTML输出
        Velocity: hello.vm + VTL
        Freemarker: hello.ftl + FTL
        Thymeleaf: hello.html + THTL

    模板技术优点:
        简单、方便
        响应效率高
        模板文件有利于框架移植

1.在pom.xml导入web、thymeleaf包(与jsp不同)

<dependencies>     <dependency>         <groupId>org.springframework.boot</groupId>         <artifactId>spring-boot-starter-web</artifactId>         <version>2.0.5.RELEASE</version>     </dependency>     <dependency>         <groupId>org.springframework.boot</groupId>         <artifactId>spring-boot-starter-thymeleaf</artifactId>         <version>2.0.1.RELEASE</version>     </dependency> </dependencies> 

2.在application.properties定义参数(与jsp不同)

server.port=8888 

3.定义启动类(与jsp相同)

@SpringBootApplication public class RunBoot {      public static void main(String[] args) {         SpringApplication.run(RunBoot.class, args);     } } 

4.定义HelloController(与jsp相同)

@Controller public class HelloController {     @GetMapping("/hello.do")     public ModelAndView say(){         ModelAndView mav = new ModelAndView();         mav.setViewName("hello");// src/main/resources/templates/hello.html         mav.getModel().put("msg", "Hello Thymeleaf");         List<String> friends = new ArrayList<String>();         friends.add("小马哥");         friends.add("杰哥");         friends.add("郭哥");         mav.getModel().put("friends", friends);         return mav;     }    } 

5.在src/main/resources/templates目录下定义模板文件(与jsp不同)

<!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <head>     <meta charset="UTF-8"/>     <title>Insert title here</title> </head>     <body>         <h1 th:text="${msg}"></h1>         <input type="text" th:value="${msg}"/>     </body> </html> 

SpringBoot MVC扩展
SpringBoot MVC拦截器

SpringBoot MVC拦截器和SpringMVC拦截器编写规则相同,配置不同。

1.编写一个拦截器,实现HandlerInterceptor接口

@Component public class SomeInterceptor implements HandlerInterceptor{      public boolean preHandle(         HttpServletRequest request,          HttpServletResponse response, Object handler)             throws Exception {         System.out.println("执行了拦截器");         return true;     }    } 

2.配置拦截器组件,指定拦截哪些请求

@Component public class MyInterceptorConfiguration implements WebMvcConfigurer{      @Autowired     private SomeInterceptor some;      public void addInterceptors(InterceptorRegistry registry) {         registry.addInterceptor(some).addPathPatterns("/hello.do");     }    } 

SpringBoot MVC异常处理
SpringBoot MVC静态资源处理

SpringBootMVC提供了几个静态资源目录,在src\main\resources下,分别为
    public(优先级最低)
    static
    resources
    META-INF/resources(优先级最高)

如果自定义文件夹放静态资源,可以编写配置类

@Component public class MyResourceConfiguration implements WebMvcConfigurer{      //重写接口方法,重新配置静态资源访问路径     public void addResourceHandlers(ResourceHandlerRegistry registry){         registry.addResourceHandler("/**")             .addResourceLocations(                 "classpath:/mystatic/",                 "classpath:/META-INF/resources/",                 "classpath:/resources/",                 "classpath:/static/",                 "classpath:/public/");     } } 
  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值