zoukankan      html  css  js  c++  java
  • SpringBoot MVC 拦截器

    SpringBoot MVC

    环境搭建

    1. 在pom.xml添加spring-boot-starter-web

      1.  
        <dependency>
      2.  
        <groupId>org.springframework.boot</groupId>
      3.  
        <artifactId>spring-boot-starter-web</artifactId>
      4.  
        </dependency>
    2. 修改application.yml配置

      1.  
         
      2.  
        spring:
      3.  
        datasource:
      4.  
        username: system
      5.  
        password: 123456
      6.  
        url: jdbc:oracle:thin:@localhost:1521:XE
      7.  
        driverClassName: oracle.jdbc.OracleDriver
      8.  
        server:
      9.  
        port: 8889
    3. 编写Controller,使用@RequestMapping和@RestController

      1.  
        @RestController
      2.  
        public class BookService {
      3.  
         
      4.  
        @Autowired
      5.  
        private BookDao bookDao;
      6.  
         
      7.  
        @RequestMapping(value="/book",method=RequestMethod.GET)
      8.  
        public List<Book> loadAll(){
      9.  
        return bookDao.findAll();
      10.  
        }
      11.  
         
      12.  
        @RequestMapping(value="/book/{id}",method=RequestMethod.GET)
      13.  
        public Book load(@PathVariable("id")int id){
      14.  
        return bookDao.findById(id);
      15.  
        }
      16.  
         
      17.  
        @RequestMapping(value="/book/{id}",method=RequestMethod.DELETE)
      18.  
        public boolean delete(@PathVariable("id")int id){
      19.  
        int row = bookDao.deleteById(id);
      20.  
        if(row >= 1){
      21.  
        return true;
      22.  
        }else{
      23.  
        return false;
      24.  
        }
      25.  
        }
      26.  
         
      27.  
        @RequestMapping(value="/book/{id}",method=RequestMethod.PUT)
      28.  
        public boolean update(@PathVariable("id")int id, @RequestBody Book book){
      29.  
        book.setId(id);
      30.  
        int row = bookDao.update(book);
      31.  
        if(row >= 1){
      32.  
        return true;
      33.  
        }else{
      34.  
        return false;
      35.  
        }
      36.  
        }
      37.  
         
      38.  
        @RequestMapping(value="/book",method=RequestMethod.POST)
      39.  
        public boolean add(@RequestBody Book book){
      40.  
        bookDao.save(book);
      41.  
        return true;
      42.  
        }
      43.  
        }
    4. 编写Filter,解决Ajax调用跨域问题

      1.  
        @WebFilter(servletNames={"dispatcherServlet"})
      2.  
        public class AjaxDomainFilter implements Filter{
      3.  
         
      4.  
        @Override
      5.  
        public void destroy() {
      6.  
        // TODO Auto-generated method stub
      7.  
         
      8.  
        }
      9.  
         
      10.  
        @Override
      11.  
        public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
      12.  
        throws IOException, ServletException {
      13.  
        HttpServletResponse httpResponse = (HttpServletResponse)response;
      14.  
        httpResponse.setHeader("Access-Control-Allow-Origin", "*");//设置允许哪些域名应用进行ajax访问
      15.  
        httpResponse.setHeader("Access-Control-Allow-Methods", "GET,PUT,POST,DELETE");
      16.  
        httpResponse.setHeader("Access-Control-Allow-Headers", "Content-Type");
      17.  
        chain.doFilter(request, response);//调用后续serlvet
      18.  
        }
      19.  
         
      20.  
        @Override
      21.  
        public void init(FilterConfig arg0) throws ServletException {
      22.  
        // TODO Auto-generated method stub
      23.  
         
      24.  
        }
      25.  
         
      26.  
        }
    5. 配置Filter,使用@WebFilter和@ServletComponentScan

      1.  
        @SpringBootApplication//开启扫描、自动配置、bean定义
      2.  
        @MapperScan(basePackages={"cn.xdl.dao"})//开启扫描Mapper接口
      3.  
        @ServletComponentScan//开启WebFilter、WebServlet、WebListener配置支持
      4.  
        public class BootApplication {
      5.  
         
      6.  
        public static void main(String[] args){
      7.  
        SpringApplication.run(BootApplication.class, args);
      8.  
        }
      9.  
         
      10.  
        }
    6. 修改pc_web前端的common.js,将请求路径改为

      var path = "http://localhost:8889";
      

    SpringBoot 热启动

    当修改完毕程序代码后,可以自动重新部署。

    1.  
      <dependency>
    2.  
      <groupId>org.springframework.boot</groupId>
    3.  
      <artifactId>spring-boot-devtools</artifactId>
    4.  
      </dependency>

    如果修改了pom.xml定义,要从新启动,修改了java代码可以自动重新启动。

    SpringBoot MyBatis分页

    1.  
      <dependency>
    2.  
      <groupId>com.github.pagehelper</groupId>
    3.  
      <artifactId>pagehelper-spring-boot-starter</artifactId>
    4.  
      <version>1.2.3</version>
    5.  
      </dependency>

    然后在调用查询代码之前执行PageHelper.startPage方法

    1.  
      Page page = PageHelper.startPage(p, 3);
    2.  
      //分页查询
    3.  
      List list = bookDao.findAll();
    4.  
      //获取总页数
    5.  
      int totalPage = page.getPages();

    SpringBoot 拦截器

    1. 编写拦截器类,实现HandlerInterceptor接口,实现PreHandle、PostHandle、afterCompletion方法

      1.  
        public class AjaxDomainInterceptor implements HandlerInterceptor{
      2.  
         
      3.  
        @Override
      4.  
        public boolean preHandle(
      5.  
        HttpServletRequest request,
      6.  
        HttpServletResponse response, Object handler)
      7.  
        throws Exception {
      8.  
        response.setHeader("Access-Control-Allow-Origin", "*");//设置允许哪些域名应用进行ajax访问
      9.  
        response.setHeader("Access-Control-Allow-Methods", "GET,PUT,POST,DELETE");
      10.  
        response.setHeader("Access-Control-Allow-Headers", "Content-Type");
      11.  
        return true;
      12.  
        }
      13.  
         
      14.  
        //...
      15.  
        }
    2. 编写配置类,继承WebMvcConfigurerAdapter,重写addInterceptors方法

      1.  
        //@Configuration
      2.  
        @Component
      3.  
        public class InterceptorConfiguration extends WebMvcConfigurerAdapter{
      4.  
         
      5.  
        public void addInterceptors(InterceptorRegistry registry) {
      6.  
        //配置拦截器
      7.  
        AjaxDomainInterceptor ajaxDomain = new AjaxDomainInterceptor();
      8.  
        registry.addInterceptor(ajaxDomain).addPathPatterns("/**");
      9.  
        }
      10.  
         
      11.  
        }
     
  • 相关阅读:
    过渡效果
    生命周期
    事件处理
    列表的搜索和排序
    DotNetBar for Windows Forms 12.1.0.0_冰河之刃重打包版 原创发布
    DotNetBar for Windows Forms 11.8.0.8冰河之刃重打包版
    闲读
    POJ 3253 Fence Repair 贪心 优先级队列
    POJ 2431 Expedition 贪心 优先级队列
    优先级队列-堆-STL实现
  • 原文地址:https://www.cnblogs.com/yelanggu/p/10299637.html
Copyright © 2011-2022 走看看