• spring boot学习(3) SpringBoot 之MVC 支持


    第一节:@RequestMapping 配置url 映射
     
    第二节:@Controller 处理http 请求
    转发到一个页面,以前是转发到jsp页面,现在使用freemarker;
    在pom.xml页面右键,spring-edit starters , 添加freemarker支持:spring-boot-starter-freemarker
    pom.xml:
          <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-freemarker</artifactId>
            </dependency>

    com.cy.controller.HelloWorldFreemarkerController.java:

    package com.cy.controller;
    
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.servlet.ModelAndView;
    
    @Controller
    @RequestMapping("/freemarker")
    public class HelloWorldFreemarkerController {
        
        @RequestMapping("/say")
        public ModelAndView say(){
            ModelAndView mav=new ModelAndView();
            mav.addObject("message", "springboot你好!");
            mav.setViewName("helloWorld");
            return mav;
        }
        
    }

    freemarker模板是ftl为后缀的;src/main/resources/templates/helloWorld.ftl:

    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="UTF-8">
    <title>Insert title here</title>
    </head>
    <body>
    show: ${message}
    </body>
    </html>

    浏览器http://localhost:8888/HelloWorld/freemarker/say:显示:show: springboot你好!

    第三节:@RestController 处理ajax 请求
     
    第四节:@PathVariable 获取url 参数
     
    第五节:@RequestParam 获取请求参数

    例子如下:

    com.cy.controller.HelloWorldAjaxController.java:

    package com.cy.controller;
    
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    
    @RestController
    @RequestMapping("/ajax")
    public class HelloWorldAjaxController {
        
        @RequestMapping("/hello")
        public String say(){
            return "{'message1':'SpringBoot你好','message2','Spring你好2'}";
        }
    }

    com.cy.controller.BlogController.java:

    package com.cy.controller;
    
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.PathVariable;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestParam;
    import org.springframework.web.servlet.ModelAndView;
    
    @Controller
    @RequestMapping("/blog")
    public class BlogController {
        
        @RequestMapping("/{id}")
        public ModelAndView show(@PathVariable("id") Integer id){
            ModelAndView mav=new ModelAndView();
            mav.addObject("id", id);
            mav.setViewName("blog");
            return mav;
        }
        
        @RequestMapping("/query")
        public ModelAndView query(@RequestParam(value="q",required=false) String q){
            ModelAndView mav=new ModelAndView();
            mav.addObject("q", q);
            mav.setViewName("query");
            return mav;
        }
        
    }

    webapp/index.html:

    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="UTF-8">
    <title>Insert title here</title>
    <script src="resources/jquery/jquery.min.js"></script>
    <script type="text/javascript">
        
        function show(){
            $.post("ajax/hello",{},function(result){
                alert(result);
            });
        }
        
    </script>
    </head>
    <body>
    <button onclick="show()">点击</button>
    <a href="/HelloWorld/blog/21">博客</a>
    <a href="/HelloWorld/blog/query?q=123456">搜索</a>
    </body>
    </html>

    src/main/resoureces/templates/blog.ftl:

    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="UTF-8">
    <title>Insert title here</title>
    </head>
    <body>
    博客id:${id}
    </body>
    </html>
    View Code

    src/main/resources/templates/query.ftl:

    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="UTF-8">
    <title>Insert title here</title>
    </head>
    <body>
    q: ${q}
    </body>
    </html>
    View Code

    测试:

    浏览器:http://localhost:8888/HelloWorld/index.html

    点击按钮:alert: {'message1':'SpringBoot你好','message2','Spring你好2'}

    点击博客链接:转发页面,显示博客id:21

    点击搜索,到页面http://localhost:8888/HelloWorld/blog/query?q=123456,显示q: 123456

  • 相关阅读:
    GreenPlum 锁表以及解除锁定
    Postgresql 解决锁表
    Greenplum 查看连接与锁信息数据字典
    Greenplum 常用数据字典
    Linux 内核参数说明
    Greenplum 如何直连segment节点
    GreenPlum 数据备份与恢复
    unity 获取DontDestroyOnLoad的游戏对象
    scheduleOnce时出错,CCScheduler#scheduleSelector. Selector already scheduled. Updating interval from: 0 to 0"
    正比适配,留黑边
  • 原文地址:https://www.cnblogs.com/tenWood/p/8641778.html
Copyright © 2020-2023  润新知